-- Polymarket Bot Database Schema

CREATE TABLE IF NOT EXISTS trades (
    id            TEXT PRIMARY KEY,
    market_id     TEXT NOT NULL,
    question      TEXT NOT NULL,
    direction     TEXT NOT NULL,      -- BUY_YES | BUY_NO
    size_usdc     DOUBLE PRECISION,
    entry_price   DOUBLE PRECISION,
    shares        DOUBLE PRECISION,
    fee_usdc      DOUBLE PRECISION,
    net_cost      DOUBLE PRECISION,
    timestamp     TIMESTAMPTZ NOT NULL,
    reasoning     TEXT,
    paper         BOOLEAN DEFAULT TRUE
);

CREATE TABLE IF NOT EXISTS metrics_daily (
    id                SERIAL PRIMARY KEY,
    timestamp         TIMESTAMPTZ NOT NULL,
    total_trades      INTEGER,
    total_deployed    DOUBLE PRECISION,
    total_fees        DOUBLE PRECISION,
    total_pnl         DOUBLE PRECISION,
    win_rate          DOUBLE PRECISION,
    avg_edge          DOUBLE PRECISION,
    sharpe_ratio      DOUBLE PRECISION,
    calibration_score DOUBLE PRECISION,
    paper_mode        BOOLEAN DEFAULT TRUE
);

CREATE TABLE IF NOT EXISTS markets (
    id            TEXT PRIMARY KEY,
    condition_id  TEXT,
    question      TEXT NOT NULL,
    category      TEXT,
    end_date      TEXT,
    active        BOOLEAN DEFAULT TRUE,
    last_seen     TIMESTAMPTZ DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS signals (
    id                SERIAL PRIMARY KEY,
    market_id         TEXT NOT NULL,
    timestamp         TIMESTAMPTZ NOT NULL,
    polymarket_price  DOUBLE PRECISION,
    estimated_prob    DOUBLE PRECISION,
    edge              DOUBLE PRECISION,
    confidence        DOUBLE PRECISION,
    direction         TEXT,
    acted_on          BOOLEAN DEFAULT FALSE
);

CREATE INDEX IF NOT EXISTS idx_trades_timestamp ON trades(timestamp DESC);
CREATE INDEX IF NOT EXISTS idx_trades_market ON trades(market_id);
CREATE INDEX IF NOT EXISTS idx_metrics_timestamp ON metrics_daily(timestamp DESC);
CREATE INDEX IF NOT EXISTS idx_signals_timestamp ON signals(timestamp DESC);
