from pathlib import Path

from absolutelyright import (
    absolutely_right,
    by_project,
    corrections,
    daily_counts,
    hour_histogram,
    images_pasted,
    load_sessions,
    model_mix,
    overview,
    phrase_counts,
    repeated_prompts,
    session_rows,
    shipped,
    slash_counts,
    tool_counts,
    weekday_histogram,
    word_counts,
)


def test_overview(claude_dir: Path) -> None:
    stats = overview(load_sessions(claude_dir))
    assert stats["sessions"] == 1
    assert stats["prompts"] == 4
    assert stats["interrupts"] == 1
    assert stats["toolCalls"] == 2
    assert stats["outputTokens"] == 200
    assert stats["first"] is not None and stats["first"].startswith("2026-06-10")


def test_repeated_prompts_requires_min_count(claude_dir: Path) -> None:
    assert repeated_prompts(load_sessions(claude_dir)) == [("fix the bug in auth", 2)]


def test_corrections_match_openers_only(claude_dir: Path) -> None:
    found = corrections(load_sessions(claude_dir))
    assert [p.text for p in found] == ["no, wait — look at the tests first"]


def test_by_project_aggregates(claude_dir: Path) -> None:
    (row,) = by_project(load_sessions(claude_dir))
    assert row["project"] == "github.com/me/proj"
    assert row["prompts"] == 4
    assert row["outputTokens"] == 200


def test_tool_counts(claude_dir: Path) -> None:
    assert tool_counts(load_sessions(claude_dir)) == [("Bash", 1), ("Edit", 1)]


def test_hour_histogram_uses_local_time(claude_dir: Path) -> None:
    histogram = hour_histogram(load_sessions(claude_dir))
    assert sum(histogram.values()) == 4
    assert len(histogram) == 1


def test_overview_includes_prs_and_cache(claude_dir: Path) -> None:
    stats = overview(load_sessions(claude_dir))
    assert stats["prsShipped"] == 1
    assert stats["cacheReadTokens"] == 10000


def test_raw_words_and_phrases(claude_dir: Path) -> None:
    sessions = load_sessions(claude_dir)
    assert absolutely_right(sessions) == 1
    assert dict(word_counts(sessions, "you"))["please"] == 1
    assert dict(word_counts(sessions, "claude"))["absolutely"] == 1
    assert dict(phrase_counts(sessions, "you", 3))["the bug in"] == 2
    assert dict(phrase_counts(sessions, "you", 4))["fix the bug in"] == 2
    assert images_pasted(sessions) == 1


def test_weekday_histogram(claude_dir: Path) -> None:
    counts = dict(weekday_histogram(load_sessions(claude_dir)))
    assert sum(counts.values()) == 4


def test_session_rows_and_shipped(claude_dir: Path) -> None:
    sessions = load_sessions(claude_dir)
    (row,) = session_rows(sessions)
    assert row["title"] == "fixing auth"
    assert row["activeMinutes"] == 1
    (pr,) = shipped(sessions)
    assert pr["url"].endswith("/pull/1")


def test_model_mix_and_slash_and_daily(claude_dir: Path) -> None:
    sessions = load_sessions(claude_dir)
    assert model_mix(sessions) == [("claude-fable-5", 2)]
    assert slash_counts(sessions) == [("/digest", 1)]
    assert sum(daily_counts(sessions, days=3650)) == 4


def test_by_parent_groups_first_segment(claude_dir: Path) -> None:
    from absolutelyright import by_parent

    (row,) = by_parent(load_sessions(claude_dir))
    assert row == ("github.com", 4)


def test_phrase_children_builds_prefix_tree(claude_dir: Path) -> None:
    from absolutelyright import phrase_children

    maps = phrase_children(load_sessions(claude_dir), "you")
    assert ("fix", 2) in maps[()]
    assert maps[("fix",)] == [("the", 2)]
    assert maps[("fix", "the")] == [("bug", 2)]
    assert maps[("fix", "the", "bug")] == [("in", 2)]


def test_distinctive_words_drops_shared_vocabulary(claude_dir: Path) -> None:
    from absolutelyright import distinctive_words

    rows = distinctive_words(load_sessions(claude_dir), "you", min_count=1)
    words = [w for w, _, _ in rows]
    assert "please" in words  # you say it, claude doesn't
    # "the" appears on both sides, so it scores near 1x and ranks below
    assert words.index("please") < words.index("the")
