"""Tests for the interview module."""

from timmy.interview import (
    INTERVIEW_QUESTIONS,
    InterviewEntry,
    format_transcript,
    run_interview,
)


class TestInterviewQuestions:
    def test_questions_not_empty(self):
        assert len(INTERVIEW_QUESTIONS) > 0

    def test_each_question_has_category_and_question(self):
        for item in INTERVIEW_QUESTIONS:
            assert "category" in item
            assert "question" in item

    def test_covers_expected_categories(self):
        categories = {q["category"] for q in INTERVIEW_QUESTIONS}
        assert categories == {"Identity", "Capabilities", "Values", "Operational"}


class TestRunInterview:
    def test_collects_all_answers(self):
        transcript = run_interview(chat_fn=lambda q: f"answer to: {q}")
        assert len(transcript) == len(INTERVIEW_QUESTIONS)
        for entry in transcript:
            assert isinstance(entry, InterviewEntry)
            assert entry.answer.startswith("answer to:")

    def test_on_answer_callback_called(self):
        received = []
        run_interview(chat_fn=lambda q: "ok", on_answer=lambda e: received.append(e))
        assert len(received) == len(INTERVIEW_QUESTIONS)

    def test_custom_questions(self):
        custom = [{"category": "Test", "question": "Hello?"}]
        transcript = run_interview(chat_fn=lambda q: "hi", questions=custom)
        assert len(transcript) == 1
        assert transcript[0].category == "Test"
        assert transcript[0].question == "Hello?"

    def test_chat_fn_error_captured(self):
        def failing_chat(q):
            raise RuntimeError("boom")

        transcript = run_interview(chat_fn=failing_chat)
        assert len(transcript) == len(INTERVIEW_QUESTIONS)
        for entry in transcript:
            assert "(Error: boom)" in entry.answer


class TestFormatTranscript:
    def test_empty_transcript(self):
        assert format_transcript([]) == "(No interview data)"

    def test_contains_header(self):
        entries = [InterviewEntry(category="Identity", question="Who?", answer="Timmy")]
        result = format_transcript(entries)
        assert "TIMMY INTERVIEW TRANSCRIPT" in result

    def test_contains_qa(self):
        entries = [InterviewEntry(category="Identity", question="Who?", answer="Timmy")]
        result = format_transcript(entries)
        assert "Q: Who?" in result
        assert "A: Timmy" in result

    def test_groups_by_category(self):
        entries = [
            InterviewEntry(category="Identity", question="Q1", answer="A1"),
            InterviewEntry(category="Values", question="Q2", answer="A2"),
        ]
        result = format_transcript(entries)
        assert "--- Identity ---" in result
        assert "--- Values ---" in result
