CREATE TABLE `company_overview_cache` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`user_id` text NOT NULL,
	`ticker` text NOT NULL,
	`cache_version` integer NOT NULL,
	`source_signature` text NOT NULL,
	`payload` text NOT NULL,
	`created_at` text NOT NULL,
	`updated_at` text NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `company_overview_cache_uidx` ON `company_overview_cache` (`user_id`,`ticker`);--> statement-breakpoint
CREATE INDEX `company_overview_cache_lookup_idx` ON `company_overview_cache` (`user_id`,`ticker`,`updated_at`);--> statement-breakpoint
CREATE TABLE `filing_statement_snapshot` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`filing_id` integer NOT NULL,
	`ticker` text NOT NULL,
	`filing_date` text NOT NULL,
	`filing_type` text NOT NULL,
	`period_end` text,
	`statement_bundle` text,
	`standardized_bundle` text,
	`dimension_bundle` text,
	`parse_status` text NOT NULL,
	`parse_error` text,
	`source` text NOT NULL,
	`created_at` text NOT NULL,
	`updated_at` text NOT NULL,
	FOREIGN KEY (`filing_id`) REFERENCES `filing`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `filing_stmt_filing_uidx` ON `filing_statement_snapshot` (`filing_id`);--> statement-breakpoint
CREATE INDEX `filing_stmt_ticker_date_idx` ON `filing_statement_snapshot` (`ticker`,`filing_date`);--> statement-breakpoint
CREATE INDEX `filing_stmt_date_idx` ON `filing_statement_snapshot` (`filing_date`);--> statement-breakpoint
CREATE INDEX `filing_stmt_status_idx` ON `filing_statement_snapshot` (`parse_status`);--> statement-breakpoint
CREATE TABLE `filing_taxonomy_asset` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`snapshot_id` integer NOT NULL,
	`asset_type` text NOT NULL,
	`name` text NOT NULL,
	`url` text NOT NULL,
	`size_bytes` integer,
	`score` numeric,
	`is_selected` integer DEFAULT false NOT NULL,
	`created_at` text NOT NULL,
	FOREIGN KEY (`snapshot_id`) REFERENCES `filing_taxonomy_snapshot`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `filing_taxonomy_asset_snapshot_idx` ON `filing_taxonomy_asset` (`snapshot_id`);--> statement-breakpoint
CREATE INDEX `filing_taxonomy_asset_type_idx` ON `filing_taxonomy_asset` (`snapshot_id`,`asset_type`);--> statement-breakpoint
CREATE TABLE `filing_taxonomy_concept` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`snapshot_id` integer NOT NULL,
	`concept_key` text NOT NULL,
	`qname` text NOT NULL,
	`namespace_uri` text NOT NULL,
	`local_name` text NOT NULL,
	`label` text,
	`is_extension` integer DEFAULT false NOT NULL,
	`balance` text,
	`period_type` text,
	`data_type` text,
	`statement_kind` text,
	`role_uri` text,
	`authoritative_concept_key` text,
	`mapping_method` text,
	`surface_key` text,
	`detail_parent_surface_key` text,
	`kpi_key` text,
	`residual_flag` integer DEFAULT false NOT NULL,
	`presentation_order` numeric,
	`presentation_depth` integer,
	`parent_concept_key` text,
	`is_abstract` integer DEFAULT false NOT NULL,
	`created_at` text NOT NULL,
	FOREIGN KEY (`snapshot_id`) REFERENCES `filing_taxonomy_snapshot`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `filing_taxonomy_concept_snapshot_idx` ON `filing_taxonomy_concept` (`snapshot_id`);--> statement-breakpoint
CREATE INDEX `filing_taxonomy_concept_statement_idx` ON `filing_taxonomy_concept` (`snapshot_id`,`statement_kind`);--> statement-breakpoint
CREATE UNIQUE INDEX `filing_taxonomy_concept_uidx` ON `filing_taxonomy_concept` (`snapshot_id`,`concept_key`,`role_uri`,`presentation_order`);--> statement-breakpoint
CREATE TABLE `filing_taxonomy_context` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`snapshot_id` integer NOT NULL,
	`context_id` text NOT NULL,
	`entity_identifier` text,
	`entity_scheme` text,
	`period_start` text,
	`period_end` text,
	`period_instant` text,
	`segment_json` text,
	`scenario_json` text,
	`created_at` text NOT NULL,
	FOREIGN KEY (`snapshot_id`) REFERENCES `filing_taxonomy_snapshot`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `filing_taxonomy_context_snapshot_idx` ON `filing_taxonomy_context` (`snapshot_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `filing_taxonomy_context_uidx` ON `filing_taxonomy_context` (`snapshot_id`,`context_id`);--> statement-breakpoint
CREATE TABLE `filing_taxonomy_fact` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`snapshot_id` integer NOT NULL,
	`concept_key` text NOT NULL,
	`qname` text NOT NULL,
	`namespace_uri` text NOT NULL,
	`local_name` text NOT NULL,
	`data_type` text,
	`statement_kind` text,
	`role_uri` text,
	`authoritative_concept_key` text,
	`mapping_method` text,
	`surface_key` text,
	`detail_parent_surface_key` text,
	`kpi_key` text,
	`residual_flag` integer DEFAULT false NOT NULL,
	`context_id` text NOT NULL,
	`unit` text,
	`decimals` text,
	`precision` text,
	`nil` integer DEFAULT false NOT NULL,
	`value_num` numeric NOT NULL,
	`period_start` text,
	`period_end` text,
	`period_instant` text,
	`dimensions` text NOT NULL,
	`is_dimensionless` integer DEFAULT true NOT NULL,
	`source_file` text,
	`created_at` text NOT NULL,
	FOREIGN KEY (`snapshot_id`) REFERENCES `filing_taxonomy_snapshot`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `filing_taxonomy_fact_snapshot_idx` ON `filing_taxonomy_fact` (`snapshot_id`);--> statement-breakpoint
CREATE INDEX `filing_taxonomy_fact_concept_idx` ON `filing_taxonomy_fact` (`snapshot_id`,`concept_key`);--> statement-breakpoint
CREATE INDEX `filing_taxonomy_fact_period_idx` ON `filing_taxonomy_fact` (`snapshot_id`,`period_end`,`period_instant`);--> statement-breakpoint
CREATE INDEX `filing_taxonomy_fact_statement_idx` ON `filing_taxonomy_fact` (`snapshot_id`,`statement_kind`);--> statement-breakpoint
CREATE TABLE `filing_taxonomy_metric_validation` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`snapshot_id` integer NOT NULL,
	`metric_key` text NOT NULL,
	`taxonomy_value` numeric,
	`llm_value` numeric,
	`absolute_diff` numeric,
	`relative_diff` numeric,
	`status` text NOT NULL,
	`evidence_pages` text NOT NULL,
	`pdf_url` text,
	`provider` text,
	`model` text,
	`error` text,
	`created_at` text NOT NULL,
	`updated_at` text NOT NULL,
	FOREIGN KEY (`snapshot_id`) REFERENCES `filing_taxonomy_snapshot`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `filing_taxonomy_metric_validation_snapshot_idx` ON `filing_taxonomy_metric_validation` (`snapshot_id`);--> statement-breakpoint
CREATE INDEX `filing_taxonomy_metric_validation_status_idx` ON `filing_taxonomy_metric_validation` (`snapshot_id`,`status`);--> statement-breakpoint
CREATE UNIQUE INDEX `filing_taxonomy_metric_validation_uidx` ON `filing_taxonomy_metric_validation` (`snapshot_id`,`metric_key`);--> statement-breakpoint
CREATE TABLE `filing_taxonomy_snapshot` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`filing_id` integer NOT NULL,
	`ticker` text NOT NULL,
	`filing_date` text NOT NULL,
	`filing_type` text NOT NULL,
	`parse_status` text NOT NULL,
	`parse_error` text,
	`source` text NOT NULL,
	`parser_engine` text DEFAULT 'fiscal-xbrl' NOT NULL,
	`parser_version` text DEFAULT 'unknown' NOT NULL,
	`taxonomy_regime` text DEFAULT 'unknown' NOT NULL,
	`fiscal_pack` text,
	`periods` text,
	`faithful_rows` text,
	`statement_rows` text,
	`surface_rows` text,
	`detail_rows` text,
	`kpi_rows` text,
	`computed_definitions` text,
	`derived_metrics` text,
	`validation_result` text,
	`normalization_summary` text,
	`facts_count` integer DEFAULT 0 NOT NULL,
	`concepts_count` integer DEFAULT 0 NOT NULL,
	`dimensions_count` integer DEFAULT 0 NOT NULL,
	`created_at` text NOT NULL,
	`updated_at` text NOT NULL,
	FOREIGN KEY (`filing_id`) REFERENCES `filing`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `filing_taxonomy_snapshot_filing_uidx` ON `filing_taxonomy_snapshot` (`filing_id`);--> statement-breakpoint
CREATE INDEX `filing_taxonomy_snapshot_ticker_date_idx` ON `filing_taxonomy_snapshot` (`ticker`,`filing_date`);--> statement-breakpoint
CREATE INDEX `filing_taxonomy_snapshot_status_idx` ON `filing_taxonomy_snapshot` (`parse_status`);--> statement-breakpoint
CREATE TABLE `research_artifact` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`user_id` text NOT NULL,
	`organization_id` text,
	`ticker` text NOT NULL,
	`accession_number` text,
	`kind` text NOT NULL,
	`source` text DEFAULT 'user' NOT NULL,
	`subtype` text,
	`title` text,
	`summary` text,
	`body_markdown` text,
	`search_text` text,
	`visibility_scope` text DEFAULT 'private' NOT NULL,
	`tags` text,
	`metadata` text,
	`file_name` text,
	`mime_type` text,
	`file_size_bytes` integer,
	`storage_path` text,
	`created_at` text NOT NULL,
	`updated_at` text NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`organization_id`) REFERENCES `organization`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE INDEX `research_artifact_ticker_idx` ON `research_artifact` (`user_id`,`ticker`,`updated_at`);--> statement-breakpoint
CREATE INDEX `research_artifact_kind_idx` ON `research_artifact` (`user_id`,`kind`,`updated_at`);--> statement-breakpoint
CREATE INDEX `research_artifact_accession_idx` ON `research_artifact` (`user_id`,`accession_number`);--> statement-breakpoint
CREATE INDEX `research_artifact_source_idx` ON `research_artifact` (`user_id`,`source`,`updated_at`);--> statement-breakpoint
CREATE TABLE `research_journal_entry` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`user_id` text NOT NULL,
	`ticker` text NOT NULL,
	`accession_number` text,
	`entry_type` text NOT NULL,
	`title` text,
	`body_markdown` text NOT NULL,
	`metadata` text,
	`created_at` text NOT NULL,
	`updated_at` text NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `research_journal_ticker_idx` ON `research_journal_entry` (`user_id`,`ticker`,`created_at`);--> statement-breakpoint
CREATE INDEX `research_journal_accession_idx` ON `research_journal_entry` (`user_id`,`accession_number`);--> statement-breakpoint
CREATE TABLE `research_memo` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`user_id` text NOT NULL,
	`organization_id` text,
	`ticker` text NOT NULL,
	`rating` text,
	`conviction` text,
	`time_horizon_months` integer,
	`packet_title` text,
	`packet_subtitle` text,
	`thesis_markdown` text DEFAULT '' NOT NULL,
	`variant_view_markdown` text DEFAULT '' NOT NULL,
	`catalysts_markdown` text DEFAULT '' NOT NULL,
	`risks_markdown` text DEFAULT '' NOT NULL,
	`disconfirming_evidence_markdown` text DEFAULT '' NOT NULL,
	`next_actions_markdown` text DEFAULT '' NOT NULL,
	`created_at` text NOT NULL,
	`updated_at` text NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`organization_id`) REFERENCES `organization`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE UNIQUE INDEX `research_memo_ticker_uidx` ON `research_memo` (`user_id`,`ticker`);--> statement-breakpoint
CREATE INDEX `research_memo_updated_idx` ON `research_memo` (`user_id`,`updated_at`);--> statement-breakpoint
CREATE TABLE `research_memo_evidence` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`memo_id` integer NOT NULL,
	`artifact_id` integer NOT NULL,
	`section` text NOT NULL,
	`annotation` text,
	`sort_order` integer DEFAULT 0 NOT NULL,
	`created_at` text NOT NULL,
	FOREIGN KEY (`memo_id`) REFERENCES `research_memo`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`artifact_id`) REFERENCES `research_artifact`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `research_memo_evidence_memo_idx` ON `research_memo_evidence` (`memo_id`,`section`,`sort_order`);--> statement-breakpoint
CREATE INDEX `research_memo_evidence_artifact_idx` ON `research_memo_evidence` (`artifact_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `research_memo_evidence_unique_uidx` ON `research_memo_evidence` (`memo_id`,`artifact_id`,`section`);--> statement-breakpoint
CREATE TABLE `search_chunk` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`document_id` integer NOT NULL,
	`chunk_index` integer NOT NULL,
	`chunk_text` text NOT NULL,
	`char_count` integer NOT NULL,
	`start_offset` integer NOT NULL,
	`end_offset` integer NOT NULL,
	`heading_path` text,
	`citation_label` text NOT NULL,
	`created_at` text NOT NULL,
	FOREIGN KEY (`document_id`) REFERENCES `search_document`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `search_chunk_document_chunk_uidx` ON `search_chunk` (`document_id`,`chunk_index`);--> statement-breakpoint
CREATE INDEX `search_chunk_document_idx` ON `search_chunk` (`document_id`);--> statement-breakpoint
CREATE TABLE `search_document` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`source_kind` text NOT NULL,
	`source_ref` text NOT NULL,
	`scope` text NOT NULL,
	`user_id` text,
	`ticker` text,
	`accession_number` text,
	`title` text,
	`content_text` text NOT NULL,
	`content_hash` text NOT NULL,
	`metadata` text,
	`index_status` text NOT NULL,
	`indexed_at` text,
	`last_error` text,
	`created_at` text NOT NULL,
	`updated_at` text NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `search_document_source_uidx` ON `search_document` (`scope`,`ifnull("user_id"`,` '')`,`source_kind`,`source_ref`);--> statement-breakpoint
CREATE INDEX `search_document_scope_idx` ON `search_document` (`scope`,`source_kind`,`ticker`,`updated_at`);--> statement-breakpoint
CREATE INDEX `search_document_accession_idx` ON `search_document` (`accession_number`,`source_kind`);--> statement-breakpoint
CREATE TABLE `task_stage_event` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`task_id` text NOT NULL,
	`user_id` text NOT NULL,
	`stage` text NOT NULL,
	`stage_detail` text,
	`stage_context` text,
	`status` text NOT NULL,
	`created_at` text NOT NULL,
	FOREIGN KEY (`task_id`) REFERENCES `task_run`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`user_id`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `task_stage_event_task_created_idx` ON `task_stage_event` (`task_id`,`created_at`);--> statement-breakpoint
CREATE INDEX `task_stage_event_user_created_idx` ON `task_stage_event` (`user_id`,`created_at`);--> statement-breakpoint
ALTER TABLE `holding` ADD `company_name` text;--> statement-breakpoint
ALTER TABLE `task_run` ADD `stage` text NOT NULL;--> statement-breakpoint
ALTER TABLE `task_run` ADD `stage_detail` text;--> statement-breakpoint
ALTER TABLE `task_run` ADD `stage_context` text;--> statement-breakpoint
ALTER TABLE `task_run` ADD `resource_key` text;--> statement-breakpoint
ALTER TABLE `task_run` ADD `notification_read_at` text;--> statement-breakpoint
ALTER TABLE `task_run` ADD `notification_silenced_at` text;--> statement-breakpoint
CREATE INDEX `task_user_updated_idx` ON `task_run` (`user_id`,`updated_at`);--> statement-breakpoint
CREATE INDEX `task_user_resource_status_idx` ON `task_run` (`user_id`,`task_type`,`resource_key`,`status`,`created_at`);--> statement-breakpoint
ALTER TABLE `watchlist_item` ADD `category` text;--> statement-breakpoint
ALTER TABLE `watchlist_item` ADD `tags` text;--> statement-breakpoint
ALTER TABLE `watchlist_item` ADD `status` text DEFAULT 'backlog' NOT NULL;--> statement-breakpoint
ALTER TABLE `watchlist_item` ADD `priority` text DEFAULT 'medium' NOT NULL;--> statement-breakpoint
ALTER TABLE `watchlist_item` ADD `updated_at` text NOT NULL;--> statement-breakpoint
ALTER TABLE `watchlist_item` ADD `last_reviewed_at` text;--> statement-breakpoint
CREATE INDEX `watchlist_user_updated_idx` ON `watchlist_item` (`user_id`,`updated_at`);
