-- InsightFlow Phase 3 - Database Schema
-- 支持知识生长与多文件融合

-- 项目表
CREATE TABLE IF NOT EXISTS projects (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    description TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 文件/转录表
CREATE TABLE IF NOT EXISTS transcripts (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    filename TEXT,
    full_text TEXT,
    type TEXT DEFAULT 'audio',  -- 'audio' 或 'document'
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- 全局实体表（跨文件共享）
CREATE TABLE IF NOT EXISTS entities (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    name TEXT NOT NULL,
    canonical_name TEXT,  -- 规范名称（用于对齐）
    type TEXT,
    definition TEXT,
    aliases TEXT,  -- JSON 数组：["别名1", "别名2"]
    embedding TEXT,  -- JSON 数组：实体名称+定义的 embedding
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- 实体提及表（文件中的具体位置）
CREATE TABLE IF NOT EXISTS entity_mentions (
    id TEXT PRIMARY KEY,
    entity_id TEXT NOT NULL,
    transcript_id TEXT NOT NULL,
    start_pos INTEGER,
    end_pos INTEGER,
    text_snippet TEXT,
    confidence REAL DEFAULT 1.0,
    FOREIGN KEY (entity_id) REFERENCES entities(id),
    FOREIGN KEY (transcript_id) REFERENCES transcripts(id)
);

-- 实体关系表
CREATE TABLE IF NOT EXISTS entity_relations (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    source_entity_id TEXT NOT NULL,
    target_entity_id TEXT NOT NULL,
    relation_type TEXT,  -- "belongs_to", "works_with", "depends_on" 等
    evidence TEXT,  -- 关系来源文本
    transcript_id TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (project_id) REFERENCES projects(id),
    FOREIGN KEY (source_entity_id) REFERENCES entities(id),
    FOREIGN KEY (target_entity_id) REFERENCES entities(id)
);

-- 术语表（项目级热词，用于 ASR 优化）
CREATE TABLE IF NOT EXISTS glossary (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    term TEXT NOT NULL,
    pronunciation TEXT,  -- 发音提示，如 "K8s" -> "Kubernetes"
    frequency INTEGER DEFAULT 1,
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- Phase 5: 属性模板表
CREATE TABLE IF NOT EXISTS attribute_templates (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    name TEXT NOT NULL,
    type TEXT NOT NULL,  -- text/number/date/select/multiselect/boolean
    description TEXT,
    options TEXT,  -- JSON 数组，用于 select/multiselect 类型
    is_required INTEGER DEFAULT 0,
    default_value TEXT,
    sort_order INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- Phase 5: 实体属性值表
CREATE TABLE IF NOT EXISTS entity_attributes (
    id TEXT PRIMARY KEY,
    entity_id TEXT NOT NULL,
    template_id TEXT,
    name TEXT NOT NULL,
    type TEXT NOT NULL,  -- text/number/date/select/multiselect
    value TEXT,  -- 存储实际值
    options TEXT,  -- JSON 数组，用于 select/multiselect
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE,
    FOREIGN KEY (template_id) REFERENCES attribute_templates(id) ON DELETE SET NULL,
    UNIQUE(entity_id, name)
);

-- Phase 5: 属性变更历史表
CREATE TABLE IF NOT EXISTS attribute_history (
    id TEXT PRIMARY KEY,
    entity_id TEXT NOT NULL,
    template_id TEXT,
    attribute_name TEXT NOT NULL,
    old_value TEXT,
    new_value TEXT,
    changed_by TEXT,  -- 用户ID或系统
    changed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    change_reason TEXT,
    FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE,
    FOREIGN KEY (template_id) REFERENCES attribute_templates(id) ON DELETE CASCADE
);

-- 创建索引以提高查询性能
CREATE INDEX IF NOT EXISTS idx_entities_project ON entities(project_id);
CREATE INDEX IF NOT EXISTS idx_entities_name ON entities(name);
CREATE INDEX IF NOT EXISTS idx_transcripts_project ON transcripts(project_id);
CREATE INDEX IF NOT EXISTS idx_mentions_entity ON entity_mentions(entity_id);
CREATE INDEX IF NOT EXISTS idx_mentions_transcript ON entity_mentions(transcript_id);
CREATE INDEX IF NOT EXISTS idx_relations_project ON entity_relations(project_id);
CREATE INDEX IF NOT EXISTS idx_glossary_project ON glossary(project_id);

-- Phase 5: 属性相关索引
CREATE INDEX IF NOT EXISTS idx_attr_templates_project ON attribute_templates(project_id);
CREATE INDEX IF NOT EXISTS idx_entity_attributes_entity ON entity_attributes(entity_id);
CREATE INDEX IF NOT EXISTS idx_entity_attributes_template ON entity_attributes(template_id);
CREATE INDEX IF NOT EXISTS idx_attr_history_entity ON attribute_history(entity_id);

-- Phase 7: 工作流相关表

-- 工作流配置表
CREATE TABLE IF NOT EXISTS workflows (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    description TEXT,
    workflow_type TEXT NOT NULL,  -- auto_analyze, auto_align, auto_relation, scheduled_report, custom
    project_id TEXT NOT NULL,
    status TEXT DEFAULT 'active',  -- active, paused, error, completed
    schedule TEXT,  -- cron expression or interval minutes
    schedule_type TEXT DEFAULT 'manual',  -- manual, cron, interval
    config TEXT,  -- JSON: workflow specific configuration
    webhook_ids TEXT,  -- JSON array of webhook config IDs
    is_active BOOLEAN DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_run_at TIMESTAMP,
    next_run_at TIMESTAMP,
    run_count INTEGER DEFAULT 0,
    success_count INTEGER DEFAULT 0,
    fail_count INTEGER DEFAULT 0,
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- 工作流任务表
CREATE TABLE IF NOT EXISTS workflow_tasks (
    id TEXT PRIMARY KEY,
    workflow_id TEXT NOT NULL,
    name TEXT NOT NULL,
    task_type TEXT NOT NULL,  -- analyze, align, discover_relations, notify, custom
    config TEXT,  -- JSON: task specific configuration
    task_order INTEGER DEFAULT 0,
    depends_on TEXT,  -- JSON array of task IDs
    timeout_seconds INTEGER DEFAULT 300,
    retry_count INTEGER DEFAULT 3,
    retry_delay INTEGER DEFAULT 5,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (workflow_id) REFERENCES workflows(id) ON DELETE CASCADE
);

-- Webhook 配置表
CREATE TABLE IF NOT EXISTS webhook_configs (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    webhook_type TEXT NOT NULL,  -- feishu, dingtalk, slack, custom
    url TEXT NOT NULL,
    secret TEXT,  -- for signature verification
    headers TEXT,  -- JSON: custom headers
    template TEXT,  -- message template
    is_active BOOLEAN DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_used_at TIMESTAMP,
    success_count INTEGER DEFAULT 0,
    fail_count INTEGER DEFAULT 0
);

-- 工作流执行日志表
CREATE TABLE IF NOT EXISTS workflow_logs (
    id TEXT PRIMARY KEY,
    workflow_id TEXT NOT NULL,
    task_id TEXT,  -- NULL if workflow-level log
    status TEXT DEFAULT 'pending',  -- pending, running, success, failed, cancelled
    start_time TIMESTAMP,
    end_time TIMESTAMP,
    duration_ms INTEGER,
    input_data TEXT,  -- JSON: input parameters
    output_data TEXT,  -- JSON: execution results
    error_message TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (workflow_id) REFERENCES workflows(id) ON DELETE CASCADE,
    FOREIGN KEY (task_id) REFERENCES workflow_tasks(id) ON DELETE SET NULL
);

-- Phase 7: 工作流相关索引
CREATE INDEX IF NOT EXISTS idx_workflows_project ON workflows(project_id);
CREATE INDEX IF NOT EXISTS idx_workflows_status ON workflows(status);
CREATE INDEX IF NOT EXISTS idx_workflows_type ON workflows(workflow_type);
CREATE INDEX IF NOT EXISTS idx_workflow_tasks_workflow ON workflow_tasks(workflow_id);
CREATE INDEX IF NOT EXISTS idx_workflow_logs_workflow ON workflow_logs(workflow_id);
CREATE INDEX IF NOT EXISTS idx_workflow_logs_task ON workflow_logs(task_id);
CREATE INDEX IF NOT EXISTS idx_workflow_logs_status ON workflow_logs(status);
CREATE INDEX IF NOT EXISTS idx_workflow_logs_created ON workflow_logs(created_at);

-- Phase 7: 多模态支持相关表

-- 视频表
CREATE TABLE IF NOT EXISTS videos (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    filename TEXT NOT NULL,
    duration REAL,  -- 视频时长（秒）
    fps REAL,  -- 帧率
    resolution TEXT,  -- JSON: {"width": int, "height": int}
    audio_transcript_id TEXT,  -- 关联的音频转录ID
    full_ocr_text TEXT,  -- 所有帧OCR文本合并
    extracted_entities TEXT,  -- JSON: 提取的实体列表
    extracted_relations TEXT,  -- JSON: 提取的关系列表
    status TEXT DEFAULT 'processing',  -- processing, completed, failed
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (project_id) REFERENCES projects(id),
    FOREIGN KEY (audio_transcript_id) REFERENCES transcripts(id)
);

-- 视频关键帧表
CREATE TABLE IF NOT EXISTS video_frames (
    id TEXT PRIMARY KEY,
    video_id TEXT NOT NULL,
    frame_number INTEGER,
    timestamp REAL,  -- 时间戳（秒）
    image_data BLOB,  -- 帧图片数据（可选，可存储在OSS）
    image_url TEXT,  -- 图片URL（如果存储在OSS）
    ocr_text TEXT,  -- OCR识别文本
    extracted_entities TEXT,  -- JSON: 该帧提取的实体
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (video_id) REFERENCES videos(id) ON DELETE CASCADE
);

-- 图片表
CREATE TABLE IF NOT EXISTS images (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    filename TEXT NOT NULL,
    image_data BLOB,  -- 图片数据（可选）
    image_url TEXT,  -- 图片URL
    ocr_text TEXT,  -- OCR识别文本
    description TEXT,  -- 图片描述（LLM生成）
    extracted_entities TEXT,  -- JSON: 提取的实体列表
    extracted_relations TEXT,  -- JSON: 提取的关系列表
    status TEXT DEFAULT 'processing',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- 多模态实体提及表
CREATE TABLE IF NOT EXISTS multimodal_mentions (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    entity_id TEXT NOT NULL,
    modality TEXT NOT NULL,  -- audio, video, image, document
    source_id TEXT NOT NULL,  -- transcript_id, video_id, image_id
    source_type TEXT NOT NULL,  -- 来源类型
    position TEXT,  -- JSON: 位置信息
    text_snippet TEXT,  -- 提及的文本片段
    confidence REAL DEFAULT 1.0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (project_id) REFERENCES projects(id),
    FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE
);

-- 多模态实体关联表
CREATE TABLE IF NOT EXISTS multimodal_entity_links (
    id TEXT PRIMARY KEY,
    entity_id TEXT NOT NULL,
    linked_entity_id TEXT NOT NULL,  -- 关联的实体ID
    link_type TEXT NOT NULL,  -- same_as, related_to, part_of
    confidence REAL DEFAULT 1.0,
    evidence TEXT,  -- 关联证据
    modalities TEXT,  -- JSON: 涉及的模态列表
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (entity_id) REFERENCES entities(id) ON DELETE CASCADE,
    FOREIGN KEY (linked_entity_id) REFERENCES entities(id) ON DELETE CASCADE
);

-- 多模态相关索引
CREATE INDEX IF NOT EXISTS idx_videos_project ON videos(project_id);
CREATE INDEX IF NOT EXISTS idx_videos_status ON videos(status);
CREATE INDEX IF NOT EXISTS idx_video_frames_video ON video_frames(video_id);
CREATE INDEX IF NOT EXISTS idx_images_project ON images(project_id);
CREATE INDEX IF NOT EXISTS idx_images_status ON images(status);
CREATE INDEX IF NOT EXISTS idx_multimodal_mentions_project ON multimodal_mentions(project_id);
CREATE INDEX IF NOT EXISTS idx_multimodal_mentions_entity ON multimodal_mentions(entity_id);
CREATE INDEX IF NOT EXISTS idx_multimodal_mentions_modality ON multimodal_mentions(modality);
CREATE INDEX IF NOT EXISTS idx_multimodal_mentions_source ON multimodal_mentions(source_id);
CREATE INDEX IF NOT EXISTS idx_multimodal_links_entity ON multimodal_entity_links(entity_id);
CREATE INDEX IF NOT EXISTS idx_multimodal_links_linked ON multimodal_entity_links(linked_entity_id);

-- Phase 7 Task 7: 插件与集成相关表

-- 插件配置表
CREATE TABLE IF NOT EXISTS plugins (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    plugin_type TEXT NOT NULL,  -- chrome_extension, feishu_bot, dingtalk_bot, zapier, make, webdav, custom
    project_id TEXT,
    status TEXT DEFAULT 'active',  -- active, inactive, error, pending
    config TEXT,  -- JSON: plugin specific configuration
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_used_at TIMESTAMP,
    use_count INTEGER DEFAULT 0,
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- 插件详细配置表
CREATE TABLE IF NOT EXISTS plugin_configs (
    id TEXT PRIMARY KEY,
    plugin_id TEXT NOT NULL,
    config_key TEXT NOT NULL,
    config_value TEXT,
    is_encrypted BOOLEAN DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (plugin_id) REFERENCES plugins(id) ON DELETE CASCADE,
    UNIQUE(plugin_id, config_key)
);

-- 机器人会话表
CREATE TABLE IF NOT EXISTS bot_sessions (
    id TEXT PRIMARY KEY,
    bot_type TEXT NOT NULL,  -- feishu, dingtalk
    session_id TEXT NOT NULL,  -- 群ID或会话ID
    session_name TEXT NOT NULL,
    project_id TEXT,
    webhook_url TEXT,
    secret TEXT,  -- 签名密钥
    is_active BOOLEAN DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_message_at TIMESTAMP,
    message_count INTEGER DEFAULT 0,
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- Webhook 端点表（Zapier/Make集成）
CREATE TABLE IF NOT EXISTS webhook_endpoints (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    endpoint_type TEXT NOT NULL,  -- zapier, make, custom
    endpoint_url TEXT NOT NULL,
    project_id TEXT,
    auth_type TEXT DEFAULT 'none',  -- none, api_key, oauth, custom
    auth_config TEXT,  -- JSON: authentication configuration
    trigger_events TEXT,  -- JSON array: events that trigger this webhook
    is_active BOOLEAN DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_triggered_at TIMESTAMP,
    trigger_count INTEGER DEFAULT 0,
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- WebDAV 同步配置表
CREATE TABLE IF NOT EXISTS webdav_syncs (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    project_id TEXT NOT NULL,
    server_url TEXT NOT NULL,
    username TEXT NOT NULL,
    password TEXT NOT NULL,  -- 建议加密存储
    remote_path TEXT DEFAULT '/insightflow',
    sync_mode TEXT DEFAULT 'bidirectional',  -- bidirectional, upload_only, download_only
    sync_interval INTEGER DEFAULT 3600,  -- 秒
    last_sync_at TIMESTAMP,
    last_sync_status TEXT DEFAULT 'pending',  -- pending, success, failed
    last_sync_error TEXT,
    is_active BOOLEAN DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    sync_count INTEGER DEFAULT 0,
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- Chrome 扩展令牌表
CREATE TABLE IF NOT EXISTS chrome_extension_tokens (
    id TEXT PRIMARY KEY,
    token_hash TEXT NOT NULL UNIQUE,  -- SHA256 hash of the token
    user_id TEXT,
    project_id TEXT,
    name TEXT,
    permissions TEXT,  -- JSON array: read, write, delete
    expires_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_used_at TIMESTAMP,
    use_count INTEGER DEFAULT 0,
    is_revoked BOOLEAN DEFAULT 0,
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- 插件相关索引
CREATE INDEX IF NOT EXISTS idx_plugins_project ON plugins(project_id);
CREATE INDEX IF NOT EXISTS idx_plugins_type ON plugins(plugin_type);
CREATE INDEX IF NOT EXISTS idx_plugins_status ON plugins(status);
CREATE INDEX IF NOT EXISTS idx_plugin_configs_plugin ON plugin_configs(plugin_id);
CREATE INDEX IF NOT EXISTS idx_bot_sessions_project ON bot_sessions(project_id);
CREATE INDEX IF NOT EXISTS idx_bot_sessions_type ON bot_sessions(bot_type);
CREATE INDEX IF NOT EXISTS idx_webhook_endpoints_project ON webhook_endpoints(project_id);
CREATE INDEX IF NOT EXISTS idx_webhook_endpoints_type ON webhook_endpoints(endpoint_type);
CREATE INDEX IF NOT EXISTS idx_webdav_syncs_project ON webdav_syncs(project_id);
CREATE INDEX IF NOT EXISTS idx_chrome_tokens_project ON chrome_extension_tokens(project_id);
CREATE INDEX IF NOT EXISTS idx_chrome_tokens_hash ON chrome_extension_tokens(token_hash);

-- ============================================
-- Phase 7 Task 6: 高级搜索与发现
-- ============================================

-- 搜索索引表
CREATE TABLE IF NOT EXISTS search_indexes (
    id TEXT PRIMARY KEY,
    content_id TEXT NOT NULL,
    content_type TEXT NOT NULL,  -- transcript, entity, relation
    project_id TEXT NOT NULL,
    tokens TEXT,  -- JSON 数组
    token_positions TEXT,  -- JSON 对象
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(content_id, content_type)
);

-- 搜索词频统计表
CREATE TABLE IF NOT EXISTS search_term_freq (
    term TEXT NOT NULL,
    content_id TEXT NOT NULL,
    content_type TEXT NOT NULL,
    project_id TEXT NOT NULL,
    frequency INTEGER DEFAULT 1,
    positions TEXT,  -- JSON 数组
    PRIMARY KEY (term, content_id, content_type)
);

-- 文本 Embedding 表
CREATE TABLE IF NOT EXISTS embeddings (
    id TEXT PRIMARY KEY,
    content_id TEXT NOT NULL,
    content_type TEXT NOT NULL,
    project_id TEXT NOT NULL,
    embedding TEXT,  -- JSON 数组
    model_name TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(content_id, content_type)
);

-- 搜索相关索引
CREATE INDEX IF NOT EXISTS idx_search_content ON search_indexes(content_id, content_type);
CREATE INDEX IF NOT EXISTS idx_search_project ON search_indexes(project_id);
CREATE INDEX IF NOT EXISTS idx_term_freq_term ON search_term_freq(term);
CREATE INDEX IF NOT EXISTS idx_term_freq_project ON search_term_freq(project_id);
CREATE INDEX IF NOT EXISTS idx_embedding_content ON embeddings(content_id, content_type);
CREATE INDEX IF NOT EXISTS idx_embedding_project ON embeddings(project_id);

-- ============================================
-- Phase 7 Task 8: 性能优化与扩展
-- ============================================

-- 缓存统计表
CREATE TABLE IF NOT EXISTS cache_stats (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    total_requests INTEGER DEFAULT 0,
    hits INTEGER DEFAULT 0,
    misses INTEGER DEFAULT 0,
    hit_rate REAL DEFAULT 0.0,
    memory_usage INTEGER DEFAULT 0
);

-- 任务队列表
CREATE TABLE IF NOT EXISTS task_queue (
    id TEXT PRIMARY KEY,
    task_type TEXT NOT NULL,
    status TEXT DEFAULT 'pending',  -- pending, running, success, failed, retrying, cancelled
    payload TEXT,  -- JSON
    result TEXT,  -- JSON
    error_message TEXT,
    retry_count INTEGER DEFAULT 0,
    max_retries INTEGER DEFAULT 3,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    started_at TIMESTAMP,
    completed_at TIMESTAMP
);

-- 性能指标表
CREATE TABLE IF NOT EXISTS performance_metrics (
    id TEXT PRIMARY KEY,
    metric_type TEXT NOT NULL,  -- api_response, db_query, cache_operation
    endpoint TEXT,
    duration_ms REAL,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    metadata TEXT  -- JSON
);

-- 性能相关索引
CREATE INDEX IF NOT EXISTS idx_cache_stats_time ON cache_stats(timestamp);
CREATE INDEX IF NOT EXISTS idx_task_status ON task_queue(status);
CREATE INDEX IF NOT EXISTS idx_task_type ON task_queue(task_type);
CREATE INDEX IF NOT EXISTS idx_task_created ON task_queue(created_at);
CREATE INDEX IF NOT EXISTS idx_metrics_type ON performance_metrics(metric_type);
CREATE INDEX IF NOT EXISTS idx_metrics_endpoint ON performance_metrics(endpoint);
CREATE INDEX IF NOT EXISTS idx_metrics_time ON performance_metrics(timestamp);

-- ============================================
-- Phase 7: 插件与集成相关表
-- ============================================

-- 插件表
CREATE TABLE IF NOT EXISTS plugins (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    plugin_type TEXT NOT NULL,  -- chrome_extension, feishu_bot, dingtalk_bot, slack_bot, webhook, webdav, custom
    project_id TEXT,
    status TEXT DEFAULT 'active',  -- active, inactive, error, pending
    config TEXT,  -- JSON: 插件配置
    api_key TEXT UNIQUE,  -- 用于认证的 API Key
    api_secret TEXT,  -- 用于签名验证的 Secret
    webhook_url TEXT,  -- 机器人 Webhook URL
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_used_at TIMESTAMP,
    use_count INTEGER DEFAULT 0,
    success_count INTEGER DEFAULT 0,
    fail_count INTEGER DEFAULT 0,
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- 机器人会话表
CREATE TABLE IF NOT EXISTS bot_sessions (
    id TEXT PRIMARY KEY,
    plugin_id TEXT NOT NULL,
    platform TEXT NOT NULL,  -- feishu, dingtalk, slack, wechat
    session_id TEXT NOT NULL,  -- 平台特定的会话ID
    user_id TEXT,
    user_name TEXT,
    project_id TEXT,  -- 关联的项目ID
    context TEXT,  -- JSON: 会话上下文
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_message_at TIMESTAMP,
    message_count INTEGER DEFAULT 0,
    FOREIGN KEY (plugin_id) REFERENCES plugins(id) ON DELETE CASCADE,
    FOREIGN KEY (project_id) REFERENCES projects(id),
    UNIQUE(plugin_id, session_id)
);

-- Webhook 端点表（用于 Zapier/Make 集成）
CREATE TABLE IF NOT EXISTS webhook_endpoints (
    id TEXT PRIMARY KEY,
    plugin_id TEXT NOT NULL,
    name TEXT NOT NULL,
    endpoint_path TEXT NOT NULL UNIQUE,  -- 如 /webhook/zapier/abc123
    endpoint_type TEXT NOT NULL,  -- zapier, make, custom
    secret TEXT,  -- 用于签名验证
    allowed_events TEXT,  -- JSON: 允许的事件列表
    target_project_id TEXT,  -- 数据导入的目标项目
    is_active BOOLEAN DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_triggered_at TIMESTAMP,
    trigger_count INTEGER DEFAULT 0,
    FOREIGN KEY (plugin_id) REFERENCES plugins(id) ON DELETE CASCADE,
    FOREIGN KEY (target_project_id) REFERENCES projects(id)
);

-- WebDAV 同步配置表
CREATE TABLE IF NOT EXISTS webdav_syncs (
    id TEXT PRIMARY KEY,
    plugin_id TEXT NOT NULL,
    name TEXT NOT NULL,
    server_url TEXT NOT NULL,
    username TEXT NOT NULL,
    password TEXT NOT NULL,  -- 建议加密存储
    remote_path TEXT DEFAULT '/',
    local_path TEXT DEFAULT './sync',
    sync_direction TEXT DEFAULT 'bidirectional',  -- upload, download, bidirectional
    sync_mode TEXT DEFAULT 'manual',  -- manual, realtime, scheduled
    sync_schedule TEXT,  -- cron expression
    file_patterns TEXT,  -- JSON: 文件匹配模式列表
    auto_analyze BOOLEAN DEFAULT 1,  -- 同步后自动分析
    last_sync_at TIMESTAMP,
    last_sync_status TEXT,
    is_active BOOLEAN DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    sync_count INTEGER DEFAULT 0,
    FOREIGN KEY (plugin_id) REFERENCES plugins(id) ON DELETE CASCADE
);

-- 插件活动日志表
CREATE TABLE IF NOT EXISTS plugin_activity_logs (
    id TEXT PRIMARY KEY,
    plugin_id TEXT NOT NULL,
    activity_type TEXT NOT NULL,  -- message, webhook, sync, error
    source TEXT NOT NULL,  -- 来源标识
    details TEXT,  -- JSON: 详细信息
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (plugin_id) REFERENCES plugins(id) ON DELETE CASCADE
);

-- 插件相关索引
CREATE INDEX IF NOT EXISTS idx_plugins_project ON plugins(project_id);
CREATE INDEX IF NOT EXISTS idx_plugins_type ON plugins(plugin_type);
CREATE INDEX IF NOT EXISTS idx_plugins_api_key ON plugins(api_key);
CREATE INDEX IF NOT EXISTS idx_bot_sessions_plugin ON bot_sessions(plugin_id);
CREATE INDEX IF NOT EXISTS idx_bot_sessions_project ON bot_sessions(project_id);
CREATE INDEX IF NOT EXISTS idx_webhook_endpoints_plugin ON webhook_endpoints(plugin_id);
CREATE INDEX IF NOT EXISTS idx_webdav_syncs_plugin ON webdav_syncs(plugin_id);
CREATE INDEX IF NOT EXISTS idx_plugin_logs_plugin ON plugin_activity_logs(plugin_id);
CREATE INDEX IF NOT EXISTS idx_plugin_logs_type ON plugin_activity_logs(activity_type);
CREATE INDEX IF NOT EXISTS idx_plugin_logs_created ON plugin_activity_logs(created_at);

-- ============================================
-- Phase 7 Task 3: 数据安全与合规
-- ============================================

-- 审计日志表
CREATE TABLE IF NOT EXISTS audit_logs (
    id TEXT PRIMARY KEY,
    action_type TEXT NOT NULL,  -- create, read, update, delete, login, export, etc.
    user_id TEXT,
    user_ip TEXT,
    user_agent TEXT,
    resource_type TEXT,  -- project, entity, transcript, api_key, etc.
    resource_id TEXT,
    action_details TEXT,  -- JSON: 详细操作信息
    before_value TEXT,  -- 变更前的值
    after_value TEXT,  -- 变更后的值
    success INTEGER DEFAULT 1,  -- 0 = 失败, 1 = 成功
    error_message TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 加密配置表
CREATE TABLE IF NOT EXISTS encryption_configs (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    is_enabled INTEGER DEFAULT 0,
    encryption_type TEXT DEFAULT 'aes-256-gcm',  -- aes-256-gcm, chacha20-poly1305
    key_derivation TEXT DEFAULT 'pbkdf2',  -- pbkdf2, argon2
    master_key_hash TEXT,  -- 主密钥哈希（用于验证）
    salt TEXT,  -- 密钥派生盐值
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- 脱敏规则表
CREATE TABLE IF NOT EXISTS masking_rules (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    name TEXT NOT NULL,
    rule_type TEXT NOT NULL,  -- phone, email, id_card, bank_card, name, address, custom
    pattern TEXT NOT NULL,  -- 正则表达式
    replacement TEXT NOT NULL,  -- 替换模板
    is_active INTEGER DEFAULT 1,
    priority INTEGER DEFAULT 0,
    description TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- 数据访问策略表
CREATE TABLE IF NOT EXISTS data_access_policies (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    name TEXT NOT NULL,
    description TEXT,
    allowed_users TEXT,  -- JSON array: 允许访问的用户ID列表
    allowed_roles TEXT,  -- JSON array: 允许的角色列表
    allowed_ips TEXT,  -- JSON array: 允许的IP模式列表
    time_restrictions TEXT,  -- JSON: {"start_time": "09:00", "end_time": "18:00", "days_of_week": [0,1,2,3,4]}
    max_access_count INTEGER,  -- 最大访问次数限制
    require_approval INTEGER DEFAULT 0,  -- 是否需要审批
    is_active INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- 访问请求表（用于需要审批的访问）
CREATE TABLE IF NOT EXISTS access_requests (
    id TEXT PRIMARY KEY,
    policy_id TEXT NOT NULL,
    user_id TEXT NOT NULL,
    request_reason TEXT,
    status TEXT DEFAULT 'pending',  -- pending, approved, rejected, expired
    approved_by TEXT,
    approved_at TIMESTAMP,
    expires_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (policy_id) REFERENCES data_access_policies(id)
);

-- 数据安全相关索引
CREATE INDEX IF NOT EXISTS idx_audit_logs_user ON audit_logs(user_id);
CREATE INDEX IF NOT EXISTS idx_audit_logs_resource ON audit_logs(resource_type, resource_id);
CREATE INDEX IF NOT EXISTS idx_audit_logs_action ON audit_logs(action_type);
CREATE INDEX IF NOT EXISTS idx_audit_logs_created ON audit_logs(created_at);
CREATE INDEX IF NOT EXISTS idx_encryption_project ON encryption_configs(project_id);
CREATE INDEX IF NOT EXISTS idx_masking_project ON masking_rules(project_id);
CREATE INDEX IF NOT EXISTS idx_access_policy_project ON data_access_policies(project_id);
CREATE INDEX IF NOT EXISTS idx_access_requests_policy ON access_requests(policy_id);
CREATE INDEX IF NOT EXISTS idx_access_requests_user ON access_requests(user_id);

-- ============================================
-- Phase 7 Task 4: 协作与共享
-- ============================================

-- 项目分享表
CREATE TABLE IF NOT EXISTS project_shares (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    token TEXT NOT NULL UNIQUE,           -- 分享令牌
    permission TEXT DEFAULT 'read_only',  -- 权限级别: read_only, comment, edit, admin
    created_by TEXT NOT NULL,             -- 创建者
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    expires_at TIMESTAMP,                 -- 过期时间
    max_uses INTEGER,                     -- 最大使用次数
    use_count INTEGER DEFAULT 0,          -- 已使用次数
    password_hash TEXT,                   -- 密码保护(哈希)
    is_active BOOLEAN DEFAULT 1,          -- 是否激活
    allow_download BOOLEAN DEFAULT 0,     -- 允许下载
    allow_export BOOLEAN DEFAULT 0,       -- 允许导出
    FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
);

-- 评论表
CREATE TABLE IF NOT EXISTS comments (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    target_type TEXT NOT NULL,            -- 目标类型: entity, relation, transcript, project
    target_id TEXT NOT NULL,              -- 目标ID
    parent_id TEXT,                       -- 父评论ID(支持回复)
    author TEXT NOT NULL,                 -- 作者ID
    author_name TEXT,                     -- 作者显示名
    content TEXT NOT NULL,                -- 评论内容
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    resolved BOOLEAN DEFAULT 0,           -- 是否已解决
    resolved_by TEXT,                     -- 解决者
    resolved_at TIMESTAMP,                -- 解决时间
    mentions TEXT,                        -- JSON数组: 提及的用户
    attachments TEXT,                     -- JSON数组: 附件
    FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,
    FOREIGN KEY (parent_id) REFERENCES comments(id) ON DELETE CASCADE
);

-- 变更历史表
CREATE TABLE IF NOT EXISTS change_history (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    change_type TEXT NOT NULL,            -- 变更类型: create, update, delete, merge, split
    entity_type TEXT NOT NULL,            -- 实体类型: entity, relation, transcript, project
    entity_id TEXT NOT NULL,              -- 实体ID
    entity_name TEXT,                     -- 实体名称(用于显示)
    changed_by TEXT NOT NULL,             -- 变更者ID
    changed_by_name TEXT,                 -- 变更者显示名
    changed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    old_value TEXT,                       -- JSON: 旧值
    new_value TEXT,                       -- JSON: 新值
    description TEXT,                     -- 变更描述
    session_id TEXT,                      -- 会话ID(批量变更关联)
    reverted BOOLEAN DEFAULT 0,           -- 是否已回滚
    reverted_at TIMESTAMP,                -- 回滚时间
    reverted_by TEXT,                     -- 回滚者
    FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
);

-- 团队成员表
CREATE TABLE IF NOT EXISTS team_members (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    user_id TEXT NOT NULL,                -- 用户ID
    user_name TEXT,                       -- 用户名
    user_email TEXT,                      -- 用户邮箱
    role TEXT DEFAULT 'viewer',           -- 角色: owner, admin, editor, viewer, commenter
    joined_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    invited_by TEXT,                      -- 邀请者
    last_active_at TIMESTAMP,             -- 最后活跃时间
    permissions TEXT,                     -- JSON数组: 具体权限列表
    FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE,
    UNIQUE(project_id, user_id)           -- 每个项目每个用户只能有一条记录
);

-- 协作与共享相关索引
CREATE INDEX IF NOT EXISTS idx_shares_project ON project_shares(project_id);
CREATE INDEX IF NOT EXISTS idx_shares_token ON project_shares(token);
CREATE INDEX IF NOT EXISTS idx_comments_project ON comments(project_id);
CREATE INDEX IF NOT EXISTS idx_comments_target ON comments(target_type, target_id);
CREATE INDEX IF NOT EXISTS idx_comments_parent ON comments(parent_id);
CREATE INDEX IF NOT EXISTS idx_change_history_project ON change_history(project_id);
CREATE INDEX IF NOT EXISTS idx_change_history_entity ON change_history(entity_type, entity_id);
CREATE INDEX IF NOT EXISTS idx_change_history_session ON change_history(session_id);
CREATE INDEX IF NOT EXISTS idx_team_members_project ON team_members(project_id);
CREATE INDEX IF NOT EXISTS idx_team_members_user ON team_members(user_id);

-- ============================================
-- Phase 7 Task 6: 高级搜索与发现
-- ============================================

-- 搜索索引表
CREATE TABLE IF NOT EXISTS search_indexes (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    content_type TEXT NOT NULL,  -- transcript, entity, relation
    content_id TEXT NOT NULL,
    content_text TEXT NOT NULL,
    tokens TEXT,  -- JSON array of tokens
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (project_id) REFERENCES projects(id),
    UNIQUE(project_id, content_type, content_id)
);

-- 文本 Embedding 表
CREATE TABLE IF NOT EXISTS embeddings (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL,
    content_type TEXT NOT NULL,  -- transcript, entity
    content_id TEXT NOT NULL,
    text TEXT NOT NULL,
    embedding TEXT NOT NULL,  -- JSON array of floats
    model TEXT NOT NULL,  -- 使用的模型名称
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (project_id) REFERENCES projects(id),
    UNIQUE(project_id, content_type, content_id)
);

-- 全文搜索虚拟表 (FTS5)
CREATE VIRTUAL TABLE IF NOT EXISTS fts_transcripts USING fts5(
    content_text,
    content_id UNINDEXED,
    project_id UNINDEXED,
    content_type UNINDEXED
);

-- 搜索相关索引
CREATE INDEX IF NOT EXISTS idx_search_indexes_project ON search_indexes(project_id);
CREATE INDEX IF NOT EXISTS idx_search_indexes_type ON search_indexes(content_type);
CREATE INDEX IF NOT EXISTS idx_search_indexes_content ON search_indexes(content_id);
CREATE INDEX IF NOT EXISTS idx_embeddings_project ON embeddings(project_id);
CREATE INDEX IF NOT EXISTS idx_embeddings_type ON embeddings(content_type);
CREATE INDEX IF NOT EXISTS idx_embeddings_content ON embeddings(content_id);
CREATE INDEX IF NOT EXISTS idx_embeddings_model ON embeddings(model);

-- ============================================
-- Phase 7 Task 8: 性能优化与扩展
-- ============================================

-- 缓存统计表
CREATE TABLE IF NOT EXISTS cache_stats (
    id TEXT PRIMARY KEY,
    stat_date DATE NOT NULL,
    cache_type TEXT NOT NULL,  -- redis, memory
    total_keys INTEGER DEFAULT 0,
    memory_usage INTEGER DEFAULT 0,  -- bytes
    hit_count INTEGER DEFAULT 0,
    miss_count INTEGER DEFAULT 0,
    evicted_count INTEGER DEFAULT 0,
    expired_count INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    UNIQUE(stat_date, cache_type)
);

-- 任务队列表
CREATE TABLE IF NOT EXISTS task_queue (
    id TEXT PRIMARY KEY,
    task_type TEXT NOT NULL,  -- audio_analysis, report_generation, entity_extraction
    status TEXT NOT NULL,  -- pending, running, success, failure, retry, revoked
    project_id TEXT NOT NULL,
    params TEXT NOT NULL,  -- JSON
    result TEXT,  -- JSON
    error TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    started_at TIMESTAMP,
    completed_at TIMESTAMP,
    retry_count INTEGER DEFAULT 0,
    priority INTEGER DEFAULT 5,  -- 0=high, 5=normal, 10=low
    worker_id TEXT,  -- 执行任务的 worker
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- 性能指标表
CREATE TABLE IF NOT EXISTS performance_metrics (
    id TEXT PRIMARY KEY,
    metric_type TEXT NOT NULL,  -- api_response, db_query, cache_operation
    endpoint TEXT NOT NULL,
    duration_ms REAL NOT NULL,
    status_code INTEGER,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    metadata TEXT  -- JSON: 额外信息
);

-- 数据库分片映射表
CREATE TABLE IF NOT EXISTS shard_mappings (
    id TEXT PRIMARY KEY,
    project_id TEXT NOT NULL UNIQUE,
    shard_id TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    migrated_at TIMESTAMP,
    FOREIGN KEY (project_id) REFERENCES projects(id)
);

-- 性能相关索引
CREATE INDEX IF NOT EXISTS idx_cache_stats_date ON cache_stats(stat_date);
CREATE INDEX IF NOT EXISTS idx_task_queue_project ON task_queue(project_id);
CREATE INDEX IF NOT EXISTS idx_task_queue_status ON task_queue(status);
CREATE INDEX IF NOT EXISTS idx_task_queue_type ON task_queue(task_type);
CREATE INDEX IF NOT EXISTS idx_task_queue_created ON task_queue(created_at);
CREATE INDEX IF NOT EXISTS idx_metrics_type ON performance_metrics(metric_type);
CREATE INDEX IF NOT EXISTS idx_metrics_endpoint ON performance_metrics(endpoint);
CREATE INDEX IF NOT EXISTS idx_metrics_timestamp ON performance_metrics(timestamp);
CREATE INDEX IF NOT EXISTS idx_shard_mappings_project ON shard_mappings(project_id);
CREATE INDEX IF NOT EXISTS idx_shard_mappings_shard ON shard_mappings(shard_id);

-- ============================================
-- Phase 8 Task 1: 多租户 SaaS 架构
-- ============================================

-- 租户主表
CREATE TABLE IF NOT EXISTS tenants (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    slug TEXT UNIQUE NOT NULL,
    description TEXT,
    tier TEXT DEFAULT 'free',
    status TEXT DEFAULT 'pending',
    owner_id TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    expires_at TIMESTAMP,
    settings TEXT DEFAULT '{}',
    resource_limits TEXT DEFAULT '{}',
    metadata TEXT DEFAULT '{}'
);

-- 租户域名表
CREATE TABLE IF NOT EXISTS tenant_domains (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    domain TEXT UNIQUE NOT NULL,
    status TEXT DEFAULT 'pending',
    verification_token TEXT NOT NULL,
    verification_method TEXT DEFAULT 'dns',
    verified_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    is_primary INTEGER DEFAULT 0,
    ssl_enabled INTEGER DEFAULT 0,
    ssl_expires_at TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 租户品牌配置表
CREATE TABLE IF NOT EXISTS tenant_branding (
    id TEXT PRIMARY KEY,
    tenant_id TEXT UNIQUE NOT NULL,
    logo_url TEXT,
    favicon_url TEXT,
    primary_color TEXT,
    secondary_color TEXT,
    custom_css TEXT,
    custom_js TEXT,
    login_page_bg TEXT,
    email_template TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 租户成员表
CREATE TABLE IF NOT EXISTS tenant_members (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    user_id TEXT,  -- NULL for pending invitations
    email TEXT NOT NULL,
    role TEXT DEFAULT 'member',
    permissions TEXT DEFAULT '[]',
    invited_by TEXT,
    invited_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    joined_at TIMESTAMP,
    last_active_at TIMESTAMP,
    status TEXT DEFAULT 'pending',
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 租户权限定义表
CREATE TABLE IF NOT EXISTS tenant_permissions (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    code TEXT NOT NULL,
    description TEXT,
    resource_type TEXT NOT NULL,
    actions TEXT NOT NULL,
    conditions TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    UNIQUE(tenant_id, code)
);

-- 租户资源使用统计表
CREATE TABLE IF NOT EXISTS tenant_usage (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    date DATE NOT NULL,
    storage_bytes INTEGER DEFAULT 0,
    transcription_seconds INTEGER DEFAULT 0,
    api_calls INTEGER DEFAULT 0,
    projects_count INTEGER DEFAULT 0,
    entities_count INTEGER DEFAULT 0,
    members_count INTEGER DEFAULT 0,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    UNIQUE(tenant_id, date)
);

-- 租户相关索引
CREATE INDEX IF NOT EXISTS idx_tenants_slug ON tenants(slug);
CREATE INDEX IF NOT EXISTS idx_tenants_owner ON tenants(owner_id);
CREATE INDEX IF NOT EXISTS idx_tenants_status ON tenants(status);
CREATE INDEX IF NOT EXISTS idx_domains_tenant ON tenant_domains(tenant_id);
CREATE INDEX IF NOT EXISTS idx_domains_domain ON tenant_domains(domain);
CREATE INDEX IF NOT EXISTS idx_domains_status ON tenant_domains(status);
CREATE INDEX IF NOT EXISTS idx_members_tenant ON tenant_members(tenant_id);
CREATE INDEX IF NOT EXISTS idx_members_user ON tenant_members(user_id);
CREATE INDEX IF NOT EXISTS idx_usage_tenant ON tenant_usage(tenant_id);
CREATE INDEX IF NOT EXISTS idx_usage_date ON tenant_usage(date);

-- ============================================
-- Phase 8 Task 2: 订阅与计费系统
-- ============================================

-- 订阅计划表
CREATE TABLE IF NOT EXISTS subscription_plans (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    tier TEXT UNIQUE NOT NULL,  -- free/pro/enterprise
    description TEXT,
    price_monthly REAL DEFAULT 0,
    price_yearly REAL DEFAULT 0,
    currency TEXT DEFAULT 'CNY',
    features TEXT DEFAULT '[]',  -- JSON array
    limits TEXT DEFAULT '{}',  -- JSON object
    is_active INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    metadata TEXT DEFAULT '{}'
);

-- 订阅表
CREATE TABLE IF NOT EXISTS subscriptions (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    plan_id TEXT NOT NULL,
    status TEXT DEFAULT 'pending',  -- active/cancelled/expired/past_due/trial/pending
    current_period_start TIMESTAMP,
    current_period_end TIMESTAMP,
    cancel_at_period_end INTEGER DEFAULT 0,
    canceled_at TIMESTAMP,
    trial_start TIMESTAMP,
    trial_end TIMESTAMP,
    payment_provider TEXT,  -- stripe/alipay/wechat/bank_transfer
    provider_subscription_id TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    metadata TEXT DEFAULT '{}',
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    FOREIGN KEY (plan_id) REFERENCES subscription_plans(id)
);

-- 用量记录表
CREATE TABLE IF NOT EXISTS usage_records (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    resource_type TEXT NOT NULL,  -- transcription/storage/api_call/export
    quantity REAL DEFAULT 0,
    unit TEXT NOT NULL,  -- minutes/mb/count/page
    recorded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    cost REAL DEFAULT 0,
    description TEXT,
    metadata TEXT DEFAULT '{}',
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 支付记录表
CREATE TABLE IF NOT EXISTS payments (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    subscription_id TEXT,
    invoice_id TEXT,
    amount REAL NOT NULL,
    currency TEXT DEFAULT 'CNY',
    provider TEXT NOT NULL,  -- stripe/alipay/wechat/bank_transfer
    provider_payment_id TEXT,
    status TEXT DEFAULT 'pending',  -- pending/processing/completed/failed/refunded/partial_refunded
    payment_method TEXT,
    payment_details TEXT DEFAULT '{}',  -- JSON
    paid_at TIMESTAMP,
    failed_at TIMESTAMP,
    failure_reason TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    FOREIGN KEY (subscription_id) REFERENCES subscriptions(id) ON DELETE SET NULL,
    FOREIGN KEY (invoice_id) REFERENCES invoices(id) ON DELETE SET NULL
);

-- 发票表
CREATE TABLE IF NOT EXISTS invoices (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    subscription_id TEXT,
    invoice_number TEXT UNIQUE NOT NULL,
    status TEXT DEFAULT 'draft',  -- draft/issued/paid/overdue/void/credit_note
    amount_due REAL DEFAULT 0,
    amount_paid REAL DEFAULT 0,
    currency TEXT DEFAULT 'CNY',
    period_start TIMESTAMP,
    period_end TIMESTAMP,
    description TEXT,
    line_items TEXT DEFAULT '[]',  -- JSON array
    due_date TIMESTAMP,
    paid_at TIMESTAMP,
    voided_at TIMESTAMP,
    void_reason TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    FOREIGN KEY (subscription_id) REFERENCES subscriptions(id) ON DELETE SET NULL
);

-- 退款表
CREATE TABLE IF NOT EXISTS refunds (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    payment_id TEXT NOT NULL,
    invoice_id TEXT,
    amount REAL NOT NULL,
    currency TEXT DEFAULT 'CNY',
    reason TEXT,
    status TEXT DEFAULT 'pending',  -- pending/approved/rejected/completed/failed
    requested_by TEXT NOT NULL,
    requested_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    approved_by TEXT,
    approved_at TIMESTAMP,
    completed_at TIMESTAMP,
    provider_refund_id TEXT,
    metadata TEXT DEFAULT '{}',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    FOREIGN KEY (payment_id) REFERENCES payments(id) ON DELETE CASCADE,
    FOREIGN KEY (invoice_id) REFERENCES invoices(id) ON DELETE SET NULL
);

-- 账单历史表
CREATE TABLE IF NOT EXISTS billing_history (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    type TEXT NOT NULL,  -- subscription/usage/payment/refund
    amount REAL NOT NULL,
    currency TEXT DEFAULT 'CNY',
    description TEXT,
    reference_id TEXT,  -- 关联的订阅/支付/退款ID
    balance_after REAL DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    metadata TEXT DEFAULT '{}',
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 订阅相关索引
CREATE INDEX IF NOT EXISTS idx_subscriptions_tenant ON subscriptions(tenant_id);
CREATE INDEX IF NOT EXISTS idx_subscriptions_status ON subscriptions(status);
CREATE INDEX IF NOT EXISTS idx_subscriptions_plan ON subscriptions(plan_id);
CREATE INDEX IF NOT EXISTS idx_usage_records_tenant ON usage_records(tenant_id);
CREATE INDEX IF NOT EXISTS idx_usage_records_type ON usage_records(resource_type);
CREATE INDEX IF NOT EXISTS idx_usage_records_recorded ON usage_records(recorded_at);
CREATE INDEX IF NOT EXISTS idx_payments_tenant ON payments(tenant_id);
CREATE INDEX IF NOT EXISTS idx_payments_status ON payments(status);
CREATE INDEX IF NOT EXISTS idx_payments_provider ON payments(provider);
CREATE INDEX IF NOT EXISTS idx_invoices_tenant ON invoices(tenant_id);
CREATE INDEX IF NOT EXISTS idx_invoices_status ON invoices(status);
CREATE INDEX IF NOT EXISTS idx_invoices_number ON invoices(invoice_number);
CREATE INDEX IF NOT EXISTS idx_refunds_tenant ON refunds(tenant_id);
CREATE INDEX IF NOT EXISTS idx_refunds_status ON refunds(status);
CREATE INDEX IF NOT EXISTS idx_refunds_payment ON refunds(payment_id);
CREATE INDEX IF NOT EXISTS idx_billing_history_tenant ON billing_history(tenant_id);
CREATE INDEX IF NOT EXISTS idx_billing_history_created ON billing_history(created_at);
CREATE INDEX IF NOT EXISTS idx_billing_history_type ON billing_history(type);

-- ============================================
-- Phase 8 Task 3: 企业级功能
-- ============================================

-- SSO 配置表
CREATE TABLE IF NOT EXISTS sso_configs (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    provider TEXT NOT NULL,  -- wechat_work/dingtalk/feishu/okta/azure_ad/google/custom_saml
    status TEXT DEFAULT 'disabled',  -- disabled/pending/active/error
    entity_id TEXT,
    sso_url TEXT,
    slo_url TEXT,
    certificate TEXT,  -- X.509 证书
    metadata_url TEXT,
    metadata_xml TEXT,
    client_id TEXT,
    client_secret TEXT,
    authorization_url TEXT,
    token_url TEXT,
    userinfo_url TEXT,
    scopes TEXT DEFAULT '["openid", "email", "profile"]',
    attribute_mapping TEXT DEFAULT '{}',  -- JSON: 属性映射
    auto_provision INTEGER DEFAULT 1,  -- 自动创建用户
    default_role TEXT DEFAULT 'member',
    domain_restriction TEXT DEFAULT '[]',  -- JSON: 允许的邮箱域名
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_tested_at TIMESTAMP,
    last_error TEXT,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- SAML 认证请求表
CREATE TABLE IF NOT EXISTS saml_auth_requests (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    sso_config_id TEXT NOT NULL,
    request_id TEXT NOT NULL UNIQUE,
    relay_state TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    expires_at TIMESTAMP NOT NULL,
    used INTEGER DEFAULT 0,
    used_at TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    FOREIGN KEY (sso_config_id) REFERENCES sso_configs(id) ON DELETE CASCADE
);

-- SAML 认证响应表
CREATE TABLE IF NOT EXISTS saml_auth_responses (
    id TEXT PRIMARY KEY,
    request_id TEXT NOT NULL,
    tenant_id TEXT NOT NULL,
    user_id TEXT,
    email TEXT,
    name TEXT,
    attributes TEXT DEFAULT '{}',  -- JSON: SAML 属性
    session_index TEXT,
    processed INTEGER DEFAULT 0,
    processed_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (request_id) REFERENCES saml_auth_requests(request_id) ON DELETE CASCADE,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- SCIM 配置表
CREATE TABLE IF NOT EXISTS scim_configs (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    provider TEXT NOT NULL,
    status TEXT DEFAULT 'disabled',
    scim_base_url TEXT,
    scim_token TEXT,
    sync_interval_minutes INTEGER DEFAULT 60,
    last_sync_at TIMESTAMP,
    last_sync_status TEXT,
    last_sync_error TEXT,
    last_sync_users_count INTEGER DEFAULT 0,
    attribute_mapping TEXT DEFAULT '{}',
    sync_rules TEXT DEFAULT '{}',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- SCIM 用户表
CREATE TABLE IF NOT EXISTS scim_users (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    external_id TEXT NOT NULL,
    user_name TEXT NOT NULL,
    email TEXT NOT NULL,
    display_name TEXT,
    given_name TEXT,
    family_name TEXT,
    active INTEGER DEFAULT 1,
    groups TEXT DEFAULT '[]',
    raw_data TEXT DEFAULT '{}',
    synced_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    UNIQUE(tenant_id, external_id)
);

-- 审计日志导出表
CREATE TABLE IF NOT EXISTS audit_log_exports (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    export_format TEXT NOT NULL,  -- json/csv/pdf/xlsx
    start_date TIMESTAMP NOT NULL,
    end_date TIMESTAMP NOT NULL,
    filters TEXT DEFAULT '{}',
    compliance_standard TEXT,  -- soc2/iso27001/gdpr/hipaa/pci_dss
    status TEXT DEFAULT 'pending',  -- pending/processing/completed/failed
    file_path TEXT,
    file_size INTEGER,
    record_count INTEGER,
    checksum TEXT,
    downloaded_by TEXT,
    downloaded_at TIMESTAMP,
    expires_at TIMESTAMP,
    created_by TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    completed_at TIMESTAMP,
    error_message TEXT,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 数据保留策略表
CREATE TABLE IF NOT EXISTS data_retention_policies (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    description TEXT,
    resource_type TEXT NOT NULL,  -- project/transcript/entity/audit_log/user_data
    retention_days INTEGER NOT NULL,
    action TEXT NOT NULL,  -- archive/delete/anonymize
    conditions TEXT DEFAULT '{}',
    auto_execute INTEGER DEFAULT 0,
    execute_at TEXT,  -- cron 表达式
    notify_before_days INTEGER DEFAULT 7,
    archive_location TEXT,
    archive_encryption INTEGER DEFAULT 1,
    is_active INTEGER DEFAULT 1,
    last_executed_at TIMESTAMP,
    last_execution_result TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 数据保留任务表
CREATE TABLE IF NOT EXISTS data_retention_jobs (
    id TEXT PRIMARY KEY,
    policy_id TEXT NOT NULL,
    tenant_id TEXT NOT NULL,
    status TEXT DEFAULT 'pending',  -- pending/running/completed/failed
    started_at TIMESTAMP,
    completed_at TIMESTAMP,
    affected_records INTEGER DEFAULT 0,
    archived_records INTEGER DEFAULT 0,
    deleted_records INTEGER DEFAULT 0,
    error_count INTEGER DEFAULT 0,
    details TEXT DEFAULT '{}',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (policy_id) REFERENCES data_retention_policies(id) ON DELETE CASCADE,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 企业级功能相关索引
CREATE INDEX IF NOT EXISTS idx_sso_tenant ON sso_configs(tenant_id);
CREATE INDEX IF NOT EXISTS idx_sso_provider ON sso_configs(provider);
CREATE INDEX IF NOT EXISTS idx_saml_requests_config ON saml_auth_requests(sso_config_id);
CREATE INDEX IF NOT EXISTS idx_saml_requests_expires ON saml_auth_requests(expires_at);
CREATE INDEX IF NOT EXISTS idx_saml_responses_request ON saml_auth_responses(request_id);
CREATE INDEX IF NOT EXISTS idx_scim_config_tenant ON scim_configs(tenant_id);
CREATE INDEX IF NOT EXISTS idx_scim_users_tenant ON scim_users(tenant_id);
CREATE INDEX IF NOT EXISTS idx_scim_users_external ON scim_users(external_id);
CREATE INDEX IF NOT EXISTS idx_audit_export_tenant ON audit_log_exports(tenant_id);
CREATE INDEX IF NOT EXISTS idx_audit_export_status ON audit_log_exports(status);
CREATE INDEX IF NOT EXISTS idx_retention_tenant ON data_retention_policies(tenant_id);
CREATE INDEX IF NOT EXISTS idx_retention_type ON data_retention_policies(resource_type);
CREATE INDEX IF NOT EXISTS idx_retention_jobs_policy ON data_retention_jobs(policy_id);
CREATE INDEX IF NOT EXISTS idx_retention_jobs_status ON data_retention_jobs(status);

-- ============================================
-- Phase 8 Task 7: 全球化与本地化
-- ============================================

-- 翻译表
CREATE TABLE IF NOT EXISTS translations (
    id TEXT PRIMARY KEY,
    key TEXT NOT NULL,
    language TEXT NOT NULL,
    value TEXT NOT NULL,
    namespace TEXT DEFAULT 'common',
    context TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    is_reviewed INTEGER DEFAULT 0,
    reviewed_by TEXT,
    reviewed_at TIMESTAMP,
    UNIQUE(key, language, namespace)
);

-- 语言配置表
CREATE TABLE IF NOT EXISTS language_configs (
    code TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    name_local TEXT NOT NULL,
    is_rtl INTEGER DEFAULT 0,
    is_active INTEGER DEFAULT 1,
    is_default INTEGER DEFAULT 0,
    fallback_language TEXT,
    date_format TEXT,
    time_format TEXT,
    datetime_format TEXT,
    number_format TEXT,
    currency_format TEXT,
    first_day_of_week INTEGER DEFAULT 1,
    calendar_type TEXT DEFAULT 'gregorian'
);

-- 数据中心表
CREATE TABLE IF NOT EXISTS data_centers (
    id TEXT PRIMARY KEY,
    region_code TEXT NOT NULL UNIQUE,
    name TEXT NOT NULL,
    location TEXT NOT NULL,
    endpoint TEXT NOT NULL,
    status TEXT DEFAULT 'active',
    priority INTEGER DEFAULT 1,
    supported_regions TEXT DEFAULT '[]',
    capabilities TEXT DEFAULT '{}',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 租户数据中心映射表
CREATE TABLE IF NOT EXISTS tenant_data_center_mappings (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL UNIQUE,
    primary_dc_id TEXT NOT NULL,
    secondary_dc_id TEXT,
    region_code TEXT NOT NULL,
    data_residency TEXT DEFAULT 'regional',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    FOREIGN KEY (primary_dc_id) REFERENCES data_centers(id),
    FOREIGN KEY (secondary_dc_id) REFERENCES data_centers(id)
);

-- 本地化支付方式表
CREATE TABLE IF NOT EXISTS localized_payment_methods (
    id TEXT PRIMARY KEY,
    provider TEXT NOT NULL UNIQUE,
    name TEXT NOT NULL,
    name_local TEXT DEFAULT '{}',
    supported_countries TEXT DEFAULT '[]',
    supported_currencies TEXT DEFAULT '[]',
    is_active INTEGER DEFAULT 1,
    config TEXT DEFAULT '{}',
    icon_url TEXT,
    display_order INTEGER DEFAULT 0,
    min_amount REAL,
    max_amount REAL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 国家配置表
CREATE TABLE IF NOT EXISTS country_configs (
    code TEXT PRIMARY KEY,
    code3 TEXT NOT NULL,
    name TEXT NOT NULL,
    name_local TEXT DEFAULT '{}',
    region TEXT NOT NULL,
    default_language TEXT NOT NULL,
    supported_languages TEXT DEFAULT '[]',
    default_currency TEXT NOT NULL,
    supported_currencies TEXT DEFAULT '[]',
    timezone TEXT NOT NULL,
    calendar_type TEXT DEFAULT 'gregorian',
    date_format TEXT,
    time_format TEXT,
    number_format TEXT,
    address_format TEXT,
    phone_format TEXT,
    vat_rate REAL,
    is_active INTEGER DEFAULT 1
);

-- 时区配置表
CREATE TABLE IF NOT EXISTS timezone_configs (
    id TEXT PRIMARY KEY,
    timezone TEXT NOT NULL UNIQUE,
    utc_offset TEXT NOT NULL,
    dst_offset TEXT,
    country_code TEXT NOT NULL,
    region TEXT NOT NULL,
    is_active INTEGER DEFAULT 1
);

-- 货币配置表
CREATE TABLE IF NOT EXISTS currency_configs (
    code TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    name_local TEXT DEFAULT '{}',
    symbol TEXT NOT NULL,
    decimal_places INTEGER DEFAULT 2,
    decimal_separator TEXT DEFAULT '.',
    thousands_separator TEXT DEFAULT ',',
    is_active INTEGER DEFAULT 1
);

-- 租户本地化设置表
CREATE TABLE IF NOT EXISTS localization_settings (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL UNIQUE,
    default_language TEXT DEFAULT 'en',
    supported_languages TEXT DEFAULT '["en"]',
    default_currency TEXT DEFAULT 'USD',
    supported_currencies TEXT DEFAULT '["USD"]',
    default_timezone TEXT DEFAULT 'UTC',
    default_date_format TEXT,
    default_time_format TEXT,
    default_number_format TEXT,
    calendar_type TEXT DEFAULT 'gregorian',
    first_day_of_week INTEGER DEFAULT 1,
    region_code TEXT DEFAULT 'global',
    data_residency TEXT DEFAULT 'regional',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 本地化相关索引
CREATE INDEX IF NOT EXISTS idx_translations_key ON translations(key);
CREATE INDEX IF NOT EXISTS idx_translations_lang ON translations(language);
CREATE INDEX IF NOT EXISTS idx_translations_ns ON translations(namespace);
CREATE INDEX IF NOT EXISTS idx_dc_region ON data_centers(region_code);
CREATE INDEX IF NOT EXISTS idx_dc_status ON data_centers(status);
CREATE INDEX IF NOT EXISTS idx_tenant_dc ON tenant_data_center_mappings(tenant_id);
CREATE INDEX IF NOT EXISTS idx_payment_provider ON localized_payment_methods(provider);
CREATE INDEX IF NOT EXISTS idx_payment_active ON localized_payment_methods(is_active);
CREATE INDEX IF NOT EXISTS idx_country_region ON country_configs(region);
CREATE INDEX IF NOT EXISTS idx_tz_country ON timezone_configs(country_code);
CREATE INDEX IF NOT EXISTS idx_locale_settings_tenant ON localization_settings(tenant_id);

-- ============================================
-- Phase 8 Task 4: AI 能力增强
-- ============================================

-- 自定义模型表
CREATE TABLE IF NOT EXISTS custom_models (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    description TEXT,
    model_type TEXT NOT NULL,  -- custom_ner, multimodal, summarization, prediction
    status TEXT DEFAULT 'pending',  -- pending, training, ready, failed, archived
    training_data TEXT DEFAULT '{}',
    hyperparameters TEXT DEFAULT '{}',
    metrics TEXT DEFAULT '{}',
    model_path TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    trained_at TIMESTAMP,
    created_by TEXT NOT NULL,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 训练样本表
CREATE TABLE IF NOT EXISTS training_samples (
    id TEXT PRIMARY KEY,
    model_id TEXT NOT NULL,
    text TEXT NOT NULL,
    entities TEXT DEFAULT '[]',  -- JSON: [{"start": 0, "end": 5, "label": "PERSON", "text": "..."}]
    metadata TEXT DEFAULT '{}',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (model_id) REFERENCES custom_models(id) ON DELETE CASCADE
);

-- 多模态分析表
CREATE TABLE IF NOT EXISTS multimodal_analyses (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    project_id TEXT NOT NULL,
    provider TEXT NOT NULL,  -- gpt-4-vision, claude-3, gemini-pro-vision, kimi-vl
    input_type TEXT NOT NULL,  -- image, video, audio, mixed
    input_urls TEXT DEFAULT '[]',
    prompt TEXT NOT NULL,
    result TEXT DEFAULT '{}',
    tokens_used INTEGER DEFAULT 0,
    cost REAL DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
);

-- 知识图谱 RAG 配置表
CREATE TABLE IF NOT EXISTS kg_rag_configs (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    project_id TEXT NOT NULL,
    name TEXT NOT NULL,
    description TEXT,
    kg_config TEXT DEFAULT '{}',  -- 知识图谱配置
    retrieval_config TEXT DEFAULT '{}',  -- 检索配置
    generation_config TEXT DEFAULT '{}',  -- 生成配置
    is_active INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
);

-- RAG 查询记录表
CREATE TABLE IF NOT EXISTS rag_queries (
    id TEXT PRIMARY KEY,
    rag_id TEXT NOT NULL,
    query TEXT NOT NULL,
    context TEXT DEFAULT '{}',
    answer TEXT NOT NULL,
    sources TEXT DEFAULT '[]',
    confidence REAL DEFAULT 0,
    tokens_used INTEGER DEFAULT 0,
    latency_ms INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (rag_id) REFERENCES kg_rag_configs(id) ON DELETE CASCADE
);

-- 智能摘要表
CREATE TABLE IF NOT EXISTS smart_summaries (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    project_id TEXT NOT NULL,
    source_type TEXT NOT NULL,  -- transcript, entity, project
    source_id TEXT NOT NULL,
    summary_type TEXT NOT NULL,  -- extractive, abstractive, key_points, timeline
    content TEXT NOT NULL,
    key_points TEXT DEFAULT '[]',
    entities_mentioned TEXT DEFAULT '[]',
    confidence REAL DEFAULT 0,
    tokens_used INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
);

-- 预测模型表
CREATE TABLE IF NOT EXISTS prediction_models (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    project_id TEXT NOT NULL,
    name TEXT NOT NULL,
    prediction_type TEXT NOT NULL,  -- trend, anomaly, entity_growth, relation_evolution
    target_entity_type TEXT,
    features TEXT DEFAULT '[]',
    model_config TEXT DEFAULT '{}',
    accuracy REAL,
    last_trained_at TIMESTAMP,
    prediction_count INTEGER DEFAULT 0,
    is_active INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
);

-- 预测结果表
CREATE TABLE IF NOT EXISTS prediction_results (
    id TEXT PRIMARY KEY,
    model_id TEXT NOT NULL,
    prediction_type TEXT NOT NULL,
    target_id TEXT,
    prediction_data TEXT DEFAULT '{}',
    confidence REAL DEFAULT 0,
    explanation TEXT,
    actual_value TEXT,
    is_correct INTEGER,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (model_id) REFERENCES prediction_models(id) ON DELETE CASCADE
);

-- AI 能力增强相关索引
CREATE INDEX IF NOT EXISTS idx_custom_models_tenant ON custom_models(tenant_id);
CREATE INDEX IF NOT EXISTS idx_custom_models_type ON custom_models(model_type);
CREATE INDEX IF NOT EXISTS idx_custom_models_status ON custom_models(status);
CREATE INDEX IF NOT EXISTS idx_training_samples_model ON training_samples(model_id);
CREATE INDEX IF NOT EXISTS idx_multimodal_tenant ON multimodal_analyses(tenant_id);
CREATE INDEX IF NOT EXISTS idx_multimodal_project ON multimodal_analyses(project_id);
CREATE INDEX IF NOT EXISTS idx_kg_rag_tenant ON kg_rag_configs(tenant_id);
CREATE INDEX IF NOT EXISTS idx_kg_rag_project ON kg_rag_configs(project_id);
CREATE INDEX IF NOT EXISTS idx_rag_queries_rag ON rag_queries(rag_id);
CREATE INDEX IF NOT EXISTS idx_smart_summaries_tenant ON smart_summaries(tenant_id);
CREATE INDEX IF NOT EXISTS idx_smart_summaries_project ON smart_summaries(project_id);
CREATE INDEX IF NOT EXISTS idx_prediction_models_tenant ON prediction_models(tenant_id);
CREATE INDEX IF NOT EXISTS idx_prediction_models_project ON prediction_models(project_id);
CREATE INDEX IF NOT EXISTS idx_prediction_results_model ON prediction_results(model_id);

-- ============================================
-- Phase 8 Task 5: 运营与增长工具
-- ============================================

-- 分析事件表
CREATE TABLE IF NOT EXISTS analytics_events (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    user_id TEXT NOT NULL,
    event_type TEXT NOT NULL,  -- page_view, feature_use, conversion, signup, login, etc.
    event_name TEXT NOT NULL,
    properties TEXT DEFAULT '{}',  -- JSON: 事件属性
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    session_id TEXT,
    device_info TEXT DEFAULT '{}',  -- JSON: 设备信息
    referrer TEXT,
    utm_source TEXT,
    utm_medium TEXT,
    utm_campaign TEXT,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 用户画像表
CREATE TABLE IF NOT EXISTS user_profiles (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    user_id TEXT NOT NULL UNIQUE,
    first_seen TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    last_seen TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    total_sessions INTEGER DEFAULT 0,
    total_events INTEGER DEFAULT 0,
    feature_usage TEXT DEFAULT '{}',  -- JSON: 功能使用统计
    subscription_history TEXT DEFAULT '[]',  -- JSON: 订阅历史
    ltv REAL DEFAULT 0,  -- 生命周期价值
    churn_risk_score REAL DEFAULT 0,  -- 流失风险分数
    engagement_score REAL DEFAULT 0.5,  -- 参与度分数
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 转化漏斗表
CREATE TABLE IF NOT EXISTS funnels (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    description TEXT,
    steps TEXT NOT NULL,  -- JSON: 漏斗步骤 [{"name": "", "event_name": ""}]
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- A/B 测试实验表
CREATE TABLE IF NOT EXISTS experiments (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    description TEXT,
    hypothesis TEXT,
    status TEXT DEFAULT 'draft',  -- draft, running, paused, completed, archived
    variants TEXT NOT NULL,  -- JSON: 实验变体
    traffic_allocation TEXT DEFAULT 'random',  -- random, stratified, targeted
    traffic_split TEXT DEFAULT '{}',  -- JSON: 流量分配比例
    target_audience TEXT DEFAULT '{}',  -- JSON: 目标受众条件
    primary_metric TEXT NOT NULL,
    secondary_metrics TEXT DEFAULT '[]',  -- JSON: 次要指标列表
    start_date TIMESTAMP,
    end_date TIMESTAMP,
    min_sample_size INTEGER DEFAULT 100,
    confidence_level REAL DEFAULT 0.95,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_by TEXT,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 实验分配记录表
CREATE TABLE IF NOT EXISTS experiment_assignments (
    id TEXT PRIMARY KEY,
    experiment_id TEXT NOT NULL,
    user_id TEXT NOT NULL,
    variant_id TEXT NOT NULL,
    user_attributes TEXT DEFAULT '{}',  -- JSON: 用户属性
    assigned_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (experiment_id) REFERENCES experiments(id) ON DELETE CASCADE,
    UNIQUE(experiment_id, user_id)
);

-- 实验指标记录表
CREATE TABLE IF NOT EXISTS experiment_metrics (
    id TEXT PRIMARY KEY,
    experiment_id TEXT NOT NULL,
    variant_id TEXT NOT NULL,
    user_id TEXT NOT NULL,
    metric_name TEXT NOT NULL,
    metric_value REAL DEFAULT 0,
    recorded_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (experiment_id) REFERENCES experiments(id) ON DELETE CASCADE
);

-- 邮件模板表
CREATE TABLE IF NOT EXISTS email_templates (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    template_type TEXT NOT NULL,  -- welcome, onboarding, feature_announcement, churn_recovery, etc.
    subject TEXT NOT NULL,
    html_content TEXT NOT NULL,
    text_content TEXT,
    variables TEXT DEFAULT '[]',  -- JSON: 模板变量列表
    preview_text TEXT,
    from_name TEXT DEFAULT 'InsightFlow',
    from_email TEXT DEFAULT 'noreply@insightflow.io',
    reply_to TEXT,
    is_active INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 邮件营销活动表
CREATE TABLE IF NOT EXISTS email_campaigns (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    template_id TEXT NOT NULL,
    status TEXT DEFAULT 'draft',  -- draft, scheduled, sending, completed
    recipient_count INTEGER DEFAULT 0,
    sent_count INTEGER DEFAULT 0,
    delivered_count INTEGER DEFAULT 0,
    opened_count INTEGER DEFAULT 0,
    clicked_count INTEGER DEFAULT 0,
    bounced_count INTEGER DEFAULT 0,
    failed_count INTEGER DEFAULT 0,
    scheduled_at TIMESTAMP,
    started_at TIMESTAMP,
    completed_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    FOREIGN KEY (template_id) REFERENCES email_templates(id) ON DELETE CASCADE
);

-- 邮件发送记录表
CREATE TABLE IF NOT EXISTS email_logs (
    id TEXT PRIMARY KEY,
    campaign_id TEXT,
    tenant_id TEXT NOT NULL,
    user_id TEXT NOT NULL,
    email TEXT NOT NULL,
    template_id TEXT NOT NULL,
    status TEXT DEFAULT 'draft',  -- draft, scheduled, sending, sent, delivered, opened, clicked, bounced, failed
    subject TEXT,
    sent_at TIMESTAMP,
    delivered_at TIMESTAMP,
    opened_at TIMESTAMP,
    clicked_at TIMESTAMP,
    ip_address TEXT,
    user_agent TEXT,
    error_message TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (campaign_id) REFERENCES email_campaigns(id) ON DELETE SET NULL,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    FOREIGN KEY (template_id) REFERENCES email_templates(id) ON DELETE CASCADE
);

-- 自动化工作流表
CREATE TABLE IF NOT EXISTS automation_workflows (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    description TEXT,
    trigger_type TEXT NOT NULL,  -- user_signup, user_login, subscription_created, inactivity, etc.
    trigger_conditions TEXT DEFAULT '{}',  -- JSON: 触发条件
    actions TEXT NOT NULL,  -- JSON: 执行动作列表
    is_active INTEGER DEFAULT 1,
    execution_count INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 推荐计划表
CREATE TABLE IF NOT EXISTS referral_programs (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    description TEXT,
    referrer_reward_type TEXT NOT NULL,  -- credit, discount, feature
    referrer_reward_value REAL DEFAULT 0,
    referee_reward_type TEXT NOT NULL,
    referee_reward_value REAL DEFAULT 0,
    max_referrals_per_user INTEGER DEFAULT 10,
    referral_code_length INTEGER DEFAULT 8,
    expiry_days INTEGER DEFAULT 30,
    is_active INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 推荐记录表
CREATE TABLE IF NOT EXISTS referrals (
    id TEXT PRIMARY KEY,
    program_id TEXT NOT NULL,
    tenant_id TEXT NOT NULL,
    referrer_id TEXT NOT NULL,  -- 推荐人
    referee_id TEXT,  -- 被推荐人
    referral_code TEXT NOT NULL UNIQUE,
    status TEXT DEFAULT 'pending',  -- pending, converted, rewarded, expired
    referrer_rewarded INTEGER DEFAULT 0,
    referee_rewarded INTEGER DEFAULT 0,
    referrer_reward_value REAL DEFAULT 0,
    referee_reward_value REAL DEFAULT 0,
    converted_at TIMESTAMP,
    rewarded_at TIMESTAMP,
    expires_at TIMESTAMP NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (program_id) REFERENCES referral_programs(id) ON DELETE CASCADE,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 团队升级激励表
CREATE TABLE IF NOT EXISTS team_incentives (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    description TEXT,
    target_tier TEXT NOT NULL,  -- 目标层级
    min_team_size INTEGER DEFAULT 1,
    incentive_type TEXT NOT NULL,  -- credit, discount, feature
    incentive_value REAL DEFAULT 0,
    valid_from TIMESTAMP NOT NULL,
    valid_until TIMESTAMP NOT NULL,
    is_active INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 运营与增长相关索引
CREATE INDEX IF NOT EXISTS idx_analytics_tenant ON analytics_events(tenant_id);
CREATE INDEX IF NOT EXISTS idx_analytics_user ON analytics_events(user_id);
CREATE INDEX IF NOT EXISTS idx_analytics_type ON analytics_events(event_type);
CREATE INDEX IF NOT EXISTS idx_analytics_timestamp ON analytics_events(timestamp);
CREATE INDEX IF NOT EXISTS idx_analytics_session ON analytics_events(session_id);
CREATE INDEX IF NOT EXISTS idx_user_profiles_tenant ON user_profiles(tenant_id);
CREATE INDEX IF NOT EXISTS idx_user_profiles_user ON user_profiles(user_id);
CREATE INDEX IF NOT EXISTS idx_funnels_tenant ON funnels(tenant_id);
CREATE INDEX IF NOT EXISTS idx_experiments_tenant ON experiments(tenant_id);
CREATE INDEX IF NOT EXISTS idx_experiments_status ON experiments(status);
CREATE INDEX IF NOT EXISTS idx_exp_assignments_exp ON experiment_assignments(experiment_id);
CREATE INDEX IF NOT EXISTS idx_exp_assignments_user ON experiment_assignments(user_id);
CREATE INDEX IF NOT EXISTS idx_exp_metrics_exp ON experiment_metrics(experiment_id);
CREATE INDEX IF NOT EXISTS idx_email_templates_tenant ON email_templates(tenant_id);
CREATE INDEX IF NOT EXISTS idx_email_templates_type ON email_templates(template_type);
CREATE INDEX IF NOT EXISTS idx_email_campaigns_tenant ON email_campaigns(tenant_id);
CREATE INDEX IF NOT EXISTS idx_email_logs_campaign ON email_logs(campaign_id);
CREATE INDEX IF NOT EXISTS idx_email_logs_user ON email_logs(user_id);
CREATE INDEX IF NOT EXISTS idx_email_logs_status ON email_logs(status);
CREATE INDEX IF NOT EXISTS idx_automation_workflows_tenant ON automation_workflows(tenant_id);
CREATE INDEX IF NOT EXISTS idx_referral_programs_tenant ON referral_programs(tenant_id);
CREATE INDEX IF NOT EXISTS idx_referrals_program ON referrals(program_id);
CREATE INDEX IF NOT EXISTS idx_referrals_code ON referrals(referral_code);
CREATE INDEX IF NOT EXISTS idx_referrals_referrer ON referrals(referrer_id);
CREATE INDEX IF NOT EXISTS idx_team_incentives_tenant ON team_incentives(tenant_id);

-- ============================================
-- Phase 8 Task 6: 开发者生态系统
-- ============================================

-- SDK 发布表
CREATE TABLE IF NOT EXISTS sdk_releases (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    language TEXT NOT NULL,  -- python, javascript, typescript, go, java, rust
    version TEXT NOT NULL,
    description TEXT NOT NULL,
    changelog TEXT,
    download_url TEXT NOT NULL,
    documentation_url TEXT,
    repository_url TEXT,
    package_name TEXT NOT NULL,  -- pip/npm/go module name
    status TEXT DEFAULT 'draft',  -- draft, beta, stable, deprecated, archived
    min_platform_version TEXT DEFAULT '1.0.0',
    dependencies TEXT DEFAULT '[]',  -- JSON: [{"name": "requests", "version": ">=2.0"}]
    file_size INTEGER DEFAULT 0,
    checksum TEXT,
    download_count INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    published_at TIMESTAMP,
    created_by TEXT NOT NULL
);

-- SDK 版本历史表
CREATE TABLE IF NOT EXISTS sdk_versions (
    id TEXT PRIMARY KEY,
    sdk_id TEXT NOT NULL,
    version TEXT NOT NULL,
    is_latest INTEGER DEFAULT 0,
    is_lts INTEGER DEFAULT 0,  -- 长期支持版本
    release_notes TEXT,
    download_url TEXT NOT NULL,
    checksum TEXT,
    file_size INTEGER DEFAULT 0,
    download_count INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (sdk_id) REFERENCES sdk_releases(id) ON DELETE CASCADE
);

-- 模板市场表
CREATE TABLE IF NOT EXISTS template_market (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    description TEXT NOT NULL,
    category TEXT NOT NULL,  -- medical, legal, finance, education, tech, general
    subcategory TEXT,
    tags TEXT DEFAULT '[]',  -- JSON array
    author_id TEXT NOT NULL,
    author_name TEXT NOT NULL,
    status TEXT DEFAULT 'pending',  -- pending, approved, rejected, published, unlisted
    price REAL DEFAULT 0,  -- 0 = 免费
    currency TEXT DEFAULT 'CNY',
    preview_image_url TEXT,
    demo_url TEXT,
    documentation_url TEXT,
    download_url TEXT,
    install_count INTEGER DEFAULT 0,
    rating REAL DEFAULT 0,
    rating_count INTEGER DEFAULT 0,
    review_count INTEGER DEFAULT 0,
    version TEXT DEFAULT '1.0.0',
    min_platform_version TEXT DEFAULT '1.0.0',
    file_size INTEGER DEFAULT 0,
    checksum TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    published_at TIMESTAMP
);

-- 模板评价表
CREATE TABLE IF NOT EXISTS template_reviews (
    id TEXT PRIMARY KEY,
    template_id TEXT NOT NULL,
    user_id TEXT NOT NULL,
    user_name TEXT NOT NULL,
    rating INTEGER NOT NULL,  -- 1-5
    comment TEXT,
    is_verified_purchase INTEGER DEFAULT 0,
    helpful_count INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (template_id) REFERENCES template_market(id) ON DELETE CASCADE
);

-- 插件市场表
CREATE TABLE IF NOT EXISTS plugin_market (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    description TEXT NOT NULL,
    category TEXT NOT NULL,  -- integration, analysis, visualization, automation, security, custom
    tags TEXT DEFAULT '[]',  -- JSON array
    author_id TEXT NOT NULL,
    author_name TEXT NOT NULL,
    status TEXT DEFAULT 'pending',  -- pending, reviewing, approved, rejected, published, suspended
    price REAL DEFAULT 0,
    currency TEXT DEFAULT 'CNY',
    pricing_model TEXT DEFAULT 'free',  -- free, paid, freemium, subscription
    preview_image_url TEXT,
    demo_url TEXT,
    documentation_url TEXT,
    repository_url TEXT,
    download_url TEXT,
    webhook_url TEXT,  -- 用于插件回调
    permissions TEXT DEFAULT '[]',  -- JSON: 需要的权限列表
    install_count INTEGER DEFAULT 0,
    active_install_count INTEGER DEFAULT 0,
    rating REAL DEFAULT 0,
    rating_count INTEGER DEFAULT 0,
    review_count INTEGER DEFAULT 0,
    version TEXT DEFAULT '1.0.0',
    min_platform_version TEXT DEFAULT '1.0.0',
    file_size INTEGER DEFAULT 0,
    checksum TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    published_at TIMESTAMP,
    reviewed_by TEXT,
    reviewed_at TIMESTAMP,
    review_notes TEXT
);

-- 插件评价表
CREATE TABLE IF NOT EXISTS plugin_reviews (
    id TEXT PRIMARY KEY,
    plugin_id TEXT NOT NULL,
    user_id TEXT NOT NULL,
    user_name TEXT NOT NULL,
    rating INTEGER NOT NULL,  -- 1-5
    comment TEXT,
    is_verified_purchase INTEGER DEFAULT 0,
    helpful_count INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (plugin_id) REFERENCES plugin_market(id) ON DELETE CASCADE
);

-- 开发者档案表
CREATE TABLE IF NOT EXISTS developer_profiles (
    id TEXT PRIMARY KEY,
    user_id TEXT NOT NULL UNIQUE,
    display_name TEXT NOT NULL,
    email TEXT NOT NULL,
    bio TEXT,
    website TEXT,
    github_url TEXT,
    avatar_url TEXT,
    status TEXT DEFAULT 'unverified',  -- unverified, pending, verified, certified, suspended
    verification_documents TEXT DEFAULT '{}',  -- JSON: 认证文档
    total_sales REAL DEFAULT 0,
    total_downloads INTEGER DEFAULT 0,
    plugin_count INTEGER DEFAULT 0,
    template_count INTEGER DEFAULT 0,
    rating_average REAL DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    verified_at TIMESTAMP
);

-- 开发者收益表
CREATE TABLE IF NOT EXISTS developer_revenues (
    id TEXT PRIMARY KEY,
    developer_id TEXT NOT NULL,
    item_type TEXT NOT NULL,  -- plugin, template
    item_id TEXT NOT NULL,
    item_name TEXT NOT NULL,
    sale_amount REAL NOT NULL,
    platform_fee REAL NOT NULL,
    developer_earnings REAL NOT NULL,
    currency TEXT DEFAULT 'CNY',
    buyer_id TEXT NOT NULL,
    transaction_id TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (developer_id) REFERENCES developer_profiles(id) ON DELETE CASCADE
);

-- 代码示例表
CREATE TABLE IF NOT EXISTS code_examples (
    id TEXT PRIMARY KEY,
    title TEXT NOT NULL,
    description TEXT,
    language TEXT NOT NULL,
    category TEXT NOT NULL,
    code TEXT NOT NULL,
    explanation TEXT,
    tags TEXT DEFAULT '[]',  -- JSON array
    author_id TEXT NOT NULL,
    author_name TEXT NOT NULL,
    sdk_id TEXT,  -- 关联的 SDK
    api_endpoints TEXT DEFAULT '[]',  -- JSON: 涉及的 API 端点
    view_count INTEGER DEFAULT 0,
    copy_count INTEGER DEFAULT 0,
    rating REAL DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (sdk_id) REFERENCES sdk_releases(id) ON DELETE SET NULL
);

-- API 文档表
CREATE TABLE IF NOT EXISTS api_documentation (
    id TEXT PRIMARY KEY,
    version TEXT NOT NULL,
    openapi_spec TEXT NOT NULL,  -- OpenAPI JSON
    markdown_content TEXT NOT NULL,
    html_content TEXT NOT NULL,
    changelog TEXT,
    generated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    generated_by TEXT NOT NULL
);

-- 开发者门户配置表
CREATE TABLE IF NOT EXISTS developer_portal_configs (
    id TEXT PRIMARY KEY,
    name TEXT NOT NULL,
    description TEXT,
    theme TEXT DEFAULT 'default',
    custom_css TEXT,
    custom_js TEXT,
    logo_url TEXT,
    favicon_url TEXT,
    primary_color TEXT DEFAULT '#1890ff',
    secondary_color TEXT DEFAULT '#52c41a',
    support_email TEXT DEFAULT 'support@insightflow.io',
    support_url TEXT,
    github_url TEXT,
    discord_url TEXT,
    api_base_url TEXT DEFAULT 'https://api.insightflow.io',
    is_active INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- 开发者生态系统相关索引
CREATE INDEX IF NOT EXISTS idx_sdk_language ON sdk_releases(language);
CREATE INDEX IF NOT EXISTS idx_sdk_status ON sdk_releases(status);
CREATE INDEX IF NOT EXISTS idx_sdk_package ON sdk_releases(package_name);
CREATE INDEX IF NOT EXISTS idx_sdk_versions_sdk ON sdk_versions(sdk_id);
CREATE INDEX IF NOT EXISTS idx_template_category ON template_market(category);
CREATE INDEX IF NOT EXISTS idx_template_status ON template_market(status);
CREATE INDEX IF NOT EXISTS idx_template_author ON template_market(author_id);
CREATE INDEX IF NOT EXISTS idx_template_price ON template_market(price);
CREATE INDEX IF NOT EXISTS idx_template_reviews_template ON template_reviews(template_id);
CREATE INDEX IF NOT EXISTS idx_plugin_category ON plugin_market(category);
CREATE INDEX IF NOT EXISTS idx_plugin_status ON plugin_market(status);
CREATE INDEX IF NOT EXISTS idx_plugin_author ON plugin_market(author_id);
CREATE INDEX IF NOT EXISTS idx_plugin_reviews_plugin ON plugin_reviews(plugin_id);
CREATE INDEX IF NOT EXISTS idx_developer_user ON developer_profiles(user_id);
CREATE INDEX IF NOT EXISTS idx_developer_status ON developer_profiles(status);
CREATE INDEX IF NOT EXISTS idx_developer_revenues_dev ON developer_revenues(developer_id);
CREATE INDEX IF NOT EXISTS idx_code_examples_language ON code_examples(language);
CREATE INDEX IF NOT EXISTS idx_code_examples_category ON code_examples(category);
CREATE INDEX IF NOT EXISTS idx_code_examples_sdk ON code_examples(sdk_id);

-- ============================================
-- Phase 8 Task 8: 运维与监控
-- ============================================

-- 告警规则表
CREATE TABLE IF NOT EXISTS alert_rules (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    description TEXT,
    rule_type TEXT NOT NULL,  -- threshold, anomaly, predictive, composite
    severity TEXT NOT NULL,  -- p0, p1, p2, p3
    metric TEXT NOT NULL,
    condition TEXT NOT NULL,  -- >, <, >=, <=, ==, !=
    threshold REAL NOT NULL,
    duration INTEGER DEFAULT 60,  -- 持续时间（秒）
    evaluation_interval INTEGER DEFAULT 60,  -- 评估间隔（秒）
    channels TEXT DEFAULT '[]',  -- JSON: 告警渠道ID列表
    labels TEXT DEFAULT '{}',  -- JSON: 标签
    annotations TEXT DEFAULT '{}',  -- JSON: 注释
    is_enabled INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    created_by TEXT NOT NULL,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 告警渠道表
CREATE TABLE IF NOT EXISTS alert_channels (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    channel_type TEXT NOT NULL,  -- pagerduty, opsgenie, feishu, dingtalk, slack, email, sms, webhook
    config TEXT DEFAULT '{}',  -- JSON: 渠道特定配置
    severity_filter TEXT DEFAULT '["p0", "p1", "p2", "p3"]',  -- JSON: 过滤的告警级别
    is_enabled INTEGER DEFAULT 1,
    success_count INTEGER DEFAULT 0,
    fail_count INTEGER DEFAULT 0,
    last_used_at TIMESTAMP,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 告警实例表
CREATE TABLE IF NOT EXISTS alerts (
    id TEXT PRIMARY KEY,
    rule_id TEXT NOT NULL,
    tenant_id TEXT NOT NULL,
    severity TEXT NOT NULL,  -- p0, p1, p2, p3
    status TEXT DEFAULT 'firing',  -- firing, resolved, acknowledged, suppressed
    title TEXT NOT NULL,
    description TEXT,
    metric TEXT NOT NULL,
    value REAL NOT NULL,
    threshold REAL NOT NULL,
    labels TEXT DEFAULT '{}',  -- JSON: 标签
    annotations TEXT DEFAULT '{}',  -- JSON: 注释
    started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    resolved_at TIMESTAMP,
    acknowledged_by TEXT,
    acknowledged_at TIMESTAMP,
    notification_sent TEXT DEFAULT '{}',  -- JSON: 渠道发送状态
    suppression_count INTEGER DEFAULT 0,
    FOREIGN KEY (rule_id) REFERENCES alert_rules(id) ON DELETE CASCADE,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 告警抑制规则表
CREATE TABLE IF NOT EXISTS alert_suppression_rules (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    matchers TEXT DEFAULT '{}',  -- JSON: 匹配条件
    duration INTEGER DEFAULT 3600,  -- 抑制持续时间（秒）
    is_regex INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    expires_at TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 告警聚合组表
CREATE TABLE IF NOT EXISTS alert_groups (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    group_key TEXT NOT NULL,
    alerts TEXT DEFAULT '[]',  -- JSON: 告警ID列表
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 资源指标表
CREATE TABLE IF NOT EXISTS resource_metrics (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    resource_type TEXT NOT NULL,  -- cpu, memory, disk, network, gpu, database, cache, queue
    resource_id TEXT NOT NULL,
    metric_name TEXT NOT NULL,
    metric_value REAL NOT NULL,
    unit TEXT NOT NULL,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    metadata TEXT DEFAULT '{}',  -- JSON: 额外元数据
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 容量规划表
CREATE TABLE IF NOT EXISTS capacity_plans (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    resource_type TEXT NOT NULL,
    current_capacity REAL NOT NULL,
    predicted_capacity REAL NOT NULL,
    prediction_date TEXT NOT NULL,
    confidence REAL DEFAULT 0.8,
    recommended_action TEXT NOT NULL,  -- scale_up, scale_down, maintain
    estimated_cost REAL DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 自动扩缩容策略表
CREATE TABLE IF NOT EXISTS auto_scaling_policies (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    resource_type TEXT NOT NULL,
    min_instances INTEGER DEFAULT 1,
    max_instances INTEGER DEFAULT 10,
    target_utilization REAL DEFAULT 0.7,
    scale_up_threshold REAL DEFAULT 0.8,
    scale_down_threshold REAL DEFAULT 0.3,
    scale_up_step INTEGER DEFAULT 1,
    scale_down_step INTEGER DEFAULT 1,
    cooldown_period INTEGER DEFAULT 300,  -- 冷却时间（秒）
    is_enabled INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 扩缩容事件表
CREATE TABLE IF NOT EXISTS scaling_events (
    id TEXT PRIMARY KEY,
    policy_id TEXT NOT NULL,
    tenant_id TEXT NOT NULL,
    action TEXT NOT NULL,  -- scale_up, scale_down, maintain
    from_count INTEGER NOT NULL,
    to_count INTEGER NOT NULL,
    reason TEXT,
    triggered_by TEXT DEFAULT 'auto',  -- manual, auto, scheduled
    status TEXT DEFAULT 'pending',  -- pending, in_progress, completed, failed
    started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    completed_at TIMESTAMP,
    error_message TEXT,
    FOREIGN KEY (policy_id) REFERENCES auto_scaling_policies(id) ON DELETE CASCADE,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 健康检查配置表
CREATE TABLE IF NOT EXISTS health_checks (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    target_type TEXT NOT NULL,  -- service, database, api, etc.
    target_id TEXT NOT NULL,
    check_type TEXT NOT NULL,  -- http, tcp, ping, custom
    check_config TEXT DEFAULT '{}',  -- JSON: 检查配置
    interval INTEGER DEFAULT 60,  -- 检查间隔（秒）
    timeout INTEGER DEFAULT 10,  -- 超时时间（秒）
    retry_count INTEGER DEFAULT 3,
    healthy_threshold INTEGER DEFAULT 2,
    unhealthy_threshold INTEGER DEFAULT 3,
    is_enabled INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 健康检查结果表
CREATE TABLE IF NOT EXISTS health_check_results (
    id TEXT PRIMARY KEY,
    check_id TEXT NOT NULL,
    tenant_id TEXT NOT NULL,
    status TEXT NOT NULL,  -- healthy, degraded, unhealthy, unknown
    response_time REAL DEFAULT 0,  -- 响应时间（毫秒）
    message TEXT,
    details TEXT DEFAULT '{}',  -- JSON: 详细信息
    checked_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (check_id) REFERENCES health_checks(id) ON DELETE CASCADE,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 故障转移配置表
CREATE TABLE IF NOT EXISTS failover_configs (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    primary_region TEXT NOT NULL,
    secondary_regions TEXT DEFAULT '[]',  -- JSON: 备用区域列表
    failover_trigger TEXT NOT NULL,
    auto_failover INTEGER DEFAULT 0,
    failover_timeout INTEGER DEFAULT 300,  -- 故障转移超时（秒）
    health_check_id TEXT,
    is_enabled INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
    FOREIGN KEY (health_check_id) REFERENCES health_checks(id) ON DELETE SET NULL
);

-- 故障转移事件表
CREATE TABLE IF NOT EXISTS failover_events (
    id TEXT PRIMARY KEY,
    config_id TEXT NOT NULL,
    tenant_id TEXT NOT NULL,
    from_region TEXT NOT NULL,
    to_region TEXT NOT NULL,
    reason TEXT,
    status TEXT DEFAULT 'initiated',  -- initiated, in_progress, completed, failed, rolled_back
    started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    completed_at TIMESTAMP,
    rolled_back_at TIMESTAMP,
    FOREIGN KEY (config_id) REFERENCES failover_configs(id) ON DELETE CASCADE,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 备份任务表
CREATE TABLE IF NOT EXISTS backup_jobs (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    name TEXT NOT NULL,
    backup_type TEXT NOT NULL,  -- full, incremental, differential
    target_type TEXT NOT NULL,  -- database, files, configuration
    target_id TEXT NOT NULL,
    schedule TEXT NOT NULL,  -- cron 表达式
    retention_days INTEGER DEFAULT 30,
    encryption_enabled INTEGER DEFAULT 1,
    compression_enabled INTEGER DEFAULT 1,
    storage_location TEXT,
    is_enabled INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 备份记录表
CREATE TABLE IF NOT EXISTS backup_records (
    id TEXT PRIMARY KEY,
    job_id TEXT NOT NULL,
    tenant_id TEXT NOT NULL,
    status TEXT DEFAULT 'pending',  -- pending, in_progress, completed, failed, verified
    size_bytes INTEGER DEFAULT 0,
    checksum TEXT,
    started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    completed_at TIMESTAMP,
    verified_at TIMESTAMP,
    error_message TEXT,
    storage_path TEXT,
    FOREIGN KEY (job_id) REFERENCES backup_jobs(id) ON DELETE CASCADE,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 成本报告表
CREATE TABLE IF NOT EXISTS cost_reports (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    report_period TEXT NOT NULL,  -- YYYY-MM
    total_cost REAL DEFAULT 0,
    currency TEXT DEFAULT 'CNY',
    breakdown TEXT DEFAULT '{}',  -- JSON: 按资源类型分解
    trends TEXT DEFAULT '{}',  -- JSON: 趋势数据
    anomalies TEXT DEFAULT '[]',  -- JSON: 异常检测
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 资源利用率表
CREATE TABLE IF NOT EXISTS resource_utilizations (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    resource_type TEXT NOT NULL,
    resource_id TEXT NOT NULL,
    utilization_rate REAL DEFAULT 0,  -- 0-1
    peak_utilization REAL DEFAULT 0,
    avg_utilization REAL DEFAULT 0,
    idle_time_percent REAL DEFAULT 0,
    report_date TEXT NOT NULL,  -- YYYY-MM-DD
    recommendations TEXT DEFAULT '[]',  -- JSON: 建议列表
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 闲置资源表
CREATE TABLE IF NOT EXISTS idle_resources (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    resource_type TEXT NOT NULL,
    resource_id TEXT NOT NULL,
    resource_name TEXT NOT NULL,
    idle_since TIMESTAMP NOT NULL,
    estimated_monthly_cost REAL DEFAULT 0,
    currency TEXT DEFAULT 'CNY',
    reason TEXT,
    recommendation TEXT,
    detected_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 成本优化建议表
CREATE TABLE IF NOT EXISTS cost_optimization_suggestions (
    id TEXT PRIMARY KEY,
    tenant_id TEXT NOT NULL,
    category TEXT NOT NULL,  -- resource_rightsize, reserved_instances, spot_instances, etc.
    title TEXT NOT NULL,
    description TEXT,
    potential_savings REAL DEFAULT 0,
    currency TEXT DEFAULT 'CNY',
    confidence REAL DEFAULT 0.5,
    difficulty TEXT DEFAULT 'medium',  -- easy, medium, hard
    implementation_steps TEXT DEFAULT '[]',  -- JSON: 实施步骤
    risk_level TEXT DEFAULT 'low',  -- low, medium, high
    is_applied INTEGER DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    applied_at TIMESTAMP,
    FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
);

-- 运维与监控相关索引
CREATE INDEX IF NOT EXISTS idx_alert_rules_tenant ON alert_rules(tenant_id);
CREATE INDEX IF NOT EXISTS idx_alert_rules_enabled ON alert_rules(is_enabled);
CREATE INDEX IF NOT EXISTS idx_alert_channels_tenant ON alert_channels(tenant_id);
CREATE INDEX IF NOT EXISTS idx_alerts_tenant ON alerts(tenant_id);
CREATE INDEX IF NOT EXISTS idx_alerts_status ON alerts(status);
CREATE INDEX IF NOT EXISTS idx_alerts_severity ON alerts(severity);
CREATE INDEX IF NOT EXISTS idx_alerts_rule ON alerts(rule_id);
CREATE INDEX IF NOT EXISTS idx_resource_metrics_tenant ON resource_metrics(tenant_id);
CREATE INDEX IF NOT EXISTS idx_resource_metrics_type ON resource_metrics(resource_type);
CREATE INDEX IF NOT EXISTS idx_resource_metrics_name ON resource_metrics(metric_name);
CREATE INDEX IF NOT EXISTS idx_resource_metrics_timestamp ON resource_metrics(timestamp);
CREATE INDEX IF NOT EXISTS idx_capacity_plans_tenant ON capacity_plans(tenant_id);
CREATE INDEX IF NOT EXISTS idx_auto_scaling_policies_tenant ON auto_scaling_policies(tenant_id);
CREATE INDEX IF NOT EXISTS idx_scaling_events_policy ON scaling_events(policy_id);
CREATE INDEX IF NOT EXISTS idx_scaling_events_tenant ON scaling_events(tenant_id);
CREATE INDEX IF NOT EXISTS idx_health_checks_tenant ON health_checks(tenant_id);
CREATE INDEX IF NOT EXISTS idx_health_check_results_check ON health_check_results(check_id);
CREATE INDEX IF NOT EXISTS idx_failover_configs_tenant ON failover_configs(tenant_id);
CREATE INDEX IF NOT EXISTS idx_failover_events_config ON failover_events(config_id);
CREATE INDEX IF NOT EXISTS idx_backup_jobs_tenant ON backup_jobs(tenant_id);
CREATE INDEX IF NOT EXISTS idx_backup_records_job ON backup_records(job_id);
CREATE INDEX IF NOT EXISTS idx_cost_reports_tenant ON cost_reports(tenant_id);
CREATE INDEX IF NOT EXISTS idx_resource_utilizations_tenant ON resource_utilizations(tenant_id);
CREATE INDEX IF NOT EXISTS idx_idle_resources_tenant ON idle_resources(tenant_id);
CREATE INDEX IF NOT EXISTS idx_cost_suggestions_tenant ON cost_optimization_suggestions(tenant_id);
