CREATE TABLE `circuit_list_equipment_identifiers` (
	`owner_type` text NOT NULL,
	`owner_id` text NOT NULL,
	`circuit_list_id` text NOT NULL,
	`equipment_identifier` text NOT NULL,
	FOREIGN KEY (`circuit_list_id`) REFERENCES `circuit_lists`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `circuit_list_equipment_identifiers_owner_unique` ON `circuit_list_equipment_identifiers` (`owner_type`,`owner_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `circuit_list_equipment_identifiers_list_identifier_unique` ON `circuit_list_equipment_identifiers` (`circuit_list_id`,`equipment_identifier`);--> statement-breakpoint
CREATE TABLE `circuit_protection_devices` (
	`circuit_id` text PRIMARY KEY NOT NULL,
	`type` text NOT NULL,
	`rated_current_a` real NOT NULL,
	`fuse_utilization_category` text,
	`trip_characteristic` text,
	`rcd_type` text,
	`rated_residual_current_ma` real,
	FOREIGN KEY (`circuit_id`) REFERENCES `circuits`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `distribution_board_component_protection_devices` (
	`component_id` text PRIMARY KEY NOT NULL,
	`type` text NOT NULL,
	`rated_current_a` real NOT NULL,
	`fuse_utilization_category` text,
	`trip_characteristic` text,
	`rcd_type` text,
	`rated_residual_current_ma` real,
	FOREIGN KEY (`component_id`) REFERENCES `distribution_board_components`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `distribution_board_components` (
	`id` text PRIMARY KEY NOT NULL,
	`circuit_list_id` text NOT NULL,
	`section_id` text,
	`equipment_identifier` text NOT NULL,
	`name` text NOT NULL,
	`role` text NOT NULL,
	`placement` text NOT NULL,
	`sort_order` integer DEFAULT 0 NOT NULL,
	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 `distribution_board_components_list_identifier_unique` ON `distribution_board_components` (`circuit_list_id`,`equipment_identifier`);--> statement-breakpoint
CREATE UNIQUE INDEX `distribution_board_components_section_role_unique` ON `distribution_board_components` (`section_id`,`role`);--> statement-breakpoint
ALTER TABLE `circuit_sections` ADD `category` text;--> statement-breakpoint
ALTER TABLE `circuit_sections` ADD `group_number` integer;--> statement-breakpoint
CREATE UNIQUE INDEX `circuit_sections_list_category_group_unique` ON `circuit_sections` (`circuit_list_id`,`category`,`group_number`);--> statement-breakpoint
UPDATE `circuit_sections`
SET
	`category` = CASE `key`
		WHEN 'lighting' THEN 'lighting'
		WHEN 'single_phase' THEN 'single_phase'
		WHEN 'three_phase' THEN 'three_phase'
	END,
	`group_number` = 1
WHERE `key` IN ('lighting', 'single_phase', 'three_phase');--> statement-breakpoint
INSERT INTO `circuit_list_equipment_identifiers` (
	`owner_type`,
	`owner_id`,
	`circuit_list_id`,
	`equipment_identifier`
)
SELECT
	'circuit',
	`id`,
	`circuit_list_id`,
	`equipment_identifier`
FROM `circuits`;--> statement-breakpoint
CREATE UNIQUE INDEX `circuit_list_equipment_identifiers_normalized_unique`
ON `circuit_list_equipment_identifiers` (
	`circuit_list_id`,
	lower(trim(`equipment_identifier`))
);--> statement-breakpoint
CREATE UNIQUE INDEX `distribution_board_components_fixed_role_unique`
ON `distribution_board_components` (`circuit_list_id`, `role`)
WHERE `role` IN ('main_switch', 'surge_protective_device');--> statement-breakpoint
CREATE TRIGGER `circuits_equipment_identifier_insert`
AFTER INSERT ON `circuits`
BEGIN
	INSERT INTO `circuit_list_equipment_identifiers` (
		`owner_type`,
		`owner_id`,
		`circuit_list_id`,
		`equipment_identifier`
	)
	VALUES (
		'circuit',
		NEW.`id`,
		NEW.`circuit_list_id`,
		NEW.`equipment_identifier`
	);
END;--> statement-breakpoint
CREATE TRIGGER `circuits_equipment_identifier_update`
AFTER UPDATE OF `circuit_list_id`, `equipment_identifier` ON `circuits`
BEGIN
	UPDATE `circuit_list_equipment_identifiers`
	SET
		`circuit_list_id` = NEW.`circuit_list_id`,
		`equipment_identifier` = NEW.`equipment_identifier`
	WHERE
		`owner_type` = 'circuit'
		AND `owner_id` = OLD.`id`;
END;--> statement-breakpoint
CREATE TRIGGER `circuits_equipment_identifier_delete`
AFTER DELETE ON `circuits`
BEGIN
	DELETE FROM `circuit_list_equipment_identifiers`
	WHERE
		`owner_type` = 'circuit'
		AND `owner_id` = OLD.`id`;
END;--> statement-breakpoint
CREATE TRIGGER `distribution_board_components_equipment_identifier_insert`
AFTER INSERT ON `distribution_board_components`
BEGIN
	INSERT INTO `circuit_list_equipment_identifiers` (
		`owner_type`,
		`owner_id`,
		`circuit_list_id`,
		`equipment_identifier`
	)
	VALUES (
		'distribution_board_component',
		NEW.`id`,
		NEW.`circuit_list_id`,
		NEW.`equipment_identifier`
	);
END;--> statement-breakpoint
CREATE TRIGGER `distribution_board_components_equipment_identifier_update`
AFTER UPDATE OF `circuit_list_id`, `equipment_identifier`
ON `distribution_board_components`
BEGIN
	UPDATE `circuit_list_equipment_identifiers`
	SET
		`circuit_list_id` = NEW.`circuit_list_id`,
		`equipment_identifier` = NEW.`equipment_identifier`
	WHERE
		`owner_type` = 'distribution_board_component'
		AND `owner_id` = OLD.`id`;
END;--> statement-breakpoint
CREATE TRIGGER `distribution_board_components_equipment_identifier_delete`
AFTER DELETE ON `distribution_board_components`
BEGIN
	DELETE FROM `circuit_list_equipment_identifiers`
	WHERE
		`owner_type` = 'distribution_board_component'
		AND `owner_id` = OLD.`id`;
END;
