-- CreateTable: user_accounts - 用户账户主表
CREATE TABLE "user_accounts" (
    "user_id" BIGSERIAL NOT NULL,
    "account_sequence" VARCHAR(12) NOT NULL,
    "phone_number" VARCHAR(20),
    "password_hash" VARCHAR(100),
    "nickname" VARCHAR(100) NOT NULL,
    "avatar_url" TEXT,
    "inviter_sequence" VARCHAR(12),
    "referral_code" VARCHAR(10) NOT NULL,
    "kyc_status" VARCHAR(20) NOT NULL DEFAULT 'NOT_VERIFIED',
    "real_name" VARCHAR(100),
    "id_card_number" VARCHAR(20),
    "id_card_front_url" VARCHAR(500),
    "id_card_back_url" VARCHAR(500),
    "kyc_verified_at" TIMESTAMP(3),
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "registered_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "last_login_at" TIMESTAMP(3),
    "updated_at" TIMESTAMP(3) NOT NULL,

    CONSTRAINT "user_accounts_pkey" PRIMARY KEY ("user_id")
);

-- CreateTable: user_devices - 用户设备信息
CREATE TABLE "user_devices" (
    "id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "device_id" VARCHAR(100) NOT NULL,
    "device_name" VARCHAR(100),
    "device_info" JSONB,
    "platform" VARCHAR(20),
    "device_model" VARCHAR(100),
    "os_version" VARCHAR(50),
    "app_version" VARCHAR(20),
    "screen_width" INTEGER,
    "screen_height" INTEGER,
    "locale" VARCHAR(10),
    "timezone" VARCHAR(50),
    "added_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "last_active_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable: wallet_addresses - 钱包地址绑定
CREATE TABLE "wallet_addresses" (
    "address_id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "chain_type" VARCHAR(20) NOT NULL,
    "address" VARCHAR(100) NOT NULL,
    "public_key" VARCHAR(130) NOT NULL,
    "address_digest" VARCHAR(66) NOT NULL,
    "mpc_signature_r" VARCHAR(66) NOT NULL,
    "mpc_signature_s" VARCHAR(66) NOT NULL,
    "mpc_signature_v" INTEGER NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "bound_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "wallet_addresses_pkey" PRIMARY KEY ("address_id")
);

-- CreateTable: account_sequence_generator - 账户序列号生成器
CREATE TABLE "account_sequence_generator" (
    "id" SERIAL NOT NULL,
    "date_key" VARCHAR(6) NOT NULL,
    "current_sequence" INTEGER NOT NULL DEFAULT 0,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable: user_events - 用户事件日志
CREATE TABLE "user_events" (
    "event_id" BIGSERIAL NOT NULL,
    "event_type" VARCHAR(50) NOT NULL,
    "aggregate_id" VARCHAR(100) NOT NULL,
    "aggregate_type" VARCHAR(50) NOT NULL,
    "event_data" JSONB NOT NULL,
    "user_id" BIGINT,
    "occurred_at" TIMESTAMP(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "version" INTEGER NOT NULL DEFAULT 1,

    CONSTRAINT "user_events_pkey" PRIMARY KEY ("event_id")
);

-- CreateTable: device_tokens - 设备 Refresh Token
CREATE TABLE "device_tokens" (
    "id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "device_id" VARCHAR(100) NOT NULL,
    "refresh_token_hash" VARCHAR(64) NOT NULL,
    "expires_at" TIMESTAMP(3) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "revoked_at" TIMESTAMP(3),

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

-- CreateTable: dead_letter_events - 死信队列
CREATE TABLE "dead_letter_events" (
    "id" BIGSERIAL NOT NULL,
    "topic" VARCHAR(100) NOT NULL,
    "event_id" VARCHAR(100) NOT NULL,
    "event_type" VARCHAR(50) NOT NULL,
    "aggregate_id" VARCHAR(100) NOT NULL,
    "aggregate_type" VARCHAR(50) NOT NULL,
    "payload" JSONB,
    "error_message" TEXT NOT NULL,
    "error_stack" TEXT,
    "retry_count" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "processed_at" TIMESTAMP(3),

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

-- CreateTable: sms_codes - 短信验证码
CREATE TABLE "sms_codes" (
    "id" BIGSERIAL NOT NULL,
    "phone_number" VARCHAR(20) NOT NULL,
    "code" VARCHAR(10) NOT NULL,
    "purpose" VARCHAR(50) NOT NULL,
    "expires_at" TIMESTAMP(3) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "used_at" TIMESTAMP(3),

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

-- CreateTable: mpc_key_shares - MPC 密钥分片
CREATE TABLE "mpc_key_shares" (
    "share_id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "public_key" VARCHAR(130) NOT NULL,
    "party_index" INTEGER NOT NULL,
    "threshold" INTEGER NOT NULL DEFAULT 2,
    "total_parties" INTEGER NOT NULL DEFAULT 3,
    "encrypted_share_data" TEXT NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "rotated_at" TIMESTAMP(3),

    CONSTRAINT "mpc_key_shares_pkey" PRIMARY KEY ("share_id")
);

-- CreateTable: mpc_sessions - MPC 会话记录
CREATE TABLE "mpc_sessions" (
    "session_id" VARCHAR(50) NOT NULL,
    "session_type" VARCHAR(20) NOT NULL,
    "user_id" BIGINT,
    "public_key" VARCHAR(130),
    "status" VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    "error_message" TEXT,
    "message_hash" VARCHAR(66),
    "signature_r" VARCHAR(66),
    "signature_s" VARCHAR(66),
    "signature_v" INTEGER,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completed_at" TIMESTAMP(3),

    CONSTRAINT "mpc_sessions_pkey" PRIMARY KEY ("session_id")
);

-- CreateTable: recovery_mnemonics - 助记词恢复
CREATE TABLE "recovery_mnemonics" (
    "id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "public_key" VARCHAR(130) NOT NULL,
    "encrypted_mnemonic" TEXT NOT NULL,
    "mnemonic_hash" VARCHAR(64) NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "is_backed_up" BOOLEAN NOT NULL DEFAULT false,
    "revoked_at" TIMESTAMP(3),
    "revoked_reason" VARCHAR(200),
    "replaced_by_id" BIGINT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable: user_totp - TOTP 二次验证
CREATE TABLE "user_totp" (
    "id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "encrypted_secret" VARCHAR(100) NOT NULL,
    "is_enabled" BOOLEAN NOT NULL DEFAULT false,
    "is_verified" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "enabled_at" TIMESTAMP(3),
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable: referral_links - 推荐链接追踪
CREATE TABLE "referral_links" (
    "link_id" BIGSERIAL NOT NULL,
    "user_id" BIGINT NOT NULL,
    "referral_code" VARCHAR(10) NOT NULL,
    "short_code" VARCHAR(10) NOT NULL,
    "channel" VARCHAR(50),
    "campaign_id" VARCHAR(50),
    "click_count" INTEGER NOT NULL DEFAULT 0,
    "register_count" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "expires_at" TIMESTAMP(3),

    CONSTRAINT "referral_links_pkey" PRIMARY KEY ("link_id")
);

-- CreateTable: admin_accounts - 管理员账户
CREATE TABLE "admin_accounts" (
    "id" BIGSERIAL NOT NULL,
    "email" VARCHAR(100) NOT NULL,
    "password_hash" VARCHAR(100) NOT NULL,
    "nickname" VARCHAR(100) NOT NULL,
    "role" VARCHAR(20) NOT NULL DEFAULT 'admin',
    "status" VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "last_login_at" TIMESTAMP(3),
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable: outbox_events - Outbox Pattern 事件表
CREATE TABLE "outbox_events" (
    "outbox_id" BIGSERIAL NOT NULL,
    "event_type" VARCHAR(100) NOT NULL,
    "topic" VARCHAR(100) NOT NULL,
    "key" VARCHAR(200) NOT NULL,
    "payload" JSONB NOT NULL,
    "aggregate_id" VARCHAR(100) NOT NULL,
    "aggregate_type" VARCHAR(50) NOT NULL,
    "status" VARCHAR(20) NOT NULL DEFAULT 'PENDING',
    "retry_count" INTEGER NOT NULL DEFAULT 0,
    "max_retries" INTEGER NOT NULL DEFAULT 5,
    "last_error" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "published_at" TIMESTAMP(3),
    "next_retry_at" TIMESTAMP(3),

    CONSTRAINT "outbox_events_pkey" PRIMARY KEY ("outbox_id")
);

-- ============================================
-- CreateIndex: user_accounts
-- ============================================
CREATE UNIQUE INDEX "user_accounts_account_sequence_key" ON "user_accounts"("account_sequence");
CREATE UNIQUE INDEX "user_accounts_phone_number_key" ON "user_accounts"("phone_number");
CREATE UNIQUE INDEX "user_accounts_referral_code_key" ON "user_accounts"("referral_code");
CREATE INDEX "idx_phone" ON "user_accounts"("phone_number");
CREATE INDEX "idx_sequence" ON "user_accounts"("account_sequence");
CREATE INDEX "idx_referral_code" ON "user_accounts"("referral_code");
CREATE INDEX "idx_inviter" ON "user_accounts"("inviter_sequence");
CREATE INDEX "idx_kyc_status" ON "user_accounts"("kyc_status");
CREATE INDEX "idx_status" ON "user_accounts"("status");

-- ============================================
-- CreateIndex: user_devices
-- ============================================
CREATE UNIQUE INDEX "uk_user_device" ON "user_devices"("user_id", "device_id");
CREATE INDEX "idx_device" ON "user_devices"("device_id");
CREATE INDEX "idx_user" ON "user_devices"("user_id");
CREATE INDEX "idx_last_active" ON "user_devices"("last_active_at");
CREATE INDEX "idx_platform" ON "user_devices"("platform");

-- ============================================
-- CreateIndex: wallet_addresses
-- ============================================
CREATE UNIQUE INDEX "uk_user_chain" ON "wallet_addresses"("user_id", "chain_type");
CREATE UNIQUE INDEX "uk_chain_address" ON "wallet_addresses"("chain_type", "address");
CREATE INDEX "idx_wallet_user" ON "wallet_addresses"("user_id");
CREATE INDEX "idx_address" ON "wallet_addresses"("address");
CREATE INDEX "idx_public_key" ON "wallet_addresses"("public_key");

-- ============================================
-- CreateIndex: account_sequence_generator
-- ============================================
CREATE UNIQUE INDEX "account_sequence_generator_date_key_key" ON "account_sequence_generator"("date_key");

-- ============================================
-- CreateIndex: user_events
-- ============================================
CREATE INDEX "idx_aggregate" ON "user_events"("aggregate_type", "aggregate_id");
CREATE INDEX "idx_event_type" ON "user_events"("event_type");
CREATE INDEX "idx_event_user" ON "user_events"("user_id");
CREATE INDEX "idx_occurred" ON "user_events"("occurred_at");

-- ============================================
-- CreateIndex: device_tokens
-- ============================================
CREATE UNIQUE INDEX "device_tokens_refresh_token_hash_key" ON "device_tokens"("refresh_token_hash");
CREATE INDEX "idx_user_device_token" ON "device_tokens"("user_id", "device_id");
CREATE INDEX "idx_expires" ON "device_tokens"("expires_at");

-- ============================================
-- CreateIndex: dead_letter_events
-- ============================================
CREATE INDEX "idx_topic" ON "dead_letter_events"("topic");
CREATE INDEX "idx_dead_letter_event_type" ON "dead_letter_events"("event_type");
CREATE INDEX "idx_dead_letter_created" ON "dead_letter_events"("created_at");
CREATE INDEX "idx_processed" ON "dead_letter_events"("processed_at");

-- ============================================
-- CreateIndex: sms_codes
-- ============================================
CREATE INDEX "idx_phone_purpose" ON "sms_codes"("phone_number", "purpose");
CREATE INDEX "idx_sms_expires" ON "sms_codes"("expires_at");

-- ============================================
-- CreateIndex: mpc_key_shares
-- ============================================
CREATE UNIQUE INDEX "mpc_key_shares_user_id_key" ON "mpc_key_shares"("user_id");
CREATE UNIQUE INDEX "mpc_key_shares_public_key_key" ON "mpc_key_shares"("public_key");
CREATE INDEX "idx_mpc_public_key" ON "mpc_key_shares"("public_key");
CREATE INDEX "idx_mpc_status" ON "mpc_key_shares"("status");

-- ============================================
-- CreateIndex: mpc_sessions
-- ============================================
CREATE INDEX "idx_session_type" ON "mpc_sessions"("session_type");
CREATE INDEX "idx_session_user" ON "mpc_sessions"("user_id");
CREATE INDEX "idx_session_status" ON "mpc_sessions"("status");
CREATE INDEX "idx_session_created" ON "mpc_sessions"("created_at");

-- ============================================
-- CreateIndex: recovery_mnemonics
-- ============================================
CREATE UNIQUE INDEX "uk_user_active_mnemonic" ON "recovery_mnemonics"("user_id", "status");
CREATE INDEX "idx_recovery_user" ON "recovery_mnemonics"("user_id");
CREATE INDEX "idx_recovery_public_key" ON "recovery_mnemonics"("public_key");
CREATE INDEX "idx_recovery_status" ON "recovery_mnemonics"("status");

-- ============================================
-- CreateIndex: user_totp
-- ============================================
CREATE UNIQUE INDEX "user_totp_user_id_key" ON "user_totp"("user_id");
CREATE INDEX "idx_totp_user" ON "user_totp"("user_id");

-- ============================================
-- CreateIndex: referral_links
-- ============================================
CREATE UNIQUE INDEX "referral_links_short_code_key" ON "referral_links"("short_code");
CREATE INDEX "idx_referral_link_user" ON "referral_links"("user_id");
CREATE INDEX "idx_referral_link_code" ON "referral_links"("referral_code");
CREATE INDEX "idx_referral_link_channel" ON "referral_links"("channel");
CREATE INDEX "idx_referral_link_created" ON "referral_links"("created_at");

-- ============================================
-- CreateIndex: admin_accounts
-- ============================================
CREATE UNIQUE INDEX "admin_accounts_email_key" ON "admin_accounts"("email");
CREATE INDEX "idx_admin_email" ON "admin_accounts"("email");
CREATE INDEX "idx_admin_role" ON "admin_accounts"("role");
CREATE INDEX "idx_admin_status" ON "admin_accounts"("status");

-- ============================================
-- CreateIndex: outbox_events
-- ============================================
CREATE INDEX "outbox_events_status_created_at_idx" ON "outbox_events"("status", "created_at");
CREATE INDEX "outbox_events_status_next_retry_at_idx" ON "outbox_events"("status", "next_retry_at");
CREATE INDEX "outbox_events_aggregate_type_aggregate_id_idx" ON "outbox_events"("aggregate_type", "aggregate_id");
CREATE INDEX "outbox_events_topic_idx" ON "outbox_events"("topic");

-- ============================================
-- AddForeignKey: user_devices -> user_accounts
-- ============================================
ALTER TABLE "user_devices" ADD CONSTRAINT "user_devices_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "user_accounts"("user_id") ON DELETE CASCADE ON UPDATE CASCADE;

-- ============================================
-- AddForeignKey: wallet_addresses -> user_accounts
-- ============================================
ALTER TABLE "wallet_addresses" ADD CONSTRAINT "wallet_addresses_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "user_accounts"("user_id") ON DELETE CASCADE ON UPDATE CASCADE;

-- ============================================
-- Set user_id sequence to start from 10
-- This reserves user IDs 1-9 for system accounts
-- ============================================
DO $$
DECLARE
    current_val BIGINT;
BEGIN
    SELECT last_value INTO current_val FROM user_accounts_user_id_seq;
    IF current_val < 10 THEN
        PERFORM setval('user_accounts_user_id_seq', 10, false);
        RAISE NOTICE 'Sequence reset to start from 10';
    ELSE
        RAISE NOTICE 'Sequence already at % (>= 10), no reset needed', current_val;
    END IF;
END $$;

-- ============================================
-- Insert System Seed User (GENESIS)
-- userId: 1, accountSequence: SYSTEM00001, referralCode: GENESIS
-- ============================================
INSERT INTO "user_accounts" (
  "user_id",
  "account_sequence",
  "referral_code",
  "nickname",
  "phone_number",
  "kyc_status",
  "status",
  "registered_at",
  "updated_at"
) VALUES (
  1,
  'SYSTEM00001',
  'GENESIS',
  '系统',
  NULL,
  'NOT_VERIFIED',
  'ACTIVE',
  CURRENT_TIMESTAMP,
  CURRENT_TIMESTAMP
) ON CONFLICT ("user_id") DO NOTHING;
