"""Offline smoke test — validates scoring logic without touching the network.

Builds a synthetic StockData and runs every strategy, then asserts scores
are in range and higher for a 'good' profile than a 'bad' one.
"""
import numpy as np
import pandas as pd

from screener.data import StockData
from screener.strategies import STRATEGIES


def _hist(trend: float = 0.0004, vol: float = 0.01, n: int = 300, start: float = 100.0):
    """Deterministic synthetic price history (no RNG, reproducible)."""
    idx = pd.date_range("2024-01-01", periods=n, freq="D")
    steps = trend + vol * np.sin(np.linspace(0, 12 * np.pi, n))
    close = start * np.cumprod(1 + steps)
    return pd.DataFrame({"Close": close, "Open": close, "High": close,
                         "Low": close, "Volume": 1_000_000}, index=idx)


GOOD = StockData(
    ticker="GOOD", ok=True,
    info={
        "shortName": "Good Co", "sector": "Tech", "currentPrice": 100,
        "marketCap": 1_000_000_000, "sharesOutstanding": 10_000_000,
        "returnOnEquity": 0.28, "debtToEquity": 20, "grossMargins": 0.55,
        "operatingMargins": 0.30, "freeCashflow": 90_000_000, "revenueGrowth": 0.20,
        "trailingPE": 15, "currentRatio": 2.5, "enterpriseToEbitda": 8,
        "priceToBook": 1.2, "priceToSalesTrailing12Months": 1.5,
        "totalCash": 500_000_000, "totalDebt": 100_000_000,
        "fiftyTwoWeekLow": 95, "shortPercentOfFloat": 0.22, "shortRatio": 6,
        "earningsGrowth": 0.15,
    },
    history=_hist(trend=0.0003),
)

BAD = StockData(
    ticker="BAD", ok=True,
    info={
        "shortName": "Bad Co", "sector": "Junk", "currentPrice": 100,
        "marketCap": 1_000_000_000, "sharesOutstanding": 10_000_000,
        "returnOnEquity": -0.05, "debtToEquity": 300, "grossMargins": 0.08,
        "operatingMargins": -0.02, "freeCashflow": -20_000_000, "revenueGrowth": -0.10,
        "trailingPE": 90, "currentRatio": 0.6, "enterpriseToEbitda": 40,
        "priceToBook": 12, "priceToSalesTrailing12Months": 15,
        "totalCash": 10_000_000, "totalDebt": 800_000_000,
        "fiftyTwoWeekLow": 40, "shortPercentOfFloat": 0.01, "shortRatio": 0.5,
        "earningsGrowth": -0.2,
    },
    history=_hist(trend=-0.0002),
)


def main():
    print(f"{'Strategy':<15}{'GOOD':>8}{'BAD':>8}")
    all_ok = True
    for name, evaluate in STRATEGIES.items():
        g = evaluate(GOOD)
        b = evaluate(BAD)
        assert 0 <= g.score <= 100, f"{name} good out of range"
        assert 0 <= b.score <= 100, f"{name} bad out of range"
        flag = "" if g.score >= b.score else "  <-- unexpected"
        if g.score < b.score:
            all_ok = False
        print(f"{name:<15}{g.score:>8.1f}{b.score:>8.1f}{flag}")
    print("\nOK" if all_ok else "\nFAIL: some GOOD < BAD")
    assert all_ok


if __name__ == "__main__":
    main()
