-- 行业配置表
CREATE TABLE IF NOT EXISTS industries (
    id TEXT PRIMARY KEY,                    -- "healthcare" | "education" | "garment" | "ecommerce"
    name TEXT NOT NULL,                     -- "医疗行政"
    icon TEXT NOT NULL DEFAULT '',          -- emoji 或图标标识
    description TEXT NOT NULL DEFAULT '',   -- 行业描述
    keywords JSONB NOT NULL DEFAULT '[]',   -- 行业关键词列表
    system_prompt TEXT NOT NULL DEFAULT '',  -- 行业 system prompt 片段
    cold_start_template TEXT NOT NULL DEFAULT '', -- 冷启动问候模板
    pain_seed_categories JSONB NOT NULL DEFAULT '[]', -- 痛点种子类别
    skill_priorities JSONB NOT NULL DEFAULT '[]',   -- 技能推荐优先级
    status TEXT NOT NULL DEFAULT 'active',  -- "active" | "disabled"
    source TEXT NOT NULL DEFAULT 'builtin', -- "builtin" | "admin"
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- 用户-行业关联表（多对多）
CREATE TABLE IF NOT EXISTS account_industries (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    account_id TEXT NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,
    industry_id TEXT NOT NULL REFERENCES industries(id) ON DELETE CASCADE,
    is_primary BOOLEAN NOT NULL DEFAULT false,
    custom_config JSONB,                    -- Admin 可覆盖的配置
    created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    CONSTRAINT uq_account_industry UNIQUE (account_id, industry_id)
);

-- 索引
CREATE INDEX IF NOT EXISTS idx_account_industries_account ON account_industries(account_id);
CREATE INDEX IF NOT EXISTS idx_account_industries_industry ON account_industries(industry_id);
CREATE INDEX IF NOT EXISTS idx_industries_status ON industries(status);
CREATE INDEX IF NOT EXISTS idx_industries_source ON industries(source);
