-- ============================================================================
-- trading-service 添加交易销毁系统
-- 包含：交易配置、黑洞账户、积分股池、价格快照、订单销毁字段
-- ============================================================================

-- ==================== 交易配置表 ====================

-- CreateTable
CREATE TABLE "trading_configs" (
    "id" TEXT NOT NULL,
    "total_shares" DECIMAL(30,8) NOT NULL DEFAULT 10002000000,
    "burn_target" DECIMAL(30,8) NOT NULL DEFAULT 10000000000,
    "burn_period_minutes" INTEGER NOT NULL DEFAULT 2102400,
    "minute_burn_rate" DECIMAL(30,18) NOT NULL DEFAULT 4756.468797564687,
    "is_active" BOOLEAN NOT NULL DEFAULT false,
    "activated_at" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- ==================== 黑洞账户（销毁池）====================

-- CreateTable
CREATE TABLE "black_holes" (
    "id" TEXT NOT NULL,
    "total_burned" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "target_burn" DECIMAL(30,8) NOT NULL,
    "remaining_burn" DECIMAL(30,8) NOT NULL,
    "last_burn_minute" TIMESTAMP(3),
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "burn_records" (
    "id" TEXT NOT NULL,
    "black_hole_id" TEXT NOT NULL,
    "burn_minute" TIMESTAMP(3) NOT NULL,
    "burn_amount" DECIMAL(30,18) NOT NULL,
    "remaining_target" DECIMAL(30,8) NOT NULL,
    "source_type" TEXT,
    "source_account_seq" TEXT,
    "source_order_no" TEXT,
    "memo" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "burn_records_burn_minute_idx" ON "burn_records"("burn_minute");

-- CreateIndex
CREATE INDEX "burn_records_source_account_seq_idx" ON "burn_records"("source_account_seq");

-- CreateIndex
CREATE INDEX "burn_records_source_order_no_idx" ON "burn_records"("source_order_no");

-- CreateIndex
CREATE INDEX "burn_records_source_type_idx" ON "burn_records"("source_type");

-- AddForeignKey
ALTER TABLE "burn_records" ADD CONSTRAINT "burn_records_black_hole_id_fkey" FOREIGN KEY ("black_hole_id") REFERENCES "black_holes"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- ==================== 积分股池（绿积分池）====================

-- CreateTable
CREATE TABLE "share_pools" (
    "id" TEXT NOT NULL,
    "green_points" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_inflow" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_outflow" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateTable
CREATE TABLE "share_pool_transactions" (
    "id" TEXT NOT NULL,
    "pool_id" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "balance_before" DECIMAL(30,8) NOT NULL,
    "balance_after" DECIMAL(30,8) NOT NULL,
    "reference_id" TEXT,
    "reference_type" TEXT,
    "memo" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE INDEX "share_pool_transactions_pool_id_created_at_idx" ON "share_pool_transactions"("pool_id", "created_at" DESC);

-- AddForeignKey
ALTER TABLE "share_pool_transactions" ADD CONSTRAINT "share_pool_transactions_pool_id_fkey" FOREIGN KEY ("pool_id") REFERENCES "share_pools"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- ==================== 价格快照 ====================

-- CreateTable
CREATE TABLE "price_snapshots" (
    "id" TEXT NOT NULL,
    "snapshot_time" TIMESTAMP(3) NOT NULL,
    "price" DECIMAL(30,18) NOT NULL,
    "green_points" DECIMAL(30,8) NOT NULL,
    "black_hole_amount" DECIMAL(30,8) NOT NULL,
    "circulation_pool" DECIMAL(30,8) NOT NULL,
    "effective_denominator" DECIMAL(30,8) NOT NULL,
    "minute_burn_rate" DECIMAL(30,18) NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

-- CreateIndex
CREATE UNIQUE INDEX "price_snapshots_snapshot_time_key" ON "price_snapshots"("snapshot_time");

-- CreateIndex
CREATE INDEX "price_snapshots_snapshot_time_idx" ON "price_snapshots"("snapshot_time" DESC);

-- ==================== 订单表添加销毁相关字段 ====================

-- AlterTable: 添加销毁相关字段到 orders 表
ALTER TABLE "orders" ADD COLUMN "burn_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0;
ALTER TABLE "orders" ADD COLUMN "burn_multiplier" DECIMAL(30,18) NOT NULL DEFAULT 0;
ALTER TABLE "orders" ADD COLUMN "effective_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0;

-- ==================== 成交记录表添加销毁相关字段 ====================

-- 添加销毁相关字段到 trades 表
ALTER TABLE "trades" ADD COLUMN "burn_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0;
ALTER TABLE "trades" ADD COLUMN "effective_qty" DECIMAL(30,8) NOT NULL DEFAULT 0;
