CREATE TABLE `scorecard_evaluations` (
	`id` text PRIMARY KEY NOT NULL,
	`created_at` text NOT NULL,
	`name` text NOT NULL,
	`description` text,
	`entities` text NOT NULL,
	`levels` text NOT NULL
);
--> statement-breakpoint
CREATE INDEX `idx_scorecard_evaluations_name_created_at` ON `scorecard_evaluations` (`name`,`created_at`);--> statement-breakpoint
CREATE TABLE `scorecard_result_level_states` (
	`result_level_id` text NOT NULL,
	`result_state_id` text NOT NULL,
	PRIMARY KEY(`result_level_id`, `result_state_id`)
);
--> statement-breakpoint
CREATE TABLE `scorecard_result_levels` (
	`id` text PRIMARY KEY NOT NULL,
	`result_id` text NOT NULL,
	`level_name` text NOT NULL,
	`rules_number` integer NOT NULL,
	`passed_rules` integer NOT NULL,
	`failed_rules` integer NOT NULL,
	`warnings_number` integer NOT NULL,
	`weight` integer NOT NULL,
	`total_weight` integer NOT NULL,
	`score` integer NOT NULL
);
--> statement-breakpoint
CREATE INDEX `idx_scorecard_result_levels_result_id` ON `scorecard_result_levels` (`result_id`);--> statement-breakpoint
CREATE INDEX `idx_scorecard_result_levels_result_level` ON `scorecard_result_levels` (`result_id`,`level_name`);--> statement-breakpoint
CREATE TABLE `scorecard_result_states` (
	`id` text PRIMARY KEY NOT NULL,
	`rule_name` text NOT NULL,
	`rule_details` text NOT NULL,
	`state` text NOT NULL,
	`severity` text,
	`score` integer NOT NULL,
	`first_evaluation_id` text NOT NULL,
	`usage_count` integer DEFAULT 1 NOT NULL,
	`hash` text NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `idx_scorecard_result_states_hash` ON `scorecard_result_states` (`hash`);--> statement-breakpoint
CREATE INDEX `idx_scorecard_result_states_rule_name` ON `scorecard_result_states` (`rule_name`);--> statement-breakpoint
CREATE INDEX `idx_scorecard_result_states_first_evaluation_id` ON `scorecard_result_states` (`first_evaluation_id`);--> statement-breakpoint
CREATE TABLE `scorecard_results` (
	`id` text PRIMARY KEY NOT NULL,
	`evaluation_id` text NOT NULL,
	`entity_id` text NOT NULL,
	`entity_key` text NOT NULL,
	`entity_type` text NOT NULL,
	`entity_version` text NOT NULL,
	`entity_revision` text,
	`final_level` text,
	`final_score` integer NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `idx_scorecard_results_evaluation_entity_unique` ON `scorecard_results` (`evaluation_id`,`entity_key`,`entity_id`,`entity_version`);--> statement-breakpoint
CREATE INDEX `idx_scorecard_results_evaluation_id` ON `scorecard_results` (`evaluation_id`);--> statement-breakpoint
CREATE INDEX `idx_scorecard_results_entity_id` ON `scorecard_results` (`entity_id`);