-- =============================================================================
-- 添加三层认证状态字段
-- 用于支持三层 KYC 认证流程:
--   层级1: 实名认证 (二要素: 姓名+身份证号)
--   层级2: 实人认证 (人脸活体检测)
--   层级3: KYC (证件照上传验证)
-- =============================================================================

-- 层级1: 实名认证 (二要素: 姓名+身份证号)
-- 注意: real_name, id_card_number 字段已在 init migration 中创建
ALTER TABLE "user_accounts" ADD COLUMN IF NOT EXISTS "real_name_verified" BOOLEAN NOT NULL DEFAULT false;
ALTER TABLE "user_accounts" ADD COLUMN IF NOT EXISTS "real_name_verified_at" TIMESTAMP(3);

-- 层级2: 实人认证 (人脸活体检测)
ALTER TABLE "user_accounts" ADD COLUMN IF NOT EXISTS "face_verified" BOOLEAN NOT NULL DEFAULT false;
ALTER TABLE "user_accounts" ADD COLUMN IF NOT EXISTS "face_verified_at" TIMESTAMP(3);
ALTER TABLE "user_accounts" ADD COLUMN IF NOT EXISTS "face_certify_id" VARCHAR(100);

-- 层级3: KYC (证件照上传验证)
-- 注意: id_card_front_url, id_card_back_url, kyc_verified_at 字段已在 init migration 中创建
ALTER TABLE "user_accounts" ADD COLUMN IF NOT EXISTS "kyc_verified" BOOLEAN NOT NULL DEFAULT false;

-- =============================================================================
-- 创建 KYC 配置表
-- 用于管理后台控制各认证步骤的开关
-- =============================================================================
CREATE TABLE IF NOT EXISTS "kyc_configs" (
    "id" SERIAL NOT NULL,
    "config_key" VARCHAR(50) NOT NULL,
    "config_value" VARCHAR(100) NOT NULL,
    "description" VARCHAR(500),
    "updated_by" VARCHAR(50),
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "kyc_configs_pkey" PRIMARY KEY ("id")
);

-- 添加唯一索引
CREATE UNIQUE INDEX IF NOT EXISTS "kyc_configs_config_key_key" ON "kyc_configs"("config_key");

-- 插入默认配置 (三层认证默认都开启)
INSERT INTO "kyc_configs" ("config_key", "config_value", "description", "updated_by")
VALUES
    ('kyc.level1.enabled', 'true', '层级1: 实名认证(二要素)开关', 'system'),
    ('kyc.level2.enabled', 'true', '层级2: 实人认证(人脸活体)开关', 'system'),
    ('kyc.level3.enabled', 'true', '层级3: KYC(证件照)开关', 'system')
ON CONFLICT ("config_key") DO NOTHING;
