CREATE TABLE IF NOT EXISTS paper_orders (
    id BIGSERIAL PRIMARY KEY,
    recommendation_id BIGINT NOT NULL UNIQUE,
    symbol TEXT NOT NULL,
    side TEXT NOT NULL DEFAULT 'long',
    order_type TEXT NOT NULL DEFAULT 'limit',
    status TEXT NOT NULL DEFAULT 'pending',
    source_status TEXT DEFAULT '',
    source_action TEXT DEFAULT '',
    target_price DOUBLE PRECISION NOT NULL,
    current_price_at_create DOUBLE PRECISION DEFAULT 0,
    fill_price DOUBLE PRECISION DEFAULT 0,
    notional_usdt DOUBLE PRECISION DEFAULT 0,
    stop_loss DOUBLE PRECISION DEFAULT 0,
    tp1 DOUBLE PRECISION DEFAULT 0,
    tp2 DOUBLE PRECISION DEFAULT 0,
    strategy_version TEXT DEFAULT '',
    entry_plan_snapshot_json TEXT DEFAULT '{}',
    created_at TEXT NOT NULL,
    updated_at TEXT NOT NULL,
    expires_at TEXT DEFAULT '',
    filled_at TEXT DEFAULT '',
    canceled_at TEXT DEFAULT '',
    cancel_reason TEXT DEFAULT ''
);

CREATE INDEX IF NOT EXISTS idx_paper_orders_status_updated ON paper_orders(status, updated_at DESC);
CREATE INDEX IF NOT EXISTS idx_paper_orders_symbol_status ON paper_orders(symbol, status);
CREATE INDEX IF NOT EXISTS idx_paper_orders_recommendation ON paper_orders(recommendation_id);
