CREATE TABLE `admin_users` (
	`id` bigint unsigned AUTO_INCREMENT NOT NULL,
	`email` varchar(150) NOT NULL,
	`password_hash` varchar(255) NOT NULL,
	`role` enum('owner','operator') NOT NULL DEFAULT 'operator',
	`created_at` datetime NOT NULL,
	CONSTRAINT `admin_users_id` PRIMARY KEY(`id`),
	CONSTRAINT `admin_users_email_unique` UNIQUE(`email`)
);
--> statement-breakpoint
CREATE TABLE `device_events` (
	`id` bigint unsigned AUTO_INCREMENT NOT NULL,
	`unit_id` bigint unsigned NOT NULL,
	`level` enum('DEBUG','INFO','WARN','ERROR') NOT NULL,
	`tag` varchar(40) NOT NULL,
	`message` text NOT NULL,
	`occurred_at` datetime NOT NULL,
	CONSTRAINT `device_events_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `locations` (
	`id` bigint unsigned AUTO_INCREMENT NOT NULL,
	`name` varchar(100) NOT NULL,
	`address` varchar(255),
	`created_at` datetime NOT NULL,
	`updated_at` datetime NOT NULL,
	CONSTRAINT `locations_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `operator_locations` (
	`user_id` bigint unsigned NOT NULL,
	`location_id` bigint unsigned NOT NULL,
	CONSTRAINT `operator_locations_user_id_location_id_pk` PRIMARY KEY(`user_id`,`location_id`)
);
--> statement-breakpoint
CREATE TABLE `sessions` (
	`id` bigint unsigned AUTO_INCREMENT NOT NULL,
	`voucher_id` bigint unsigned NOT NULL,
	`unit_id` bigint unsigned NOT NULL,
	`session_token` char(64) NOT NULL,
	`started_at` datetime NOT NULL,
	`ends_at` datetime NOT NULL,
	`ended_at` datetime,
	`end_reason` enum('user_stop','expired','call_ended','admin_stop'),
	CONSTRAINT `sessions_id` PRIMARY KEY(`id`),
	CONSTRAINT `sessions_voucher_id_unique` UNIQUE(`voucher_id`),
	CONSTRAINT `sessions_session_token_unique` UNIQUE(`session_token`)
);
--> statement-breakpoint
CREATE TABLE `unit_heartbeats` (
	`id` bigint unsigned AUTO_INCREMENT NOT NULL,
	`unit_id` bigint unsigned NOT NULL,
	`app_version` varchar(20),
	`battery_pct` tinyint unsigned,
	`wifi_rssi` smallint,
	`session_active` boolean NOT NULL DEFAULT false,
	`received_at` datetime NOT NULL,
	CONSTRAINT `unit_heartbeats_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `units` (
	`id` bigint unsigned AUTO_INCREMENT NOT NULL,
	`code` varchar(20) NOT NULL,
	`name` varchar(100) NOT NULL,
	`location_id` bigint unsigned NOT NULL,
	`api_key_hash` char(64) NOT NULL,
	`is_active` boolean NOT NULL DEFAULT true,
	`created_at` datetime NOT NULL,
	`updated_at` datetime NOT NULL,
	CONSTRAINT `units_id` PRIMARY KEY(`id`),
	CONSTRAINT `units_code_unique` UNIQUE(`code`)
);
--> statement-breakpoint
CREATE TABLE `vouchers` (
	`id` bigint unsigned AUTO_INCREMENT NOT NULL,
	`code` varchar(32) NOT NULL,
	`duration_minutes` int unsigned NOT NULL,
	`status` enum('unused','used','expired','void') NOT NULL DEFAULT 'unused',
	`batch_id` bigint unsigned,
	`valid_until` datetime,
	`used_at` datetime,
	`unit_id` bigint unsigned,
	`created_at` datetime NOT NULL,
	CONSTRAINT `vouchers_id` PRIMARY KEY(`id`),
	CONSTRAINT `vouchers_code_unique` UNIQUE(`code`)
);
--> statement-breakpoint
ALTER TABLE `device_events` ADD CONSTRAINT `device_events_unit_id_units_id_fk` FOREIGN KEY (`unit_id`) REFERENCES `units`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `operator_locations` ADD CONSTRAINT `operator_locations_user_id_admin_users_id_fk` FOREIGN KEY (`user_id`) REFERENCES `admin_users`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `operator_locations` ADD CONSTRAINT `operator_locations_location_id_locations_id_fk` FOREIGN KEY (`location_id`) REFERENCES `locations`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `sessions` ADD CONSTRAINT `sessions_voucher_id_vouchers_id_fk` FOREIGN KEY (`voucher_id`) REFERENCES `vouchers`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `sessions` ADD CONSTRAINT `sessions_unit_id_units_id_fk` FOREIGN KEY (`unit_id`) REFERENCES `units`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `unit_heartbeats` ADD CONSTRAINT `unit_heartbeats_unit_id_units_id_fk` FOREIGN KEY (`unit_id`) REFERENCES `units`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `units` ADD CONSTRAINT `units_location_id_locations_id_fk` FOREIGN KEY (`location_id`) REFERENCES `locations`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `vouchers` ADD CONSTRAINT `vouchers_unit_id_units_id_fk` FOREIGN KEY (`unit_id`) REFERENCES `units`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE INDEX `idx_event_unit_time` ON `device_events` (`unit_id`,`occurred_at`);--> statement-breakpoint
CREATE INDEX `idx_event_level` ON `device_events` (`level`);--> statement-breakpoint
CREATE INDEX `idx_session_unit_time` ON `sessions` (`unit_id`,`started_at`);--> statement-breakpoint
CREATE INDEX `idx_hb_unit_time` ON `unit_heartbeats` (`unit_id`,`received_at`);--> statement-breakpoint
CREATE INDEX `idx_voucher_status` ON `vouchers` (`status`);--> statement-breakpoint
CREATE INDEX `idx_voucher_batch` ON `vouchers` (`batch_id`);