"""Tests for the TUI Interviews pane — helper functions and data parsing."""

MOCK_INTERVIEWS = [
    {
        "id": "aaa-111",
        "role_title": "Software Engineer",
        "interview_type": "behavioral",
        "scheduled_at": "2026-04-10T14:00:00Z",
        "status": "scheduled",
        "has_feedback": False,
        "company_slug": "google",
    },
    {
        "id": "bbb-222",
        "role_title": "Product Manager",
        "interview_type": "product_sense",
        "scheduled_at": "2026-04-01T10:00:00Z",
        "status": "completed",
        "has_feedback": True,
        "company_slug": None,
    },
]

MOCK_FEEDBACK = {
    "overall_score": 4,
    "summary": "Strong performance overall.",
    "strengths": ["Clear communication", "Good STAR format"],
    "improvements": ["Quantify impact more"],
    "question_scores": [
        {"question": "Tell me about a challenge", "score": 4, "notes": "Good depth"}
    ],
}


# ---------------------------------------------------------------------------
# _status_label
# ---------------------------------------------------------------------------


class TestStatusLabel:
    def test_scheduled(self):
        from skilark_cli.tui.screens.interviews import _status_label

        assert "cyan" in _status_label("scheduled")

    def test_in_progress(self):
        from skilark_cli.tui.screens.interviews import _status_label

        assert "yellow" in _status_label("in_progress")

    def test_completed(self):
        from skilark_cli.tui.screens.interviews import _status_label

        assert "green" in _status_label("completed")

    def test_cancelled(self):
        from skilark_cli.tui.screens.interviews import _status_label

        assert "dim" in _status_label("cancelled")

    def test_failed(self):
        from skilark_cli.tui.screens.interviews import _status_label

        assert "red" in _status_label("failed")

    def test_unknown_returns_raw(self):
        from skilark_cli.tui.screens.interviews import _status_label

        assert _status_label("foo") == "foo"


# ---------------------------------------------------------------------------
# _render_feedback
# ---------------------------------------------------------------------------


class TestRenderFeedback:
    def test_includes_score(self):
        from skilark_cli.tui.screens.interviews import _render_feedback

        text = _render_feedback(MOCK_FEEDBACK)
        assert "4/5" in text

    def test_includes_stars(self):
        from skilark_cli.tui.screens.interviews import _render_feedback

        text = _render_feedback(MOCK_FEEDBACK)
        # 4 filled stars out of 5
        assert "★★★★" in text

    def test_includes_summary(self):
        from skilark_cli.tui.screens.interviews import _render_feedback

        text = _render_feedback(MOCK_FEEDBACK)
        assert "Strong performance overall." in text

    def test_includes_strengths(self):
        from skilark_cli.tui.screens.interviews import _render_feedback

        text = _render_feedback(MOCK_FEEDBACK)
        assert "Clear communication" in text
        assert "Good STAR format" in text

    def test_includes_improvements(self):
        from skilark_cli.tui.screens.interviews import _render_feedback

        text = _render_feedback(MOCK_FEEDBACK)
        assert "Quantify impact" in text

    def test_includes_question_scores(self):
        from skilark_cli.tui.screens.interviews import _render_feedback

        text = _render_feedback(MOCK_FEEDBACK)
        assert "Tell me about a challenge" in text
        assert "Good depth" in text

    def test_empty_feedback_with_score_only(self):
        from skilark_cli.tui.screens.interviews import _render_feedback

        text = _render_feedback({"overall_score": 3, "summary": "OK"})
        assert "3/5" in text
        assert "OK" in text

    def test_missing_optional_sections_no_error(self):
        from skilark_cli.tui.screens.interviews import _render_feedback

        # Should not raise even with minimal input
        text = _render_feedback({"overall_score": 5})
        assert "5/5" in text

    def test_zero_score_renders(self):
        from skilark_cli.tui.screens.interviews import _render_feedback

        text = _render_feedback({"overall_score": 0, "summary": "No show"})
        assert "0/5" in text


# ---------------------------------------------------------------------------
# InterviewsPane — instantiation and initial state
# ---------------------------------------------------------------------------


class TestInterviewsPane:
    def test_default_view_is_list(self):
        from skilark_cli.tui.screens.interviews import InterviewsPane

        pane = InterviewsPane(client=None)
        assert pane._view == "list"

    def test_interviews_stored(self):
        from skilark_cli.tui.screens.interviews import InterviewsPane

        pane = InterviewsPane(client=None)
        pane._interviews = MOCK_INTERVIEWS
        assert len(pane._interviews) == 2

    def test_interviews_default_empty(self):
        from skilark_cli.tui.screens.interviews import InterviewsPane

        pane = InterviewsPane(client=None)
        assert pane._interviews == []

    def test_client_stored(self):
        from unittest.mock import MagicMock
        from skilark_cli.tui.screens.interviews import InterviewsPane

        mock_client = MagicMock()
        pane = InterviewsPane(client=mock_client)
        assert pane.client is mock_client

    def test_bindings_include_refresh(self):
        from skilark_cli.tui.screens.interviews import InterviewsPane

        keys = {b.key for b in InterviewsPane.BINDINGS}
        assert "r" in keys

    def test_bindings_include_back(self):
        from skilark_cli.tui.screens.interviews import InterviewsPane

        keys = {b.key for b in InterviewsPane.BINDINGS}
        assert "b" in keys
        assert "escape" in keys

    def test_bindings_include_feedback(self):
        from skilark_cli.tui.screens.interviews import InterviewsPane

        keys = {b.key for b in InterviewsPane.BINDINGS}
        assert "f" in keys

    def test_bindings_include_book(self):
        from skilark_cli.tui.screens.interviews import InterviewsPane

        keys = {b.key for b in InterviewsPane.BINDINGS}
        assert "n" in keys


class TestInterviewBookModal:
    """Unit tests for the booking modal."""

    def test_interview_type_options(self):
        from skilark_cli.tui.screens.interview_book_modal import INTERVIEW_TYPE_OPTIONS

        types = [v for _, v in INTERVIEW_TYPE_OPTIONS]
        assert "behavioral" in types
        assert "technical_coding" in types
        assert "system_design" in types
        assert len(types) == 8

    def test_date_options_14_days(self):
        from skilark_cli.tui.screens.interview_book_modal import _date_options

        options = _date_options()
        assert len(options) == 14

    def test_date_options_starts_with_today(self):
        from skilark_cli.tui.screens.interview_book_modal import _date_options

        options = _date_options()
        assert "Today" in options[0][0]

    def test_date_options_tomorrow(self):
        from skilark_cli.tui.screens.interview_book_modal import _date_options

        options = _date_options()
        assert "Tomorrow" in options[1][0]

    def test_date_options_values_are_iso(self):
        from datetime import date
        from skilark_cli.tui.screens.interview_book_modal import _date_options

        options = _date_options()
        for _, val in options:
            date.fromisoformat(val)  # raises if invalid

    def test_time_options_48_slots(self):
        from skilark_cli.tui.screens.interview_book_modal import _time_options

        options = _time_options()
        assert len(options) == 48  # 24 hours * 2 slots

    def test_time_options_format(self):
        from skilark_cli.tui.screens.interview_book_modal import _time_options

        options = _time_options()
        # First slot: midnight
        assert options[0][1] == "00:00"
        # Last slot: 11:30 PM
        assert options[-1][1] == "23:30"
        # All values match HH:MM
        for _, val in options:
            assert len(val) == 5
            assert val[2] == ":"

    def test_time_options_labels_have_am_pm(self):
        from skilark_cli.tui.screens.interview_book_modal import _time_options

        options = _time_options()
        for label, _ in options:
            assert "AM" in label or "PM" in label

    def test_default_date_is_today_or_tomorrow(self):
        from datetime import datetime, timedelta
        from skilark_cli.tui.screens.interview_book_modal import _default_date

        now = datetime.now().astimezone()
        today = now.date().isoformat()
        tomorrow = (now.date() + timedelta(days=1)).isoformat()
        # Falls back to tomorrow when no time slots remain today
        assert _default_date() in (today, tomorrow)

    def test_default_time_slot_format(self):
        from skilark_cli.tui.screens.interview_book_modal import _default_time_slot

        slot = _default_time_slot()
        assert len(slot) == 5
        assert slot[2] == ":"
        minutes = int(slot[3:])
        assert minutes in (0, 30)

    def test_default_time_slot_is_on_the_half_hour(self):
        from skilark_cli.tui.screens.interview_book_modal import _default_time_slot

        slot = _default_time_slot()
        minutes = int(slot[3:])
        assert minutes in (0, 30), f"Expected :00 or :30, got :{minutes:02d}"

    def test_local_tz_name_is_non_empty(self):
        from skilark_cli.tui.screens.interview_book_modal import _LOCAL_TZ_NAME

        assert _LOCAL_TZ_NAME  # e.g. "EDT", "PST", "UTC"
        assert len(_LOCAL_TZ_NAME) >= 2

    def test_modal_can_instantiate(self):
        from skilark_cli.tui.screens.interview_book_modal import InterviewBookModal

        modal = InterviewBookModal(client=None)
        assert modal is not None
