CREATE TABLE `author_mappings` (
	`id` text PRIMARY KEY NOT NULL,
	`git_email` text,
	`git_username` text,
	`user_id` text,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE UNIQUE INDEX `uniq_mapping_email` ON `author_mappings` (`git_email`);--> statement-breakpoint
CREATE UNIQUE INDEX `uniq_mapping_username` ON `author_mappings` (`git_username`);--> statement-breakpoint
CREATE TABLE `git_commits` (
	`id` text PRIMARY KEY NOT NULL,
	`repo_name` text NOT NULL,
	`sha` text NOT NULL,
	`author_email` text,
	`author_name` text,
	`user_id` text,
	`message` text,
	`additions` integer DEFAULT 0,
	`deletions` integer DEFAULT 0,
	`committed_at` integer NOT NULL,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE UNIQUE INDEX `git_commits_sha_unique` ON `git_commits` (`sha`);--> statement-breakpoint
CREATE UNIQUE INDEX `uniq_commits_sha` ON `git_commits` (`sha`);--> statement-breakpoint
CREATE INDEX `idx_commits_user` ON `git_commits` (`user_id`);--> statement-breakpoint
CREATE INDEX `idx_commits_repo` ON `git_commits` (`repo_name`);--> statement-breakpoint
CREATE INDEX `idx_commits_committed_at` ON `git_commits` (`committed_at`);--> statement-breakpoint
CREATE TABLE `git_prs` (
	`id` text PRIMARY KEY NOT NULL,
	`repo_name` text NOT NULL,
	`external_id` integer NOT NULL,
	`title` text,
	`user_id` text,
	`author_username` text,
	`state` text,
	`additions` integer DEFAULT 0,
	`deletions` integer DEFAULT 0,
	`review_comments` integer DEFAULT 0,
	`created_at` integer,
	`merged_at` integer,
	`merge_time_hours` real,
	`updated_at` integer NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX `idx_prs_user` ON `git_prs` (`user_id`);--> statement-breakpoint
CREATE INDEX `idx_prs_repo` ON `git_prs` (`repo_name`);--> statement-breakpoint
CREATE INDEX `idx_prs_state` ON `git_prs` (`state`);--> statement-breakpoint
CREATE TABLE `key_results` (
	`id` text PRIMARY KEY NOT NULL,
	`objective_id` text NOT NULL,
	`title` text NOT NULL,
	`target_value` real NOT NULL,
	`current_value` real DEFAULT 0,
	`unit` text,
	`weight` real DEFAULT 1,
	`linked_plane_cycle_id` text,
	`linked_plane_module_id` text,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL,
	FOREIGN KEY (`objective_id`) REFERENCES `objectives`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX `idx_kr_objective` ON `key_results` (`objective_id`);--> statement-breakpoint
CREATE TABLE `milestones` (
	`id` text PRIMARY KEY NOT NULL,
	`plane_module_id` text NOT NULL,
	`project_id` text,
	`name` text NOT NULL,
	`status` text,
	`target_date` text,
	`total_issues` integer DEFAULT 0,
	`completed_issues` integer DEFAULT 0,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL,
	FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX `idx_milestone_project` ON `milestones` (`project_id`);--> statement-breakpoint
CREATE TABLE `objectives` (
	`id` text PRIMARY KEY NOT NULL,
	`title` text NOT NULL,
	`owner_id` text,
	`project_id` text,
	`period` text NOT NULL,
	`progress` real DEFAULT 0,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL,
	FOREIGN KEY (`owner_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action,
	FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX `idx_obj_period` ON `objectives` (`period`);--> statement-breakpoint
CREATE INDEX `idx_obj_owner` ON `objectives` (`owner_id`);--> statement-breakpoint
CREATE TABLE `projects` (
	`id` text PRIMARY KEY NOT NULL,
	`plane_project_id` text NOT NULL,
	`name` text NOT NULL,
	`identifier` text,
	`last_synced_at` integer,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `uniq_projects_plane_id` ON `projects` (`plane_project_id`);--> statement-breakpoint
CREATE TABLE `sprint_snapshots` (
	`id` text PRIMARY KEY NOT NULL,
	`project_id` text,
	`plane_cycle_id` text NOT NULL,
	`name` text NOT NULL,
	`start_date` text,
	`end_date` text,
	`total_points` integer DEFAULT 0,
	`completed_points` integer DEFAULT 0,
	`total_issues` integer DEFAULT 0,
	`completed_issues` integer DEFAULT 0,
	`burndown_data` text,
	`status` text,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL,
	FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX `idx_sprint_project` ON `sprint_snapshots` (`project_id`);--> statement-breakpoint
CREATE INDEX `idx_sprint_status` ON `sprint_snapshots` (`status`);--> statement-breakpoint
CREATE TABLE `sync_logs` (
	`id` text PRIMARY KEY NOT NULL,
	`source` text NOT NULL,
	`status` text NOT NULL,
	`message` text,
	`records_processed` integer DEFAULT 0,
	`synced_at` integer NOT NULL
);
--> statement-breakpoint
CREATE TABLE `task_snapshots` (
	`id` text PRIMARY KEY NOT NULL,
	`plane_issue_id` text NOT NULL,
	`project_id` text,
	`sprint_id` text,
	`title` text NOT NULL,
	`status` text,
	`priority` text,
	`assignee_id` text,
	`story_points` integer,
	`created_at` integer,
	`completed_at` integer,
	`due_date` text,
	`labels` text,
	`updated_at` integer NOT NULL,
	FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE no action,
	FOREIGN KEY (`sprint_id`) REFERENCES `sprint_snapshots`(`id`) ON UPDATE no action ON DELETE no action,
	FOREIGN KEY (`assignee_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX `idx_task_project` ON `task_snapshots` (`project_id`);--> statement-breakpoint
CREATE INDEX `idx_task_sprint` ON `task_snapshots` (`sprint_id`);--> statement-breakpoint
CREATE INDEX `idx_task_assignee` ON `task_snapshots` (`assignee_id`);--> statement-breakpoint
CREATE INDEX `idx_task_status` ON `task_snapshots` (`status`);--> statement-breakpoint
CREATE TABLE `users` (
	`id` text PRIMARY KEY NOT NULL,
	`plane_user_id` text,
	`display_name` text NOT NULL,
	`email` text NOT NULL,
	`git_username` text,
	`role` text NOT NULL,
	`password_hash` text NOT NULL,
	`login_attempts` integer DEFAULT 0,
	`locked_until` integer,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `users_email_unique` ON `users` (`email`);--> statement-breakpoint
CREATE UNIQUE INDEX `uniq_users_email` ON `users` (`email`);