-- Drop all old scorecard tables
DROP TABLE IF EXISTS `scorecard_result_level_states`;--> statement-breakpoint
DROP TABLE IF EXISTS `scorecard_result_levels`;--> statement-breakpoint
DROP TABLE IF EXISTS `scorecard_result_states`;--> statement-breakpoint
DROP TABLE IF EXISTS `scorecard_results`;--> statement-breakpoint
DROP TABLE IF EXISTS `scorecard_evaluations`;--> statement-breakpoint

-- Create scorecards_results table (renamed from scorecard_results)
CREATE TABLE `scorecards_results` (
	`id` text PRIMARY KEY NOT NULL,
	`entity_id` text NOT NULL,
	`config_id` text NOT NULL,
	`created_at` integer NOT NULL,
	`evaluated_at` integer NOT NULL,
	`final_score` integer NOT NULL,
	`final_level` text,
	`evaluated_rules` text NOT NULL,
	`rules_config_hash` text NOT NULL,
	`entity_version` text NOT NULL,
	`entity_revision` text
);--> statement-breakpoint

-- Create indexes for scorecards_results
CREATE UNIQUE INDEX `idx_results_lookup` ON `scorecards_results` (`entity_id`, `config_id`, `created_at` DESC);--> statement-breakpoint
CREATE INDEX `idx_results_config_time` ON `scorecards_results` (`config_id`, `created_at` DESC);--> statement-breakpoint
CREATE INDEX `idx_results_entity` ON `scorecards_results` (`entity_id`, `created_at` DESC);--> statement-breakpoint
CREATE INDEX `idx_results_config_entity_time` ON `scorecards_results` (`config_id`, `entity_id`, `created_at` DESC);--> statement-breakpoint

-- Create scorecards_level_results table (renamed from scorecard_result_levels)
CREATE TABLE `scorecards_level_results` (
	`id` text PRIMARY KEY NOT NULL,
	`result_id` text NOT NULL,
	`level_name` text NOT NULL,
	`score` integer NOT NULL,
	`passed_rules` integer NOT NULL,
	`failed_rules` integer NOT NULL,
	`total_rules` integer NOT NULL,
	`rule_summary` text
);--> statement-breakpoint

-- Create indexes for scorecards_level_results
CREATE INDEX `idx_level_results_result` ON `scorecards_level_results` (`result_id`);--> statement-breakpoint
CREATE INDEX `idx_level_results_level` ON `scorecards_level_results` (`level_name`);--> statement-breakpoint
CREATE INDEX `idx_level_results_result_level` ON `scorecards_level_results` (`result_id`, `level_name`);--> statement-breakpoint

-- Create scorecards_rule_results table (renamed from scorecard_result_states)
CREATE TABLE `scorecards_rule_results` (
	`id` text PRIMARY KEY NOT NULL,
	`rule_name` text NOT NULL,
	`problems_hash` text NOT NULL,
	`state` text NOT NULL,
	`problems` text,
	`first_seen_at` integer NOT NULL
);--> statement-breakpoint

-- Create indexes for scorecards_rule_results
CREATE UNIQUE INDEX `idx_rules_name_hash` ON `scorecards_rule_results` (`rule_name`, `problems_hash`);--> statement-breakpoint
CREATE INDEX `idx_rules_state` ON `scorecards_rule_results` (`state`);--> statement-breakpoint

-- Create scorecards_levels_rules_results table (renamed from scorecard_result_level_states)
CREATE TABLE `scorecards_levels_rules_results` (
	`id` text PRIMARY KEY NOT NULL,
	`level_score_id` text NOT NULL,
	`rule_id` text NOT NULL
);--> statement-breakpoint

-- Create indexes for scorecards_levels_rules_results
CREATE UNIQUE INDEX `idx_rule_problems_level_score_rule` ON `scorecards_levels_rules_results` (`level_score_id`, `rule_id`);--> statement-breakpoint
CREATE INDEX `idx_rule_problems_level_score` ON `scorecards_levels_rules_results` (`level_score_id`);--> statement-breakpoint
CREATE INDEX `idx_rule_problems_rule` ON `scorecards_levels_rules_results` (`rule_id`);--> statement-breakpoint

-- Create scorecards_evaluation_runs table (renamed from scorecard_evaluations)
CREATE TABLE `scorecards_evaluation_runs` (
	`id` text PRIMARY KEY NOT NULL,
	`config_id` text NOT NULL,
	`evaluated_at` integer NOT NULL,
	`entities_checked` integer NOT NULL,
	`entities_changed` integer NOT NULL,
	`duration_ms` integer,
	`status` text NOT NULL,
	`error_message` text,
	`entity_id` text
);--> statement-breakpoint

-- Create indexes for scorecards_evaluation_runs
CREATE INDEX `idx_runs_config_time` ON `scorecards_evaluation_runs` (`config_id`, `evaluated_at` DESC);

