"""
Real-shaped (synthetic, never live-API) company fixtures for validating the metrics/scoring/
valuation engines against structurally distinct business types (StockLab overhaul, Part 24).

None of these are real companies' actual financials — every number here is hand-constructed to be
INTERNALLY CONSISTENT (revenue - cogs = gross profit, debt figures that produce the intended
leverage ratio, etc.) and structurally representative of the named archetype, never fetched from
or fitted to a live provider. This is the explicit alternative to live-API test data the overhaul
spec requires ("Do NOT use live financial APIs during automated tests").
"""
from __future__ import annotations

from datetime import date

from app.engines.types import FinancialSnapshot, LineItems, MarketCapBucket


def _li(period_end: str, **kwargs) -> LineItems:
    defaults = dict(
        security_id="FIXTURE", period_end=date.fromisoformat(period_end), period_type="FY",
        filing_date=date.fromisoformat(period_end), currency="USD",
    )
    defaults.update(kwargs)
    return LineItems(**defaults)


def _snap(current, history, sector="DEFAULT", bucket=MarketCapBucket.LARGE, **kw) -> FinancialSnapshot:
    return FinancialSnapshot(
        security_id="FIXTURE", industry_id="FIXTURE_IND", sector_id=sector,
        market_cap_bucket=bucket, calculation_date=current.period_end,
        current=current, history=history, **kw,
    )


def large_cap_stable() -> FinancialSnapshot:
    """Mega-cap, wide-moat shape: high margins, low leverage, steady mid-single-digit growth."""
    cur = _li("2025-12-31", revenue=400_000, cogs=160_000, gross_profit=240_000, operating_income=120_000,
              ebit=120_000, ebitda=135_000, net_income=95_000, eps_diluted=6.10, tax_expense=22_000,
              pretax_income=117_000, interest_expense=2_000, total_debt=40_000, shareholders_equity=180_000,
              cash_and_equivalents=60_000, current_assets=110_000, current_liabilities=70_000,
              diluted_shares=15_500, operating_cash_flow=110_000, capital_expenditure=20_000,
              dividends_paid=15_000, buybacks=40_000, price=180.0, market_cap=2_790_000)
    h = [_li("2024-12-31", revenue=380_000, net_income=88_000, shareholders_equity=165_000, diluted_shares=15_700,
              ebit=112_000, tax_expense=20_500, pretax_income=109_000)]
    return _snap(cur, h, sector="DEFAULT", bucket=MarketCapBucket.LARGE)


def small_cap_growth() -> FinancialSnapshot:
    """Small-cap, still-scaling shape: high growth, thinner margins, modest debt."""
    cur = _li("2025-12-31", revenue=180, cogs=90, gross_profit=90, operating_income=18, ebit=18, ebitda=22,
              net_income=10, eps_diluted=0.50, tax_expense=3, pretax_income=13, interest_expense=1,
              total_debt=25, shareholders_equity=60, cash_and_equivalents=15, current_assets=40,
              current_liabilities=25, diluted_shares=20, operating_cash_flow=14, capital_expenditure=6,
              price=12.0, market_cap=240)
    h = [_li("2024-12-31", revenue=140, net_income=6, shareholders_equity=50, diluted_shares=19.5, ebit=13,
              tax_expense=2, pretax_income=9)]
    return _snap(cur, h, sector="DEFAULT", bucket=MarketCapBucket.SMALL)


def bank() -> FinancialSnapshot:
    """Bank shape: FINANCIALS sector (Debt/EBITDA, Current Ratio, EV/EBITDA all N/M per the
    applicability matrix), ROE-driven, large balance sheet relative to income."""
    cur = _li("2025-12-31", revenue=25_000, net_income=6_500, eps_diluted=4.20, tax_expense=1_800,
              pretax_income=8_300, total_debt=350_000, shareholders_equity=42_000,
              cash_and_equivalents=30_000, diluted_shares=1_550, dividends_paid=2_200,
              price=95.0, market_cap=147_250, ebit=8_300, interest_expense=9_000)
    h = [_li("2024-12-31", net_income=6_000, shareholders_equity=39_000)]
    return _snap(cur, h, sector="FINANCIALS", bucket=MarketCapBucket.LARGE)


def reit() -> FinancialSnapshot:
    """REIT shape: REAL_ESTATE sector, high leverage funding real assets, high dividend payout."""
    cur = _li("2025-12-31", revenue=3_000, cogs=900, gross_profit=2_100, operating_income=1_200,
              ebit=1_200, ebitda=1_800, net_income=500, eps_diluted=1.10, tax_expense=50,
              pretax_income=550, interest_expense=650, total_debt=14_000, shareholders_equity=9_000,
              cash_and_equivalents=300, diluted_shares=450, operating_cash_flow=1_400,
              capital_expenditure=1_100, dividends_paid=1_250, price=28.0, market_cap=12_600)
    h = [_li("2024-12-31", revenue=2_800, net_income=460, shareholders_equity=8_700)]
    return _snap(cur, h, sector="REAL_ESTATE", bucket=MarketCapBucket.MID)


def cyclical() -> FinancialSnapshot:
    """Cyclical (materials/industrials) shape: revenue and margins swinging with the cycle —
    history shows a down year sandwiched between up years."""
    cur = _li("2025-12-31", revenue=8_500, cogs=6_800, gross_profit=1_700, operating_income=700,
              ebit=700, ebitda=1_000, net_income=380, eps_diluted=1.90, tax_expense=110,
              pretax_income=490, interest_expense=90, total_debt=2_200, shareholders_equity=3_500,
              cash_and_equivalents=400, diluted_shares=200, operating_cash_flow=650,
              capital_expenditure=400, price=22.0, market_cap=4_400)
    h = [
        _li("2024-12-31", revenue=6_200, net_income=-150, shareholders_equity=3_150, ebit=-100),  # the down year
        _li("2023-12-31", revenue=9_000, net_income=420, shareholders_equity=3_400, ebit=750),
    ]
    return _snap(cur, h, sector="ENERGY", bucket=MarketCapBucket.MID)


def high_growth_unprofitable() -> FinancialSnapshot:
    """Hypergrowth, pre-profitability shape: very high revenue growth, negative net income,
    negative FCF (funding growth from the balance sheet, not operations)."""
    cur = _li("2025-12-31", revenue=600, cogs=250, gross_profit=350, operating_income=-120,
              ebit=-120, ebitda=-90, net_income=-140, eps_diluted=-0.70, total_debt=100,
              shareholders_equity=400, cash_and_equivalents=350, diluted_shares=200,
              operating_cash_flow=-60, capital_expenditure=40, price=18.0, market_cap=3_600)
    h = [_li("2024-12-31", revenue=320, net_income=-110, shareholders_equity=480, ebit=-95)]
    return _snap(cur, h, sector="DEFAULT", bucket=MarketCapBucket.MID)


def negative_fcf_reinvestor() -> FinancialSnapshot:
    """Profitable on paper but heavy capex consistently exceeds operating cash flow — a distinct
    shape from high_growth_unprofitable (this one has POSITIVE net income, negative FCF)."""
    cur = _li("2025-12-31", revenue=2_200, cogs=1_300, gross_profit=900, operating_income=250,
              ebit=250, ebitda=450, net_income=140, eps_diluted=1.40, tax_expense=40,
              pretax_income=180, total_debt=900, shareholders_equity=1_100, cash_and_equivalents=100,
              diluted_shares=100, operating_cash_flow=300, capital_expenditure=480,
              price=16.0, market_cap=1_600)
    h = [_li("2024-12-31", revenue=1_900, net_income=120, shareholders_equity=980, ebit=210, pretax_income=150)]
    return _snap(cur, h, sector="DEFAULT", bucket=MarketCapBucket.MID)


def high_debt_leveraged() -> FinancialSnapshot:
    """High-leverage shape: elevated Debt/EBITDA and Net Debt/EBITDA, thin interest coverage —
    the archetype the debt-focused red flags/sell-triggers should catch."""
    cur = _li("2025-12-31", revenue=1_800, cogs=1_100, gross_profit=700, operating_income=220,
              ebit=220, ebitda=300, net_income=40, eps_diluted=0.40, tax_expense=15,
              pretax_income=55, interest_expense=140, total_debt=2_100, shareholders_equity=250,
              cash_and_equivalents=80, diluted_shares=100, operating_cash_flow=180,
              capital_expenditure=90, price=6.0, market_cap=600)
    h = [_li("2024-12-31", revenue=1_750, net_income=60, shareholders_equity=230, total_debt=1_800, ebit=230)]
    return _snap(cur, h, sector="DEFAULT", bucket=MarketCapBucket.SMALL)


ALL_FIXTURES = {
    "large_cap_stable": large_cap_stable,
    "small_cap_growth": small_cap_growth,
    "bank": bank,
    "reit": reit,
    "cyclical": cyclical,
    "high_growth_unprofitable": high_growth_unprofitable,
    "negative_fcf_reinvestor": negative_fcf_reinvestor,
    "high_debt_leveraged": high_debt_leveraged,
}
