"""Tests for the six-dimension analyzer."""

import pytest

from ai_checkup.analyzer import analyze, piecewise_score
from ai_checkup.models import CheckupResult


class TestPiecewiseScore:
    def test_at_breakpoint(self):
        bps = [(0, 0), (0.5, 50), (1.0, 100)]
        assert piecewise_score(0, bps) == 0
        assert piecewise_score(0.5, bps) == 50
        assert piecewise_score(1.0, bps) == 100

    def test_between_breakpoints(self):
        bps = [(0, 0), (0.5, 50), (1.0, 100)]
        assert piecewise_score(0.25, bps) == 25

    def test_below_range(self):
        bps = [(0.1, 10), (1.0, 100)]
        assert piecewise_score(0, bps) == 10

    def test_above_range(self):
        bps = [(0, 0), (0.5, 50)]
        assert piecewise_score(1.0, bps) == 50


class TestAnalyzer:
    def test_analyze_returns_result(self, sample_conversations):
        result = analyze(sample_conversations)
        assert isinstance(result, CheckupResult)
        assert result.total_conversations == len(sample_conversations)

    def test_all_dimensions_present(self, sample_conversations):
        result = analyze(sample_conversations)
        expected_dims = ["questioning", "judgment", "hands_on",
                         "deep_thinking", "execution", "narrative"]
        for dim in expected_dims:
            assert dim in result.dimensions
            assert result.dimensions[dim].name != ""
            assert 0 <= result.dimensions[dim].score <= 100

    def test_scores_are_reasonable(self, sample_conversations):
        result = analyze(sample_conversations)
        # With our mock data mix, questioning should have some high-level questions
        q_score = result.dimensions["questioning"].score
        assert q_score > 0  # At least some non-L1 questions

        # Judgment should be non-zero since we have judgment conversations
        j_score = result.dimensions["judgment"].score
        assert j_score > 0

    def test_weekly_data_present(self, sample_conversations):
        result = analyze(sample_conversations)
        for dim in result.dimensions.values():
            assert len(dim.weekly_data) > 0

    def test_insights_generated(self, sample_conversations):
        result = analyze(sample_conversations)
        assert len(result.insights) > 0

    def test_raw_analysis_has_distribution(self, sample_conversations):
        result = analyze(sample_conversations)
        assert "question_distribution" in result.raw_analysis
        assert "judgment_types" in result.raw_analysis

    def test_period_format(self, sample_conversations):
        result = analyze(sample_conversations)
        assert "~" in result.period
        assert "W" in result.period
