-- =============================================================================
-- System Maintenance & Co-Managed Wallet Migration
-- 系统维护公告 + 共管钱包系统
-- =============================================================================

-- -----------------------------------------------------------------------------
-- 1. 系统维护公告表
-- -----------------------------------------------------------------------------

CREATE TABLE "system_maintenances" (
    "id" TEXT NOT NULL,
    "title" VARCHAR(100) NOT NULL,
    "message" TEXT NOT NULL,
    "start_time" TIMESTAMP(3) NOT NULL,
    "end_time" TIMESTAMP(3) NOT NULL,
    "is_active" BOOLEAN NOT NULL DEFAULT false,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,
    "created_by" TEXT NOT NULL,
    "updated_by" TEXT,

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

CREATE INDEX "system_maintenances_is_active_idx" ON "system_maintenances"("is_active");
CREATE INDEX "system_maintenances_start_time_end_time_idx" ON "system_maintenances"("start_time", "end_time");

-- -----------------------------------------------------------------------------
-- 2. 共管钱包会话状态枚举
-- -----------------------------------------------------------------------------

CREATE TYPE "WalletSessionStatus" AS ENUM ('WAITING', 'READY', 'PROCESSING', 'COMPLETED', 'FAILED', 'CANCELLED');

-- -----------------------------------------------------------------------------
-- 3. 共管钱包会话表
-- -----------------------------------------------------------------------------

CREATE TABLE "co_managed_wallet_sessions" (
    "id" TEXT NOT NULL,
    "wallet_name" VARCHAR(100) NOT NULL,
    "threshold_t" INTEGER NOT NULL,
    "threshold_n" INTEGER NOT NULL,
    "invite_code" VARCHAR(20) NOT NULL,
    "status" "WalletSessionStatus" NOT NULL DEFAULT 'WAITING',
    "participants" TEXT NOT NULL,
    "current_round" INTEGER NOT NULL DEFAULT 0,
    "total_rounds" INTEGER NOT NULL DEFAULT 3,
    "public_key" VARCHAR(200),
    "error" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,
    "created_by" VARCHAR(100) NOT NULL,

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

CREATE UNIQUE INDEX "co_managed_wallet_sessions_invite_code_key" ON "co_managed_wallet_sessions"("invite_code");
CREATE INDEX "co_managed_wallet_sessions_invite_code_idx" ON "co_managed_wallet_sessions"("invite_code");
CREATE INDEX "co_managed_wallet_sessions_status_idx" ON "co_managed_wallet_sessions"("status");
CREATE INDEX "co_managed_wallet_sessions_created_by_idx" ON "co_managed_wallet_sessions"("created_by");
CREATE INDEX "co_managed_wallet_sessions_created_at_idx" ON "co_managed_wallet_sessions"("created_at");

-- -----------------------------------------------------------------------------
-- 4. 共管钱包表
-- -----------------------------------------------------------------------------

CREATE TABLE "co_managed_wallets" (
    "id" TEXT NOT NULL,
    "session_id" TEXT NOT NULL,
    "name" VARCHAR(100) NOT NULL,
    "public_key" VARCHAR(200) NOT NULL,
    "threshold_t" INTEGER NOT NULL,
    "threshold_n" INTEGER NOT NULL,
    "participants" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "created_by" VARCHAR(100) NOT NULL,

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

CREATE UNIQUE INDEX "co_managed_wallets_session_id_key" ON "co_managed_wallets"("session_id");
CREATE INDEX "co_managed_wallets_session_id_idx" ON "co_managed_wallets"("session_id");
CREATE INDEX "co_managed_wallets_public_key_idx" ON "co_managed_wallets"("public_key");
CREATE INDEX "co_managed_wallets_created_by_idx" ON "co_managed_wallets"("created_by");
CREATE INDEX "co_managed_wallets_created_at_idx" ON "co_managed_wallets"("created_at");
