CREATE TABLE `documents` (
	`id` text PRIMARY KEY NOT NULL,
	`repository_id` text NOT NULL,
	`version_id` text,
	`file_path` text NOT NULL,
	`title` text,
	`language` text,
	`token_count` integer DEFAULT 0,
	`checksum` text NOT NULL,
	`indexed_at` integer NOT NULL,
	FOREIGN KEY (`repository_id`) REFERENCES `repositories`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`version_id`) REFERENCES `repository_versions`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `indexing_jobs` (
	`id` text PRIMARY KEY NOT NULL,
	`repository_id` text NOT NULL,
	`version_id` text,
	`status` text DEFAULT 'queued' NOT NULL,
	`progress` integer DEFAULT 0,
	`total_files` integer DEFAULT 0,
	`processed_files` integer DEFAULT 0,
	`error` text,
	`started_at` integer,
	`completed_at` integer,
	`created_at` integer NOT NULL,
	FOREIGN KEY (`repository_id`) REFERENCES `repositories`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `repositories` (
	`id` text PRIMARY KEY NOT NULL,
	`title` text NOT NULL,
	`description` text,
	`source` text NOT NULL,
	`source_url` text NOT NULL,
	`branch` text DEFAULT 'main',
	`state` text DEFAULT 'pending' NOT NULL,
	`total_snippets` integer DEFAULT 0,
	`total_tokens` integer DEFAULT 0,
	`trust_score` real DEFAULT 0,
	`benchmark_score` real DEFAULT 0,
	`stars` integer,
	`github_token` text,
	`last_indexed_at` integer,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE `repository_configs` (
	`repository_id` text PRIMARY KEY NOT NULL,
	`project_title` text,
	`description` text,
	`folders` text,
	`exclude_folders` text,
	`exclude_files` text,
	`rules` text,
	`previous_versions` text,
	`updated_at` integer NOT NULL,
	FOREIGN KEY (`repository_id`) REFERENCES `repositories`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `repository_versions` (
	`id` text PRIMARY KEY NOT NULL,
	`repository_id` text NOT NULL,
	`tag` text NOT NULL,
	`title` text,
	`state` text DEFAULT 'pending' NOT NULL,
	`total_snippets` integer DEFAULT 0,
	`indexed_at` integer,
	`created_at` integer NOT NULL,
	FOREIGN KEY (`repository_id`) REFERENCES `repositories`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `settings` (
	`key` text PRIMARY KEY NOT NULL,
	`value` text,
	`updated_at` integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE `snippet_embeddings` (
	`snippet_id` text PRIMARY KEY NOT NULL,
	`model` text NOT NULL,
	`dimensions` integer NOT NULL,
	`embedding` blob NOT NULL,
	`created_at` integer NOT NULL,
	FOREIGN KEY (`snippet_id`) REFERENCES `snippets`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `snippets` (
	`id` text PRIMARY KEY NOT NULL,
	`document_id` text NOT NULL,
	`repository_id` text NOT NULL,
	`version_id` text,
	`type` text NOT NULL,
	`title` text,
	`content` text NOT NULL,
	`language` text,
	`breadcrumb` text,
	`token_count` integer DEFAULT 0,
	`created_at` integer NOT NULL,
	FOREIGN KEY (`document_id`) REFERENCES `documents`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`repository_id`) REFERENCES `repositories`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`version_id`) REFERENCES `repository_versions`(`id`) ON UPDATE no action ON DELETE cascade
);
