"""SQLite schema and dataclasses for TGTC trading state."""
from __future__ import annotations

from dataclasses import dataclass

CREATE_TABLES_SQL = """
CREATE TABLE IF NOT EXISTS tgtc_sessions (
    session_id      TEXT PRIMARY KEY,
    session_name    TEXT NOT NULL UNIQUE,
    config_path     TEXT NOT NULL,
    initial_equity  REAL NOT NULL,
    created_at      TEXT NOT NULL,
    status          TEXT NOT NULL DEFAULT 'active'
);

CREATE TABLE IF NOT EXISTS tgtc_snapshots (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    session_id      TEXT NOT NULL REFERENCES tgtc_sessions(session_id),
    date            TEXT NOT NULL,
    captured_at     TEXT NOT NULL,
    symbol          TEXT NOT NULL,
    rank            INTEGER NOT NULL,
    price           REAL,
    pct_change      REAL,
    volume          REAL,
    market_cap      REAL,
    UNIQUE(session_id, captured_at, symbol)
);

CREATE TABLE IF NOT EXISTS tgtc_candidates (
    id                  INTEGER PRIMARY KEY AUTOINCREMENT,
    session_id          TEXT NOT NULL REFERENCES tgtc_sessions(session_id),
    date                TEXT NOT NULL,
    symbol              TEXT NOT NULL,
    score               REAL,
    rank_persistence    REAL,
    rank_velocity       REAL,
    price_structure     REAL,
    volume_quality      REAL,
    relative_strength   REAL,
    pct_change_at_10    REAL,
    price_at_10         REAL,
    vwap_at_10          REAL,
    above_vwap          INTEGER,
    decided_at          TEXT NOT NULL,
    status              TEXT NOT NULL DEFAULT 'pending',
    UNIQUE(session_id, date, symbol)
);

CREATE TABLE IF NOT EXISTS tgtc_positions (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    session_id      TEXT NOT NULL REFERENCES tgtc_sessions(session_id),
    date            TEXT NOT NULL,
    symbol          TEXT NOT NULL,
    entry_signal    TEXT NOT NULL DEFAULT 'vwap_pullback_reclaim',
    entry_price     REAL NOT NULL,
    stop_price      REAL NOT NULL,
    current_stop    REAL NOT NULL,
    shares          INTEGER NOT NULL,
    entered_at      TEXT NOT NULL,
    peak_price      REAL NOT NULL DEFAULT 0.0,
    partial_taken   INTEGER NOT NULL DEFAULT 0,
    be_stop_active  INTEGER NOT NULL DEFAULT 0,
    exit_price      REAL,
    exit_reason     TEXT,
    exited_at       TEXT,
    pnl             REAL,
    r_multiple      REAL,
    is_dry_run      INTEGER NOT NULL DEFAULT 1,
    status          TEXT NOT NULL DEFAULT 'open',
    UNIQUE(session_id, date, symbol)
);

CREATE TABLE IF NOT EXISTS tgtc_trades (
    trade_id        TEXT PRIMARY KEY,
    session_id      TEXT NOT NULL,
    date            TEXT NOT NULL,
    symbol          TEXT NOT NULL,
    entry_signal    TEXT NOT NULL DEFAULT 'vwap_pullback_reclaim',
    entry_price     REAL NOT NULL,
    exit_price      REAL NOT NULL,
    entered_at      TEXT NOT NULL,
    exited_at       TEXT NOT NULL,
    shares          INTEGER NOT NULL,
    pnl             REAL NOT NULL,
    r_multiple      REAL NOT NULL,
    exit_reason     TEXT NOT NULL,
    is_dry_run      INTEGER NOT NULL DEFAULT 1
);

CREATE TABLE IF NOT EXISTS tgtc_daily_snapshots (
    session_id      TEXT NOT NULL,
    date            TEXT NOT NULL,
    equity          REAL NOT NULL,
    daily_pnl       REAL,
    total_pnl       REAL,
    trades_taken    INTEGER,
    phase           TEXT NOT NULL DEFAULT 'idle',
    PRIMARY KEY (session_id, date)
);
"""


@dataclass
class TGTCSessionRow:
    session_id: str
    session_name: str
    config_path: str
    initial_equity: float
    created_at: str
    status: str = "active"


@dataclass
class TGTCSnapshotRow:
    session_id: str
    date: str
    captured_at: str
    symbol: str
    rank: int
    price: float | None = None
    pct_change: float | None = None
    volume: float | None = None
    market_cap: float | None = None
    id: int | None = None


@dataclass
class TGTCCandidateRow:
    session_id: str
    date: str
    symbol: str
    decided_at: str
    score: float | None = None
    rank_persistence: float | None = None
    rank_velocity: float | None = None
    price_structure: float | None = None
    volume_quality: float | None = None
    relative_strength: float | None = None
    pct_change_at_10: float | None = None
    price_at_10: float | None = None
    vwap_at_10: float | None = None
    above_vwap: bool = False
    status: str = "pending"
    id: int | None = None


@dataclass
class TGTCPositionRow:
    session_id: str
    date: str
    symbol: str
    entry_price: float
    stop_price: float
    current_stop: float
    shares: int
    entered_at: str
    peak_price: float = 0.0
    partial_taken: bool = False
    be_stop_active: bool = False
    entry_signal: str = "vwap_pullback_reclaim"
    exit_price: float | None = None
    exit_reason: str | None = None
    exited_at: str | None = None
    pnl: float | None = None
    r_multiple: float | None = None
    is_dry_run: bool = True
    status: str = "open"
    id: int | None = None


@dataclass
class TGTCTradeRow:
    trade_id: str
    session_id: str
    date: str
    symbol: str
    entry_price: float
    exit_price: float
    entered_at: str
    exited_at: str
    shares: int
    pnl: float
    r_multiple: float
    exit_reason: str
    entry_signal: str = "vwap_pullback_reclaim"
    is_dry_run: bool = True


@dataclass
class TGTCDailySnapshotRow:
    session_id: str
    date: str
    equity: float
    daily_pnl: float = 0.0
    total_pnl: float = 0.0
    trades_taken: int = 0
    phase: str = "idle"
