-- AlterTable: 添加手机验证状态和 KYC 增强字段
ALTER TABLE "user_accounts" ADD COLUMN "phone_verified" BOOLEAN NOT NULL DEFAULT true;
ALTER TABLE "user_accounts" ADD COLUMN "phone_verified_at" TIMESTAMP(3);
ALTER TABLE "user_accounts" ADD COLUMN "kyc_provider" VARCHAR(50);
ALTER TABLE "user_accounts" ADD COLUMN "kyc_request_id" VARCHAR(100);
ALTER TABLE "user_accounts" ADD COLUMN "kyc_rejected_reason" VARCHAR(500);

-- 修改 id_card_number 列长度以支持加密存储
ALTER TABLE "user_accounts" ALTER COLUMN "id_card_number" TYPE VARCHAR(50);

-- 修改 kyc_status 默认值
ALTER TABLE "user_accounts" ALTER COLUMN "kyc_status" SET DEFAULT 'NOT_STARTED';

-- 更新现有数据的 kyc_status
UPDATE "user_accounts" SET "kyc_status" = 'NOT_STARTED' WHERE "kyc_status" = 'NOT_VERIFIED';

-- CreateTable: KYC 验证尝试记录表
CREATE TABLE "kyc_verification_attempts" (
    "id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "verification_type" VARCHAR(20) NOT NULL,
    "provider" VARCHAR(50),
    "request_id" VARCHAR(100),
    "input_data" JSONB,
    "status" VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    "failure_reason" VARCHAR(500),
    "response_data" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completed_at" TIMESTAMP(3),

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

-- CreateIndex
CREATE INDEX "idx_kyc_attempt_user" ON "kyc_verification_attempts"("user_id");
CREATE INDEX "idx_kyc_attempt_status" ON "kyc_verification_attempts"("status");
CREATE INDEX "idx_kyc_request_id" ON "kyc_verification_attempts"("request_id");
CREATE INDEX "idx_kyc_attempt_created" ON "kyc_verification_attempts"("created_at");
