-- ============================================================================
-- trading-service 添加做市商双边深度功能
-- 包含：深度开关、做市商双边挂单配置、做市商挂单记录表
-- ============================================================================

-- ==================== TradingConfig 添加深度开关 ====================

-- 添加深度显示开关
ALTER TABLE "trading_configs" ADD COLUMN IF NOT EXISTS "depth_enabled" BOOLEAN NOT NULL DEFAULT false;

-- ==================== MarketMakerConfig 添加双边挂单配置 ====================

-- 挂单模式开关
ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "maker_enabled" BOOLEAN NOT NULL DEFAULT false;

-- 买单挂单配置
ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "bid_enabled" BOOLEAN NOT NULL DEFAULT true;
ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "bid_levels" INTEGER NOT NULL DEFAULT 5;
ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "bid_spread" DECIMAL(10, 4) NOT NULL DEFAULT 0.01;
ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "bid_level_spacing" DECIMAL(10, 4) NOT NULL DEFAULT 0.005;
ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "bid_quantity_per_level" DECIMAL(30, 8) NOT NULL DEFAULT 1000;

-- 卖单挂单配置
ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "ask_enabled" BOOLEAN NOT NULL DEFAULT true;
ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "ask_levels" INTEGER NOT NULL DEFAULT 5;
ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "ask_spread" DECIMAL(10, 4) NOT NULL DEFAULT 0.01;
ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "ask_level_spacing" DECIMAL(10, 4) NOT NULL DEFAULT 0.005;
ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "ask_quantity_per_level" DECIMAL(30, 8) NOT NULL DEFAULT 1000;

-- 挂单刷新配置
ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "refresh_interval_ms" INTEGER NOT NULL DEFAULT 60000;
ALTER TABLE "market_maker_configs" ADD COLUMN IF NOT EXISTS "last_refresh_at" TIMESTAMP(3);

-- ==================== 做市商挂单记录表 ====================

-- CreateTable
CREATE TABLE IF NOT EXISTS "market_maker_orders" (
    "id" TEXT NOT NULL,
    "market_maker_id" TEXT NOT NULL,
    "order_id" TEXT NOT NULL,
    "order_no" TEXT NOT NULL,
    "side" TEXT NOT NULL,
    "level" INTEGER NOT NULL,
    "price" DECIMAL(30, 18) NOT NULL,
    "quantity" DECIMAL(30, 8) NOT NULL,
    "remaining_qty" DECIMAL(30, 8) NOT NULL,
    "status" TEXT NOT NULL DEFAULT 'ACTIVE',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

-- CreateIndex
CREATE UNIQUE INDEX IF NOT EXISTS "market_maker_orders_order_id_key" ON "market_maker_orders"("order_id");

-- CreateIndex
CREATE UNIQUE INDEX IF NOT EXISTS "market_maker_orders_order_no_key" ON "market_maker_orders"("order_no");

-- CreateIndex
CREATE INDEX IF NOT EXISTS "market_maker_orders_market_maker_id_side_status_idx" ON "market_maker_orders"("market_maker_id", "side", "status");

-- CreateIndex
CREATE INDEX IF NOT EXISTS "market_maker_orders_status_idx" ON "market_maker_orders"("status");

-- AddForeignKey (only if not exists)
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint WHERE conname = 'market_maker_orders_market_maker_id_fkey'
    ) THEN
        ALTER TABLE "market_maker_orders" ADD CONSTRAINT "market_maker_orders_market_maker_id_fkey"
        FOREIGN KEY ("market_maker_id") REFERENCES "market_maker_configs"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
    END IF;
END $$;
