-- ============================================================================
-- 补齐 schema 与 migration 的差异
-- 新增: payment_proof_url 字段, bot_purchased 索引,
--       做市商充提记录表 (market_maker_deposits, market_maker_withdraws)
-- ============================================================================

-- 1. c2c_orders: 补充 payment_proof_url 字段
ALTER TABLE "c2c_orders" ADD COLUMN "payment_proof_url" TEXT;

-- 2. c2c_orders: 补充 bot_purchased 索引
CREATE INDEX "c2c_orders_bot_purchased_idx" ON "c2c_orders"("bot_purchased");

-- 3. 创建 MarketMakerAssetType 枚举
CREATE TYPE "MarketMakerAssetType" AS ENUM ('EUSDT', 'FUSDT');

-- 4. 创建 MarketMakerDepositStatus 枚举
CREATE TYPE "MarketMakerDepositStatus" AS ENUM ('DETECTED', 'CONFIRMING', 'CONFIRMED', 'CREDITED', 'FAILED');

-- 5. 创建 MarketMakerWithdrawStatus 枚举
CREATE TYPE "MarketMakerWithdrawStatus" AS ENUM ('PENDING', 'SIGNING', 'SIGNED', 'BROADCAST', 'CONFIRMING', 'COMPLETED', 'FAILED', 'CANCELLED');

-- 6. 创建 market_maker_deposits 表
CREATE TABLE "market_maker_deposits" (
    "id" TEXT NOT NULL,
    "market_maker_id" TEXT NOT NULL,
    "chain_type" VARCHAR(20) NOT NULL,
    "tx_hash" VARCHAR(66) NOT NULL,
    "block_number" BIGINT NOT NULL,
    "block_timestamp" TIMESTAMP(3) NOT NULL,
    "asset_type" "MarketMakerAssetType" NOT NULL,
    "token_contract" VARCHAR(42) NOT NULL,
    "from_address" VARCHAR(42) NOT NULL,
    "to_address" VARCHAR(42) NOT NULL,
    "amount" DECIMAL(36,8) NOT NULL,
    "amount_raw" DECIMAL(78,0) NOT NULL,
    "confirmations" INTEGER NOT NULL DEFAULT 0,
    "required_confirms" INTEGER NOT NULL DEFAULT 12,
    "status" "MarketMakerDepositStatus" NOT NULL DEFAULT 'DETECTED',
    "credited_at" TIMESTAMP(3),
    "credited_amount" DECIMAL(36,8),
    "ledger_id" TEXT,
    "error_message" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX "market_maker_deposits_tx_hash_key" ON "market_maker_deposits"("tx_hash");
CREATE INDEX "market_maker_deposits_market_maker_id_idx" ON "market_maker_deposits"("market_maker_id");
CREATE INDEX "market_maker_deposits_status_idx" ON "market_maker_deposits"("status");
CREATE INDEX "market_maker_deposits_chain_type_status_idx" ON "market_maker_deposits"("chain_type", "status");
CREATE INDEX "market_maker_deposits_asset_type_idx" ON "market_maker_deposits"("asset_type");
CREATE INDEX "market_maker_deposits_block_number_idx" ON "market_maker_deposits"("block_number");
CREATE INDEX "market_maker_deposits_created_at_idx" ON "market_maker_deposits"("created_at" DESC);

-- AddForeignKey
ALTER TABLE "market_maker_deposits" ADD CONSTRAINT "market_maker_deposits_market_maker_id_fkey" FOREIGN KEY ("market_maker_id") REFERENCES "market_maker_configs"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- 7. 创建 market_maker_withdraws 表
CREATE TABLE "market_maker_withdraws" (
    "id" TEXT NOT NULL,
    "withdraw_no" TEXT NOT NULL,
    "market_maker_id" TEXT NOT NULL,
    "asset_type" "MarketMakerAssetType" NOT NULL,
    "amount" DECIMAL(36,8) NOT NULL,
    "chain_type" VARCHAR(20) NOT NULL,
    "token_contract" VARCHAR(42) NOT NULL,
    "from_address" VARCHAR(42) NOT NULL,
    "to_address" VARCHAR(42) NOT NULL,
    "tx_hash" VARCHAR(66),
    "block_number" BIGINT,
    "confirmations" INTEGER NOT NULL DEFAULT 0,
    "gas_used" DECIMAL(36,0),
    "gas_price" DECIMAL(36,0),
    "status" "MarketMakerWithdrawStatus" NOT NULL DEFAULT 'PENDING',
    "ledger_id" TEXT,
    "error_message" TEXT,
    "retry_count" INTEGER NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,
    "signed_at" TIMESTAMP(3),
    "broadcast_at" TIMESTAMP(3),
    "completed_at" TIMESTAMP(3),
    "cancelled_at" TIMESTAMP(3),

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

-- CreateIndex
CREATE UNIQUE INDEX "market_maker_withdraws_withdraw_no_key" ON "market_maker_withdraws"("withdraw_no");
CREATE INDEX "market_maker_withdraws_market_maker_id_idx" ON "market_maker_withdraws"("market_maker_id");
CREATE INDEX "market_maker_withdraws_status_idx" ON "market_maker_withdraws"("status");
CREATE INDEX "market_maker_withdraws_asset_type_idx" ON "market_maker_withdraws"("asset_type");
CREATE INDEX "market_maker_withdraws_tx_hash_idx" ON "market_maker_withdraws"("tx_hash");
CREATE INDEX "market_maker_withdraws_created_at_idx" ON "market_maker_withdraws"("created_at" DESC);

-- AddForeignKey
ALTER TABLE "market_maker_withdraws" ADD CONSTRAINT "market_maker_withdraws_market_maker_id_fkey" FOREIGN KEY ("market_maker_id") REFERENCES "market_maker_configs"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
