-- ============================================================================
-- trading-service 初始化 migration
-- 合并自: 20260111000000_init (只有一个，无需合并)
-- ============================================================================

-- CreateEnum
CREATE TYPE "OutboxStatus" AS ENUM ('PENDING', 'PUBLISHED', 'FAILED');

-- CreateTable
CREATE TABLE "trading_accounts" (
    "id" TEXT NOT NULL,
    "accountSequence" TEXT NOT NULL,
    "shareBalance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "cashBalance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "frozenShares" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "frozenCash" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "totalBought" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "totalSold" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "orders" (
    "id" TEXT NOT NULL,
    "orderNo" TEXT NOT NULL,
    "accountSequence" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "status" TEXT NOT NULL,
    "price" DECIMAL(30,18) NOT NULL,
    "quantity" DECIMAL(30,8) NOT NULL,
    "filledQuantity" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "remainingQuantity" DECIMAL(30,8) NOT NULL,
    "averagePrice" DECIMAL(30,18) NOT NULL DEFAULT 0,
    "totalAmount" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,
    "cancelledAt" TIMESTAMP(3),
    "completedAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "trades" (
    "id" TEXT NOT NULL,
    "tradeNo" TEXT NOT NULL,
    "buy_order_id" TEXT NOT NULL,
    "sell_order_id" TEXT NOT NULL,
    "buyer_sequence" TEXT NOT NULL,
    "seller_sequence" TEXT NOT NULL,
    "price" DECIMAL(30,18) NOT NULL,
    "quantity" DECIMAL(30,8) NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "trading_transactions" (
    "id" TEXT NOT NULL,
    "accountSequence" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "assetType" TEXT NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "balanceBefore" DECIMAL(30,8) NOT NULL,
    "balanceAfter" DECIMAL(30,8) NOT NULL,
    "referenceId" TEXT,
    "referenceType" TEXT,
    "counterparty_type" TEXT,
    "counterparty_account_seq" TEXT,
    "counterparty_user_id" TEXT,
    "memo" TEXT,
    "description" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "circulation_pools" (
    "id" TEXT NOT NULL,
    "totalShares" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "totalCash" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "totalInflow" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "totalOutflow" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updatedAt" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "circulation_pool_transactions" (
    "id" TEXT NOT NULL,
    "pool_id" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "assetType" TEXT NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "balance_before" DECIMAL(30,8) NOT NULL,
    "balance_after" DECIMAL(30,8) NOT NULL,
    "counterparty_type" TEXT,
    "counterparty_account_seq" TEXT,
    "counterparty_user_id" TEXT,
    "reference_id" TEXT,
    "reference_type" TEXT,
    "memo" TEXT,
    "metadata" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable (deprecated, kept for compatibility)
CREATE TABLE "pool_transactions" (
    "id" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "referenceId" TEXT,
    "description" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "minute_klines" (
    "id" TEXT NOT NULL,
    "minute" TIMESTAMP(3) NOT NULL,
    "open" DECIMAL(30,18) NOT NULL,
    "high" DECIMAL(30,18) NOT NULL,
    "low" DECIMAL(30,18) NOT NULL,
    "close" DECIMAL(30,18) NOT NULL,
    "volume" DECIMAL(30,8) NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "tradeCount" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "hour_klines" (
    "id" TEXT NOT NULL,
    "hour" TIMESTAMP(3) NOT NULL,
    "open" DECIMAL(30,18) NOT NULL,
    "high" DECIMAL(30,18) NOT NULL,
    "low" DECIMAL(30,18) NOT NULL,
    "close" DECIMAL(30,18) NOT NULL,
    "volume" DECIMAL(30,8) NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "tradeCount" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "day_klines" (
    "id" TEXT NOT NULL,
    "date" DATE NOT NULL,
    "open" DECIMAL(30,18) NOT NULL,
    "high" DECIMAL(30,18) NOT NULL,
    "low" DECIMAL(30,18) NOT NULL,
    "close" DECIMAL(30,18) NOT NULL,
    "volume" DECIMAL(30,8) NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "tradeCount" INTEGER NOT NULL DEFAULT 0,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateTable
CREATE TABLE "transfer_records" (
    "id" TEXT NOT NULL,
    "transferNo" TEXT NOT NULL,
    "accountSequence" TEXT NOT NULL,
    "direction" TEXT NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "status" TEXT NOT NULL,
    "miningTxId" TEXT,
    "errorMessage" TEXT,
    "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "completedAt" TIMESTAMP(3),

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

-- CreateTable
CREATE TABLE "outbox_events" (
    "id" TEXT NOT NULL,
    "aggregate_type" TEXT NOT NULL,
    "aggregate_id" TEXT NOT NULL,
    "event_type" TEXT NOT NULL,
    "payload" JSONB NOT NULL,
    "topic" TEXT NOT NULL DEFAULT 'trading.events',
    "key" TEXT,
    "status" "OutboxStatus" NOT NULL DEFAULT 'PENDING',
    "retry_count" INTEGER NOT NULL DEFAULT 0,
    "max_retries" INTEGER NOT NULL DEFAULT 10,
    "last_error" TEXT,
    "published_at" TIMESTAMP(3),
    "next_retry_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "trading_accounts_accountSequence_key" ON "trading_accounts"("accountSequence");

-- CreateIndex
CREATE UNIQUE INDEX "orders_orderNo_key" ON "orders"("orderNo");

-- CreateIndex
CREATE INDEX "orders_accountSequence_status_idx" ON "orders"("accountSequence", "status");

-- CreateIndex
CREATE INDEX "orders_type_status_price_idx" ON "orders"("type", "status", "price");

-- CreateIndex
CREATE INDEX "orders_createdAt_idx" ON "orders"("createdAt" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "trades_tradeNo_key" ON "trades"("tradeNo");

-- CreateIndex
CREATE INDEX "trades_buyer_sequence_idx" ON "trades"("buyer_sequence");

-- CreateIndex
CREATE INDEX "trades_seller_sequence_idx" ON "trades"("seller_sequence");

-- CreateIndex
CREATE INDEX "trades_created_at_idx" ON "trades"("created_at" DESC);

-- CreateIndex
CREATE INDEX "trading_transactions_accountSequence_createdAt_idx" ON "trading_transactions"("accountSequence", "createdAt" DESC);

-- CreateIndex
CREATE INDEX "trading_transactions_counterparty_account_seq_idx" ON "trading_transactions"("counterparty_account_seq");

-- CreateIndex
CREATE INDEX "trading_transactions_counterparty_user_id_idx" ON "trading_transactions"("counterparty_user_id");

-- CreateIndex
CREATE INDEX "circulation_pool_transactions_pool_id_created_at_idx" ON "circulation_pool_transactions"("pool_id", "created_at" DESC);

-- CreateIndex
CREATE INDEX "circulation_pool_transactions_type_assetType_idx" ON "circulation_pool_transactions"("type", "assetType");

-- CreateIndex
CREATE INDEX "circulation_pool_transactions_counterparty_account_seq_idx" ON "circulation_pool_transactions"("counterparty_account_seq");

-- CreateIndex
CREATE INDEX "circulation_pool_transactions_counterparty_user_id_idx" ON "circulation_pool_transactions"("counterparty_user_id");

-- CreateIndex
CREATE INDEX "circulation_pool_transactions_reference_id_idx" ON "circulation_pool_transactions"("reference_id");

-- CreateIndex
CREATE INDEX "circulation_pool_transactions_created_at_idx" ON "circulation_pool_transactions"("created_at" DESC);

-- CreateIndex
CREATE INDEX "pool_transactions_createdAt_idx" ON "pool_transactions"("createdAt" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "minute_klines_minute_key" ON "minute_klines"("minute");

-- CreateIndex
CREATE INDEX "minute_klines_minute_idx" ON "minute_klines"("minute" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "hour_klines_hour_key" ON "hour_klines"("hour");

-- CreateIndex
CREATE INDEX "hour_klines_hour_idx" ON "hour_klines"("hour" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "day_klines_date_key" ON "day_klines"("date");

-- CreateIndex
CREATE INDEX "day_klines_date_idx" ON "day_klines"("date" DESC);

-- CreateIndex
CREATE UNIQUE INDEX "transfer_records_transferNo_key" ON "transfer_records"("transferNo");

-- CreateIndex
CREATE INDEX "transfer_records_accountSequence_idx" ON "transfer_records"("accountSequence");

-- CreateIndex
CREATE INDEX "transfer_records_status_idx" ON "transfer_records"("status");

-- CreateIndex
CREATE INDEX "outbox_events_status_idx" ON "outbox_events"("status");

-- CreateIndex
CREATE INDEX "outbox_events_next_retry_at_idx" ON "outbox_events"("next_retry_at");

-- CreateIndex
CREATE INDEX "outbox_events_created_at_idx" ON "outbox_events"("created_at");

-- AddForeignKey
ALTER TABLE "orders" ADD CONSTRAINT "orders_accountSequence_fkey" FOREIGN KEY ("accountSequence") REFERENCES "trading_accounts"("accountSequence") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "trades" ADD CONSTRAINT "trades_buy_order_id_fkey" FOREIGN KEY ("buy_order_id") REFERENCES "orders"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "trading_transactions" ADD CONSTRAINT "trading_transactions_accountSequence_fkey" FOREIGN KEY ("accountSequence") REFERENCES "trading_accounts"("accountSequence") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "circulation_pool_transactions" ADD CONSTRAINT "circulation_pool_transactions_pool_id_fkey" FOREIGN KEY ("pool_id") REFERENCES "circulation_pools"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
