-- v1.2.0 实名认证 & 手机号登录 数据库升级脚本
-- 目标数据库：LiveForumDB（业务库）
-- =============================================

-- 1. T_Users 新增实名认证字段
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('T_Users') AND name = 'IsRealNameVerified')
BEGIN
    ALTER TABLE T_Users ADD IsRealNameVerified BIT NOT NULL DEFAULT 0;
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('T_Users') AND name = 'RealName')
BEGIN
    ALTER TABLE T_Users ADD RealName NVARCHAR(50) NULL;
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('T_Users') AND name = 'IdCardNumber')
BEGIN
    ALTER TABLE T_Users ADD IdCardNumber NVARCHAR(100) NULL;
END

IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('T_Users') AND name = 'RealNameVerifiedAt')
BEGIN
    ALTER TABLE T_Users ADD RealNameVerifiedAt DATETIME2 NULL;
END

-- 2. T_Users.PhoneNumber 添加索引
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE name = 'IX_T_Users_PhoneNumber' AND object_id = OBJECT_ID('T_Users'))
BEGIN
    CREATE INDEX IX_T_Users_PhoneNumber ON T_Users (PhoneNumber);
END

-- 3. 创建实名验证记录表
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID('T_RealNameVerifyLogs') AND type = 'U')
BEGIN
    CREATE TABLE T_RealNameVerifyLogs (
        Id BIGINT PRIMARY KEY IDENTITY(1,1),
        UserId BIGINT NOT NULL,
        RealName NVARCHAR(50) NOT NULL,
        IdCardNumberMasked NVARCHAR(20) NOT NULL,
        IsSuccess BIT NOT NULL DEFAULT 0,
        FailReason NVARCHAR(200) NULL,
        ClientIp NVARCHAR(50) NULL,
        CreatedAt DATETIME2 NOT NULL DEFAULT GETDATE()
    );

    CREATE INDEX IX_T_RealNameVerifyLogs_UserId_CreatedAt 
        ON T_RealNameVerifyLogs (UserId, CreatedAt);
END

-- 4. T_SystemSettings 新增实名认证配置
IF NOT EXISTS (SELECT 1 FROM T_SystemSettings WHERE SettingKey = 'real_name_enabled')
    INSERT INTO T_SystemSettings (SettingKey, SettingValue, Description, UpdatedAt) 
    VALUES ('real_name_enabled', 'true', N'实名认证功能开关', GETDATE());

IF NOT EXISTS (SELECT 1 FROM T_SystemSettings WHERE SettingKey = 'real_name_provider')
    INSERT INTO T_SystemSettings (SettingKey, SettingValue, Description, UpdatedAt) 
    VALUES ('real_name_provider', 'aliyun', N'实名认证服务商（aliyun/tencent）', GETDATE());

IF NOT EXISTS (SELECT 1 FROM T_SystemSettings WHERE SettingKey = 'real_name_daily_limit')
    INSERT INTO T_SystemSettings (SettingKey, SettingValue, Description, UpdatedAt) 
    VALUES ('real_name_daily_limit', '5', N'每用户每天最多验证次数', GETDATE());

IF NOT EXISTS (SELECT 1 FROM T_SystemSettings WHERE SettingKey = 'real_name_interval_seconds')
    INSERT INTO T_SystemSettings (SettingKey, SettingValue, Description, UpdatedAt) 
    VALUES ('real_name_interval_seconds', '60', N'两次验证最小间隔（秒）', GETDATE());

IF NOT EXISTS (SELECT 1 FROM T_SystemSettings WHERE SettingKey = 'aliyun_access_key_id')
    INSERT INTO T_SystemSettings (SettingKey, SettingValue, Description, UpdatedAt) 
    VALUES ('aliyun_access_key_id', '', N'阿里云 AccessKey ID', GETDATE());

IF NOT EXISTS (SELECT 1 FROM T_SystemSettings WHERE SettingKey = 'aliyun_access_key_secret')
    INSERT INTO T_SystemSettings (SettingKey, SettingValue, Description, UpdatedAt) 
    VALUES ('aliyun_access_key_secret', '', N'阿里云 AccessKey Secret', GETDATE());

PRINT 'v1.2.0 实名认证升级脚本执行完成';
