CREATE TABLE IF NOT EXISTS `la_ai_kb_document` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `domain` varchar(32) NOT NULL DEFAULT '' COMMENT 'article/post/lottery',
  `subtype` varchar(32) NOT NULL DEFAULT '' COMMENT 'article/post/draw_result/ai_history',
  `source_id` bigint unsigned NOT NULL DEFAULT 0,
  `title` varchar(255) NOT NULL DEFAULT '',
  `summary` text NULL,
  `canonical_text` longtext NULL,
  `keywords` text NULL,
  `metadata_json` json NULL,
  `source_created_at` int unsigned NOT NULL DEFAULT 0,
  `source_updated_at` int unsigned NOT NULL DEFAULT 0,
  `content_hash` varchar(64) NOT NULL DEFAULT '',
  `is_active` tinyint unsigned NOT NULL DEFAULT 1,
  `create_time` int unsigned NOT NULL DEFAULT 0,
  `update_time` int unsigned NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_domain_subtype_source` (`domain`, `subtype`, `source_id`),
  KEY `idx_domain_active` (`domain`, `is_active`),
  KEY `idx_source_updated` (`source_updated_at`),
  FULLTEXT KEY `ft_doc_text` (`title`, `summary`, `canonical_text`, `keywords`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI知识库文档主表';

CREATE TABLE IF NOT EXISTS `la_ai_kb_chunk` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `document_id` bigint unsigned NOT NULL DEFAULT 0,
  `collection_name` varchar(32) NOT NULL DEFAULT 'global',
  `chunk_index` int unsigned NOT NULL DEFAULT 0,
  `chunk_text` mediumtext NULL,
  `embedding_json` mediumtext NULL,
  `embedding_model` varchar(100) NOT NULL DEFAULT '',
  `embedding_dim` int unsigned NOT NULL DEFAULT 0,
  `vector_norm` decimal(20,8) NOT NULL DEFAULT 0.00000000,
  `keyword_text` text NULL,
  `create_time` int unsigned NOT NULL DEFAULT 0,
  `update_time` int unsigned NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_document_chunk` (`document_id`, `chunk_index`),
  KEY `idx_document` (`document_id`),
  KEY `idx_collection` (`collection_name`),
  FULLTEXT KEY `ft_chunk_text` (`chunk_text`, `keyword_text`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI知识库切片表';

CREATE TABLE IF NOT EXISTS `la_ai_kb_sync_job` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `domain` varchar(32) NOT NULL DEFAULT '',
  `subtype` varchar(32) NOT NULL DEFAULT '',
  `source_id` bigint unsigned NOT NULL DEFAULT 0,
  `action` varchar(16) NOT NULL DEFAULT 'upsert',
  `priority` int unsigned NOT NULL DEFAULT 100,
  `status` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '0待处理1处理中2成功3失败',
  `retry_count` int unsigned NOT NULL DEFAULT 0,
  `error_msg` varchar(500) NOT NULL DEFAULT '',
  `scheduled_at` int unsigned NOT NULL DEFAULT 0,
  `processed_at` int unsigned NOT NULL DEFAULT 0,
  `create_time` int unsigned NOT NULL DEFAULT 0,
  `update_time` int unsigned NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  KEY `idx_status_schedule` (`status`, `scheduled_at`),
  KEY `idx_domain_source` (`domain`, `subtype`, `source_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI知识库同步任务表';

CREATE TABLE IF NOT EXISTS `la_ai_kb_query_log` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int unsigned NOT NULL DEFAULT 0,
  `scene` varchar(50) NOT NULL DEFAULT '',
  `ref_id` bigint unsigned NOT NULL DEFAULT 0,
  `query_text` text NULL,
  `filters_json` json NULL,
  `hit_docs_json` json NULL,
  `tokens_used` int unsigned NOT NULL DEFAULT 0,
  `cost_ms` int unsigned NOT NULL DEFAULT 0,
  `status` tinyint unsigned NOT NULL DEFAULT 1 COMMENT '1成功2失败',
  `create_time` int unsigned NOT NULL DEFAULT 0,
  `update_time` int unsigned NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`),
  KEY `idx_scene_ref` (`scene`, `ref_id`),
  KEY `idx_user_time` (`user_id`, `create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI知识库查询日志';
