"""
Tests for Intent Router
"""
import pytest
from services.intent_router import IntentRouter
from models.task import TaskType


class TestIntentRouter:
    """Tests for intent detection."""

    @pytest.fixture
    def router(self):
        """Create intent router instance."""
        return IntentRouter()

    @pytest.mark.asyncio
    async def test_detect_student_observation(self, router):
        """Test detecting student observation intent."""
        text = "Notiz zu Max: heute wiederholt gestoert"
        intent = await router.detect_intent(text)

        assert intent is not None
        assert intent.type == TaskType.STUDENT_OBSERVATION
        assert intent.confidence > 0.5
        assert "student_name" in intent.parameters or intent.is_actionable

    @pytest.mark.asyncio
    async def test_detect_reminder(self, router):
        """Test detecting reminder intent (without specific schedule)."""
        text = "Erinner mich an den Elternsprechtag"
        intent = await router.detect_intent(text)

        assert intent is not None
        assert intent.type == TaskType.REMINDER
        assert intent.confidence > 0.5

    @pytest.mark.asyncio
    async def test_detect_reminder_schedule(self, router):
        """Test detecting scheduled reminder intent (with 'morgen')."""
        text = "Erinner mich morgen an Hausaufgabenkontrolle"
        intent = await router.detect_intent(text)

        assert intent is not None
        assert intent.type == TaskType.REMINDER_SCHEDULE
        assert intent.confidence > 0.5

    @pytest.mark.asyncio
    async def test_detect_homework_check(self, router):
        """Test detecting homework check intent."""
        text = "7b Mathe Hausaufgabe kontrollieren"
        intent = await router.detect_intent(text)

        assert intent is not None
        assert intent.type == TaskType.HOMEWORK_CHECK
        assert intent.confidence > 0.5

    @pytest.mark.asyncio
    async def test_detect_worksheet_generate(self, router):
        """Test detecting worksheet generation intent."""
        text = "Nimm Vokabeln Lektion 4, mach 3 Lueckentexte"
        intent = await router.detect_intent(text)

        assert intent is not None
        assert intent.type == TaskType.WORKSHEET_GENERATE
        assert intent.confidence > 0.5

    @pytest.mark.asyncio
    async def test_detect_parent_letter(self, router):
        """Test detecting parent letter intent."""
        text = "Neutraler Elternbrief wegen wiederholter Stoerungen"
        intent = await router.detect_intent(text)

        assert intent is not None
        assert intent.type == TaskType.PARENT_LETTER
        assert intent.confidence > 0.5

    @pytest.mark.asyncio
    async def test_detect_class_message(self, router):
        """Test detecting class message intent."""
        text = "Nachricht an 8a: Hausaufgaben bis Mittwoch"
        intent = await router.detect_intent(text)

        assert intent is not None
        assert intent.type == TaskType.CLASS_MESSAGE
        assert intent.confidence > 0.5

    @pytest.mark.asyncio
    async def test_detect_quick_activity(self, router):
        """Test detecting quick activity intent."""
        text = "10 Minuten Einstieg, 5 Aufgaben"
        intent = await router.detect_intent(text)

        assert intent is not None
        assert intent.type == TaskType.QUICK_ACTIVITY
        assert intent.confidence > 0.5

    @pytest.mark.asyncio
    async def test_detect_quiz_generate(self, router):
        """Test detecting quiz generation intent."""
        text = "10-Minuten Vokabeltest mit Loesungen"
        intent = await router.detect_intent(text)

        assert intent is not None
        assert intent.type == TaskType.QUIZ_GENERATE
        assert intent.confidence > 0.5

    @pytest.mark.asyncio
    async def test_detect_canvas_edit(self, router):
        """Test detecting canvas edit intent."""
        text = "Ueberschriften groesser, Zeilenabstand kleiner"
        intent = await router.detect_intent(text)

        assert intent is not None
        assert intent.type == TaskType.CANVAS_EDIT
        assert intent.confidence > 0.5

    @pytest.mark.asyncio
    async def test_detect_canvas_layout(self, router):
        """Test detecting canvas layout intent."""
        text = "Alles auf eine Seite, Drucklayout A4"
        intent = await router.detect_intent(text)

        assert intent is not None
        assert intent.type == TaskType.CANVAS_LAYOUT
        assert intent.confidence > 0.5

    @pytest.mark.asyncio
    async def test_detect_operator_checklist(self, router):
        """Test detecting operator checklist intent."""
        text = "Operatoren-Checkliste fuer diese Aufgabe"
        intent = await router.detect_intent(text)

        assert intent is not None
        assert intent.type == TaskType.OPERATOR_CHECKLIST
        assert intent.is_actionable is False  # Query, not action

    @pytest.mark.asyncio
    async def test_detect_eh_passage(self, router):
        """Test detecting EH passage intent."""
        text = "Erwartungshorizont-Passage zu diesem Thema"
        intent = await router.detect_intent(text)

        assert intent is not None
        assert intent.type == TaskType.EH_PASSAGE
        assert intent.is_actionable is False  # Query, not action

    @pytest.mark.asyncio
    async def test_detect_task_summary(self, router):
        """Test detecting task summary intent."""
        text = "Fasse alle offenen Tasks dieser Woche zusammen"
        intent = await router.detect_intent(text)

        assert intent is not None
        assert intent.type == TaskType.TASK_SUMMARY
        assert intent.is_actionable is False  # Query, not action

    @pytest.mark.asyncio
    async def test_no_intent_detected(self, router):
        """Test that random text returns no intent."""
        text = "Das Wetter ist heute schoen"
        intent = await router.detect_intent(text)

        # Should return None or low confidence intent
        if intent:
            assert intent.confidence < 0.5

    @pytest.mark.asyncio
    async def test_umlaut_normalization(self, router):
        """Test that umlauts are handled correctly."""
        text = "Notiz zu Müller: braucht Förderung"
        intent = await router.detect_intent(text)

        assert intent is not None
        assert intent.type == TaskType.STUDENT_OBSERVATION

    @pytest.mark.asyncio
    async def test_extract_time_parameter(self, router):
        """Test that time is extracted from text."""
        text = "Erinner mich morgen 7:30 an Konferenz"
        intent = await router.detect_intent(text)

        assert intent is not None
        if "time" in intent.parameters:
            assert "7:30" in intent.parameters["time"]
