"""The verbatim anchor is Forrester's published record — pinned value by
value, and proven immutable under every engine code path."""

from copy import deepcopy

from teicalc import (
    ASSUMPTIONS,
    BENEFITS_VERBATIM,
    COMPOSITE,
    COSTS_VERBATIM,
    PUBLISHED,
    ClientDrivers,
    apply_scenario,
    compute_summary,
    overlay_rows,
)


def _row(rows, key):
    return next(r for r in rows if r["field_key"] == key)


def test_benefit_rows_verbatim():
    assert [r["field_key"] for r in BENEFITS_VERBATIM] == [
        "legacy_retirement",
        "self_service_savings",
        "agent_efficiency",
        "agent_assist_sales",
    ]
    expected = {
        "legacy_retirement": ({"1": 680_000, "2": 930_000, "3": 930_000}, 0.05),
        "self_service_savings": ({"1": 2_329_600, "2": 2_329_600, "3": 2_329_600}, 0.15),
        "agent_efficiency": ({"1": 2_912_000, "2": 2_912_000, "3": 2_912_000}, 0.10),
        "agent_assist_sales": ({"1": 600_000, "2": 600_000, "3": 600_000}, 0.05),
    }
    for key, (years, rf) in expected.items():
        row = _row(BENEFITS_VERBATIM, key)
        assert row["year_values"] == years
        assert row["risk_adjustment"] == rf
        assert row["table"] == "benefits"


def test_cost_rows_verbatim():
    expected = {
        "cx_cloud_licenses": ({"1": 840_000, "2": 840_000, "3": 840_000}, 0.05, 0),
        "implementation": ({"1": 0, "2": 0, "3": 0}, 0.10, 1_190_000),
        "ongoing_management": ({"1": 202_800, "2": 202_800, "3": 202_800}, 0.10, 0),
        "genesys_ai_tokens": ({"1": 0, "2": 0, "3": 0}, 0.0, 0),
    }
    for key, (years, rf, initial) in expected.items():
        row = _row(COSTS_VERBATIM, key)
        assert row["year_values"] == years
        assert row["risk_adjustment"] == rf
        assert row["initial"] == initial
        assert row["table"] == "costs"


def test_ai_token_line_is_anchored_at_zero():
    """The published study models $0 AI consumption — the study's blind spot,
    preserved verbatim so the reproduction matches the published totals."""
    row = _row(COSTS_VERBATIM, "genesys_ai_tokens")
    assert all(v == 0 for v in row["year_values"].values())
    assert row["initial"] == 0 and row["risk_adjustment"] == 0.0
    assert "NOT in the published study" in row["notes"]


def test_assumptions_and_published():
    assert ASSUMPTIONS["annual_revenue"] == 2_500_000_000
    assert ASSUMPTIONS["agents_fte"] == 600
    assert ASSUMPTIONS["concurrent_licenses"] == 400
    assert ASSUMPTIONS["weekly_interactions"] == 80_000
    assert ASSUMPTIONS["discount_rate"] == 0.10
    assert ASSUMPTIONS["analysis_years"] == 3

    assert PUBLISHED["benefits_pv"] == 14_840_638
    assert PUBLISHED["costs_pv"] == 4_057_170
    assert PUBLISHED["npv"] == 10_783_468
    assert PUBLISHED["roi_pct"] == 266
    assert "payback" not in str(sorted(PUBLISHED))  # study doesn't headline one

    # The composite drivers ARE the anchor assumptions (tokens at $0).
    assert COMPOSITE.agents_fte == ASSUMPTIONS["agents_fte"]
    assert COMPOSITE.weekly_interactions == ASSUMPTIONS["weekly_interactions"]
    assert COMPOSITE.annual_revenue == ASSUMPTIONS["annual_revenue"]
    assert COMPOSITE.ai_tokens_annual == 0.0
    assert COMPOSITE.discount_rate == ASSUMPTIONS["discount_rate"]


def test_anchor_is_never_mutated():
    """Exercise every engine code path, then prove the record unchanged."""
    ben_snap = deepcopy(BENEFITS_VERBATIM)
    cost_snap = deepcopy(COSTS_VERBATIM)

    overlay_rows()
    overlay_rows(ClientDrivers(agents_fte=137, weekly_interactions=5_000,
                               annual_revenue=9e9, ai_tokens_annual=450_000))
    for scenario in ("conservative", "moderate", "aggressive"):
        apply_scenario(BENEFITS_VERBATIM, scenario)
        apply_scenario(COSTS_VERBATIM, scenario)
    compute_summary(BENEFITS_VERBATIM, COSTS_VERBATIM, 0.10)
    compute_summary(BENEFITS_VERBATIM, COSTS_VERBATIM, 0.08)

    assert BENEFITS_VERBATIM == ben_snap
    assert COSTS_VERBATIM == cost_snap
