"""Export contracts — JSON round-trips, CSV shape pinned to the spec."""

from datetime import date

import pandas as pd

from diaglib import (
    CSV_COLUMNS,
    build_engagement,
    load_engagement,
    parse_participants,
    scores_dataframe,
    value_at_stake,
    write_exports,
)


def _engagement(config, baseline, scores):
    return build_engagement(
        config=config, client_name="Acme Demo Co", facilitator="Robert Helewka",
        workshop_date=date(2026, 7, 19),
        participants=parse_participants(
            "Jane Example | VP Customer Experience | cx; Sam Sample | Ops Director | ops"),
        baseline=baseline, scores=scores,
        computed_value=value_at_stake(config, baseline, scores))


def test_write_exports_and_reload(config, baseline, scores, tmp_path):
    eng = _engagement(config, baseline, scores)
    json_path, csv_path = write_exports(eng, config, tmp_path)
    assert json_path.name == "acme_demo_co_2026-07-19.json"
    assert csv_path.name == "acme_demo_co_2026-07-19.csv"

    # JSON is the source-of-truth artifact — it must round-trip losslessly.
    reloaded = load_engagement(json_path)
    assert reloaded == eng

    df = pd.read_csv(csv_path)
    assert list(df.columns) == CSV_COLUMNS
    assert len(df) == 12


def test_scores_dataframe_flags(config, baseline, scores):
    df = scores_dataframe(_engagement(config, baseline, scores), config)
    by_id = df.set_index("competency_id")
    assert bool(by_id.loc["data_readiness", "is_foundational"])
    assert bool(by_id.loc["data_readiness", "is_binding_constraint"])
    assert bool(by_id.loc["process_discovery", "is_foundational"])
    assert not bool(by_id.loc["process_discovery", "is_binding_constraint"])
    assert not bool(by_id.loc["automation_ai_strategy", "is_foundational"])
    assert (df["engagement_id"] == "acme_demo_co_2026-07-19").all()
    assert (df["industry"] == "contact_center").all()
