-- 订单来源字段
ALTER TABLE "orders" ADD COLUMN "source" TEXT NOT NULL DEFAULT 'USER';
ALTER TABLE "orders" ADD COLUMN "source_label" TEXT;
CREATE INDEX "orders_source_idx" ON "orders"("source");

-- 成交记录来源字段
ALTER TABLE "trades" ADD COLUMN "buyer_source" TEXT NOT NULL DEFAULT 'USER';
ALTER TABLE "trades" ADD COLUMN "seller_source" TEXT NOT NULL DEFAULT 'USER';
CREATE INDEX "trades_buyer_source_idx" ON "trades"("buyer_source");
CREATE INDEX "trades_seller_source_idx" ON "trades"("seller_source");

-- 做市商配置表
CREATE TABLE "market_maker_configs" (
    "id" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "account_sequence" TEXT NOT NULL,
    "cash_balance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "share_balance" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "frozen_cash" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "frozen_shares" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "max_buy_ratio" DECIMAL(10,4) NOT NULL DEFAULT 0.05,
    "min_interval_ms" INTEGER NOT NULL DEFAULT 1000,
    "max_interval_ms" INTEGER NOT NULL DEFAULT 4000,
    "price_strategy" TEXT NOT NULL DEFAULT 'TAKER',
    "discount_rate" DECIMAL(10,4) NOT NULL DEFAULT 1.0,
    "is_active" BOOLEAN NOT NULL DEFAULT false,
    "last_run_at" TIMESTAMP(3),
    "total_buy_count" INTEGER NOT NULL DEFAULT 0,
    "total_buy_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_buy_amount" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_sell_count" INTEGER NOT NULL DEFAULT 0,
    "total_sell_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "total_sell_amount" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

CREATE UNIQUE INDEX "market_maker_configs_name_key" ON "market_maker_configs"("name");
CREATE UNIQUE INDEX "market_maker_configs_account_sequence_key" ON "market_maker_configs"("account_sequence");

-- 做市商分类账表
CREATE TABLE "market_maker_ledgers" (
    "id" TEXT NOT NULL,
    "market_maker_id" TEXT NOT NULL,
    "type" TEXT NOT NULL,
    "asset_type" TEXT NOT NULL,
    "amount" DECIMAL(30,8) NOT NULL,
    "balance_before" DECIMAL(30,8) NOT NULL,
    "balance_after" DECIMAL(30,8) NOT NULL,
    "trade_id" TEXT,
    "trade_no" TEXT,
    "order_id" TEXT,
    "order_no" TEXT,
    "counterparty_seq" TEXT,
    "counterparty_id" TEXT,
    "price" DECIMAL(30,18),
    "quantity" DECIMAL(30,8),
    "memo" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

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

CREATE INDEX "market_maker_ledgers_market_maker_id_created_at_idx" ON "market_maker_ledgers"("market_maker_id", "created_at" DESC);
CREATE INDEX "market_maker_ledgers_type_idx" ON "market_maker_ledgers"("type");
CREATE INDEX "market_maker_ledgers_trade_no_idx" ON "market_maker_ledgers"("trade_no");
CREATE INDEX "market_maker_ledgers_order_no_idx" ON "market_maker_ledgers"("order_no");
CREATE INDEX "market_maker_ledgers_counterparty_seq_idx" ON "market_maker_ledgers"("counterparty_seq");
CREATE INDEX "market_maker_ledgers_created_at_idx" ON "market_maker_ledgers"("created_at" DESC);

ALTER TABLE "market_maker_ledgers" ADD CONSTRAINT "market_maker_ledgers_market_maker_id_fkey" FOREIGN KEY ("market_maker_id") REFERENCES "market_maker_configs"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- 做市商日统计表
CREATE TABLE "market_maker_daily_stats" (
    "id" TEXT NOT NULL,
    "market_maker_id" TEXT NOT NULL,
    "date" DATE NOT NULL,
    "buy_count" INTEGER NOT NULL DEFAULT 0,
    "buy_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "buy_amount" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "avg_buy_price" DECIMAL(30,18) NOT NULL DEFAULT 0,
    "sell_count" INTEGER NOT NULL DEFAULT 0,
    "sell_quantity" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "sell_amount" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "avg_sell_price" DECIMAL(30,18) NOT NULL DEFAULT 0,
    "realized_pnl" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "cash_balance_end" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "share_balance_end" DECIMAL(30,8) NOT NULL DEFAULT 0,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL,

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

CREATE UNIQUE INDEX "market_maker_daily_stats_market_maker_id_date_key" ON "market_maker_daily_stats"("market_maker_id", "date");
CREATE INDEX "market_maker_daily_stats_market_maker_id_date_idx" ON "market_maker_daily_stats"("market_maker_id", "date" DESC);
