"""Client-overlay pins — identity at the composite, linear per-driver
scaling, the direct AI-token input, and copy semantics."""

import dataclasses

import pytest

from teicalc import (
    BENEFIT_DRIVERS,
    BENEFITS_VERBATIM,
    COMPOSITE,
    COST_DRIVERS,
    COSTS_VERBATIM,
    ClientDrivers,
    compute_summary,
    overlay_rows,
    scale_factor,
)


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


def test_identity_at_composite():
    """overlay_rows(COMPOSITE) reproduces the verbatim study to the cent."""
    ob, oc = overlay_rows(COMPOSITE)
    got = compute_summary(ob, oc, 0.10)
    want = compute_summary(BENEFITS_VERBATIM, COSTS_VERBATIM, 0.10)
    assert got["benefits_pv"] == pytest.approx(want["benefits_pv"], abs=0.01)
    assert got["costs_pv"] == pytest.approx(want["costs_pv"], abs=0.01)
    assert got["npv"] == pytest.approx(want["npv"], abs=0.01)


def test_driver_map_covers_every_row():
    assert set(BENEFIT_DRIVERS) == {r["field_key"] for r in BENEFITS_VERBATIM}
    assert set(COST_DRIVERS) == {r["field_key"] for r in COSTS_VERBATIM}


def test_scale_factor():
    d = ClientDrivers(agents_fte=300, weekly_interactions=160_000,
                      annual_revenue=5_000_000_000)
    assert scale_factor("agents", d) == pytest.approx(0.5)
    assert scale_factor("interactions", d) == pytest.approx(2.0)
    assert scale_factor("revenue", d) == pytest.approx(2.0)
    assert scale_factor("fixed", d) == 1.0
    with pytest.raises(KeyError):
        scale_factor("contacts", d)


def test_half_agents_halves_agent_rows_only():
    ob, oc = overlay_rows(ClientDrivers(agents_fte=300))
    assert _row(ob, "legacy_retirement")["year_values"]["1"] == pytest.approx(340_000)
    assert _row(oc, "cx_cloud_licenses")["year_values"]["1"] == pytest.approx(420_000)
    # Interaction-, revenue-driven, and fixed rows unmoved.
    assert _row(ob, "self_service_savings")["year_values"]["1"] == pytest.approx(2_329_600)
    assert _row(ob, "agent_assist_sales")["year_values"]["1"] == pytest.approx(600_000)
    assert _row(oc, "implementation")["initial"] == 1_190_000


def test_double_interactions_doubles_volume_rows_only():
    ob, oc = overlay_rows(ClientDrivers(weekly_interactions=160_000))
    assert _row(ob, "self_service_savings")["year_values"]["1"] == pytest.approx(4_659_200)
    assert _row(ob, "agent_efficiency")["year_values"]["1"] == pytest.approx(5_824_000)
    assert _row(ob, "legacy_retirement")["year_values"]["1"] == pytest.approx(680_000)
    assert _row(oc, "cx_cloud_licenses")["year_values"]["1"] == pytest.approx(840_000)


def test_double_revenue_doubles_agent_assist_only():
    ob, _ = overlay_rows(ClientDrivers(annual_revenue=5_000_000_000))
    assert _row(ob, "agent_assist_sales")["year_values"]["1"] == pytest.approx(1_200_000)
    assert _row(ob, "self_service_savings")["year_values"]["1"] == pytest.approx(2_329_600)


def test_ai_tokens_direct_input():
    """The token line takes the negotiated annual figure directly (rf 0.0),
    adding annual × Σ1/1.1ⁿ = 250,000 × 2.48685… ≈ $621,713 to costs PV."""
    _, oc = overlay_rows(ClientDrivers(ai_tokens_annual=250_000))
    tokens = _row(oc, "genesys_ai_tokens")
    assert tokens["year_values"] == {"1": 250_000.0, "2": 250_000.0, "3": 250_000.0}
    base = compute_summary(BENEFITS_VERBATIM, COSTS_VERBATIM, 0.10)
    ob, oc = overlay_rows(ClientDrivers(ai_tokens_annual=250_000))
    got = compute_summary(ob, oc, 0.10)
    assert got["costs_pv"] - base["costs_pv"] == pytest.approx(621_713.00, abs=1)
    assert got["benefits_pv"] == pytest.approx(base["benefits_pv"], abs=0.01)


def test_drivers_frozen_and_rows_are_copies():
    with pytest.raises(dataclasses.FrozenInstanceError):
        COMPOSITE.agents_fte = 1  # type: ignore[misc]
    ob, oc = overlay_rows(COMPOSITE)
    ob[0]["year_values"]["1"] = -1
    oc[0]["year_values"]["1"] = -1
    assert BENEFITS_VERBATIM[0]["year_values"]["1"] == 680_000
    assert COSTS_VERBATIM[0]["year_values"]["1"] == 840_000
