CREATE TABLE `circuit_sections` (
	`id` text PRIMARY KEY NOT NULL,
	`circuit_list_id` text NOT NULL,
	`key` text NOT NULL,
	`display_name` text NOT NULL,
	`prefix` text NOT NULL,
	`sort_order` integer NOT NULL DEFAULT 0,
	FOREIGN KEY (`circuit_list_id`) REFERENCES `circuit_lists`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `circuit_sections_list_key_unique` ON `circuit_sections` (`circuit_list_id`,`key`);
--> statement-breakpoint
CREATE UNIQUE INDEX `circuit_sections_list_prefix_unique` ON `circuit_sections` (`circuit_list_id`,`prefix`);
--> statement-breakpoint

CREATE TABLE `circuits` (
	`id` text PRIMARY KEY NOT NULL,
	`circuit_list_id` text NOT NULL,
	`section_id` text NOT NULL,
	`equipment_identifier` text NOT NULL,
	`display_name` text,
	`sort_order` integer NOT NULL DEFAULT 0,
	`protection_type` text,
	`protection_rated_current` real,
	`protection_characteristic` text,
	`cable_type` text,
	`cable_cross_section` text,
	`cable_length` real,
	`rcd_assignment` text,
	`terminal_designation` text,
	`voltage` real,
	`status` text,
	`is_reserve` integer NOT NULL DEFAULT 0,
	`remark` text,
	FOREIGN KEY (`circuit_list_id`) REFERENCES `circuit_lists`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`section_id`) REFERENCES `circuit_sections`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `circuits_list_equipment_identifier_unique` ON `circuits` (`circuit_list_id`,`equipment_identifier`);
--> statement-breakpoint

CREATE TABLE `circuit_device_rows` (
	`id` text PRIMARY KEY NOT NULL,
	`circuit_id` text NOT NULL,
	`linked_project_device_id` text,
	`legacy_consumer_id` text,
	`sort_order` integer NOT NULL DEFAULT 0,
	`name` text NOT NULL,
	`display_name` text NOT NULL,
	`phase_type` text,
	`connection_kind` text,
	`cost_group` text,
	`category` text,
	`level` text,
	`room_id` text,
	`room_number_snapshot` text,
	`room_name_snapshot` text,
	`quantity` integer NOT NULL,
	`power_per_unit` real NOT NULL,
	`simultaneity_factor` real NOT NULL,
	`cos_phi` real,
	`remark` text,
	`overridden_fields` text,
	FOREIGN KEY (`circuit_id`) REFERENCES `circuits`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`linked_project_device_id`) REFERENCES `project_devices`(`id`) ON UPDATE no action ON DELETE set null,
	FOREIGN KEY (`room_id`) REFERENCES `rooms`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint

CREATE TABLE `legacy_consumer_circuit_migrations` (
	`consumer_id` text PRIMARY KEY NOT NULL,
	`circuit_id` text NOT NULL,
	`circuit_device_row_id` text NOT NULL,
	`circuit_list_id` text NOT NULL,
	`created_at_iso` text NOT NULL,
	FOREIGN KEY (`circuit_id`) REFERENCES `circuits`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`circuit_device_row_id`) REFERENCES `circuit_device_rows`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`circuit_list_id`) REFERENCES `circuit_lists`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint

CREATE TABLE `legacy_consumer_migration_reports` (
	`id` text PRIMARY KEY NOT NULL,
	`circuit_list_id` text NOT NULL,
	`legacy_consumer_count` integer NOT NULL,
	`created_circuit_count` integer NOT NULL,
	`created_device_row_count` integer NOT NULL,
	`duplicate_grouped_count` integer NOT NULL,
	`generated_identifier_count` integer NOT NULL,
	`unassigned_row_count` integer NOT NULL,
	`warnings_json` text NOT NULL,
	`generated_identifiers_json` text NOT NULL,
	`duplicate_groups_json` text NOT NULL,
	`created_at_iso` text NOT NULL,
	FOREIGN KEY (`circuit_list_id`) REFERENCES `circuit_lists`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `legacy_consumer_migration_reports_list_unique` ON `legacy_consumer_migration_reports` (`circuit_list_id`);

