-- ===========================================
-- 用户档案迁移脚本
-- 添加用户移民档案和联系方式表
-- ===========================================

-- ===========================================
-- 用户档案表 (user_profiles)
-- 存储用户移民相关的详细信息
-- ===========================================
CREATE TABLE IF NOT EXISTS user_profiles (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    tenant_id UUID NOT NULL,
    user_id UUID NOT NULL UNIQUE,

    -- 基本信息
    full_name VARCHAR(100),
    birth_date DATE,
    nationality VARCHAR(2),              -- ISO 3166-1 alpha-2
    current_country VARCHAR(2),
    current_city VARCHAR(100),
    marital_status VARCHAR(20),          -- SINGLE, MARRIED, DIVORCED, WIDOWED

    -- 移民意向
    target_countries TEXT[] DEFAULT '{}',
    immigration_types TEXT[] DEFAULT '{}', -- SKILLED, INVESTMENT, BUSINESS, STUDY, FAMILY, etc.
    planned_timeline VARCHAR(20),        -- WITHIN_6M, WITHIN_1Y, 1_2Y, 2_3Y, NO_PLAN
    primary_purpose TEXT,

    -- 教育背景
    highest_education VARCHAR(20),       -- HIGH_SCHOOL, ASSOCIATE, BACHELOR, MASTER, DOCTORATE
    education_records JSONB DEFAULT '[]',

    -- 工作经历
    current_occupation VARCHAR(100),
    total_work_years INT,
    work_records JSONB DEFAULT '[]',

    -- 语言能力
    language_scores JSONB DEFAULT '[]',

    -- 家庭信息
    family_members JSONB DEFAULT '[]',

    -- 资产信息
    net_worth_range VARCHAR(20),         -- UNDER_500K, 500K_1M, 1M_5M, 5M_10M, OVER_10M
    has_business_experience BOOLEAN DEFAULT FALSE,
    business_years INT,

    -- 其他
    has_overseas_experience BOOLEAN DEFAULT FALSE,
    has_criminal_record BOOLEAN,
    has_health_issues BOOLEAN,
    additional_notes TEXT,

    -- 完成度
    completion_percentage INT DEFAULT 0 CHECK (completion_percentage >= 0 AND completion_percentage <= 100),

    -- 时间戳
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

COMMENT ON TABLE user_profiles IS '用户档案表 - 存储用户移民相关的详细信息';
COMMENT ON COLUMN user_profiles.nationality IS '国籍，ISO 3166-1 alpha-2 代码';
COMMENT ON COLUMN user_profiles.target_countries IS '目标移民国家列表，ISO 3166-1 alpha-2 代码';
COMMENT ON COLUMN user_profiles.immigration_types IS '感兴趣的移民类型列表';
COMMENT ON COLUMN user_profiles.education_records IS '教育经历JSON数组，包含学历、专业、院校、毕业年份等';
COMMENT ON COLUMN user_profiles.work_records IS '工作经历JSON数组，包含职位、公司、行业、时间等';
COMMENT ON COLUMN user_profiles.language_scores IS '语言成绩JSON数组，包含雅思、托福、TEF等';
COMMENT ON COLUMN user_profiles.family_members IS '家庭成员JSON数组，包含配偶、子女等信息';
COMMENT ON COLUMN user_profiles.completion_percentage IS '档案完成度百分比';

-- 索引
CREATE INDEX idx_user_profiles_tenant ON user_profiles(tenant_id);
CREATE INDEX idx_user_profiles_user ON user_profiles(user_id);
CREATE INDEX idx_user_profiles_nationality ON user_profiles(tenant_id, nationality);
CREATE INDEX idx_user_profiles_target_countries ON user_profiles USING GIN(target_countries);
CREATE INDEX idx_user_profiles_immigration_types ON user_profiles USING GIN(immigration_types);
CREATE INDEX idx_user_profiles_completion ON user_profiles(tenant_id, completion_percentage DESC);

-- 更新时间触发器
CREATE TRIGGER update_user_profiles_updated_at
    BEFORE UPDATE ON user_profiles
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

-- ===========================================
-- 用户联系方式表 (user_contacts)
-- 存储用户绑定的联系方式，支持通知推送
-- ===========================================
CREATE TABLE IF NOT EXISTS user_contacts (
    id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
    tenant_id UUID NOT NULL,
    user_id UUID NOT NULL,

    -- 联系方式类型: EMAIL, WECHAT, WHATSAPP, TELEGRAM, LINE
    type VARCHAR(20) NOT NULL,
    -- 联系方式值（邮箱地址、openid、手机号等）
    value VARCHAR(255) NOT NULL,
    -- 显示名称（如微信昵称）
    display_name VARCHAR(100),

    -- 验证状态
    is_verified BOOLEAN DEFAULT FALSE,
    verified_at TIMESTAMP WITH TIME ZONE,

    -- 通知设置 (付费功能)
    notification_enabled BOOLEAN DEFAULT FALSE,
    enabled_notification_types TEXT[] DEFAULT '{}', -- POLICY_UPDATE, DEADLINE_REMINDER, APPOINTMENT, CASE_UPDATE, PROMOTION

    -- 验证相关
    verification_code VARCHAR(10),
    verification_expires_at TIMESTAMP WITH TIME ZONE,

    -- 时间戳
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),

    -- 同一用户同一类型只能有一个联系方式
    UNIQUE(tenant_id, user_id, type)
);

COMMENT ON TABLE user_contacts IS '用户联系方式表 - 存储用户绑定的联系方式，支持通知推送';
COMMENT ON COLUMN user_contacts.type IS '联系方式类型: EMAIL邮箱, WECHAT微信, WHATSAPP, TELEGRAM, LINE';
COMMENT ON COLUMN user_contacts.notification_enabled IS '是否启用通知推送（付费功能）';
COMMENT ON COLUMN user_contacts.enabled_notification_types IS '启用的通知类型列表';

-- 索引
CREATE INDEX idx_user_contacts_tenant ON user_contacts(tenant_id);
CREATE INDEX idx_user_contacts_user ON user_contacts(tenant_id, user_id);
CREATE INDEX idx_user_contacts_type ON user_contacts(tenant_id, user_id, type);
CREATE INDEX idx_user_contacts_verified ON user_contacts(tenant_id, is_verified);
CREATE INDEX idx_user_contacts_notification ON user_contacts(tenant_id, notification_enabled) WHERE notification_enabled = TRUE;

-- 更新时间触发器
CREATE TRIGGER update_user_contacts_updated_at
    BEFORE UPDATE ON user_contacts
    FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

-- ===========================================
-- 更新 init-db.sql 需要的默认数据（如果需要）
-- ===========================================
-- 暂无默认数据需要插入
