-- =============================================
-- Migration: Add system accounts support and recovery mnemonics
-- =============================================

-- AlterTable: monitored_addresses
-- Add new columns for system accounts and account_sequence
ALTER TABLE "monitored_addresses" ADD COLUMN "address_type" VARCHAR(20) NOT NULL DEFAULT 'USER';
ALTER TABLE "monitored_addresses" ADD COLUMN "account_sequence" VARCHAR(20);
ALTER TABLE "monitored_addresses" ADD COLUMN "system_account_type" VARCHAR(50);
ALTER TABLE "monitored_addresses" ADD COLUMN "system_account_id" BIGINT;
ALTER TABLE "monitored_addresses" ADD COLUMN "region_code" VARCHAR(10);

-- Make user_id nullable (system accounts don't have user_id)
ALTER TABLE "monitored_addresses" ALTER COLUMN "user_id" DROP NOT NULL;

-- AlterTable: deposit_transactions
-- Add new columns for system accounts and account_sequence
ALTER TABLE "deposit_transactions" ADD COLUMN "address_type" VARCHAR(20) NOT NULL DEFAULT 'USER';
ALTER TABLE "deposit_transactions" ADD COLUMN "account_sequence" VARCHAR(20);
ALTER TABLE "deposit_transactions" ADD COLUMN "system_account_type" VARCHAR(50);
ALTER TABLE "deposit_transactions" ADD COLUMN "system_account_id" BIGINT;

-- Make user_id nullable (system account deposits don't have user_id)
ALTER TABLE "deposit_transactions" ALTER COLUMN "user_id" DROP NOT NULL;

-- CreateTable: recovery_mnemonics
CREATE TABLE "recovery_mnemonics" (
    "id" BIGSERIAL NOT NULL,
    "account_sequence" VARCHAR(20) 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")
);

-- =============================================
-- CreateIndex: monitored_addresses (new indexes)
-- =============================================
CREATE INDEX "idx_account_sequence" ON "monitored_addresses"("account_sequence");
CREATE INDEX "idx_type_active" ON "monitored_addresses"("address_type", "is_active");
CREATE INDEX "idx_system_account_type" ON "monitored_addresses"("system_account_type");

-- Rename unique constraint to match schema
DROP INDEX IF EXISTS "monitored_addresses_chain_type_address_key";
CREATE UNIQUE INDEX "uk_chain_address" ON "monitored_addresses"("chain_type", "address");

-- =============================================
-- CreateIndex: deposit_transactions (new indexes)
-- =============================================
CREATE INDEX "idx_deposit_account" ON "deposit_transactions"("account_sequence");

-- =============================================
-- CreateIndex: recovery_mnemonics
-- =============================================
CREATE UNIQUE INDEX "uk_account_active_mnemonic" ON "recovery_mnemonics"("account_sequence", "status");
CREATE INDEX "idx_recovery_account" ON "recovery_mnemonics"("account_sequence");
CREATE INDEX "idx_recovery_public_key" ON "recovery_mnemonics"("public_key");
CREATE INDEX "idx_recovery_status" ON "recovery_mnemonics"("status");
