# Copyright (c) 2025 Patrick Motsch
# All rights reserved.
"""Tests for CommCoach context retrieval (intent detection, session lookup)."""

import pytest
from datetime import datetime

from ..serviceCommcoachContextRetrieval import (
    detectIntent,
    RetrievalIntent,
    buildSessionSummariesForPrompt,
    findSessionByDate,
    searchSessionsByTopic,
    _parseDateFromMessage,
)


class TestDetectIntent:
    def test_normal(self):
        assert detectIntent("Ich habe ein Problem mit meinem Team") == RetrievalIntent.NORMAL
        assert detectIntent("Wie kann ich besser delegieren?") == RetrievalIntent.NORMAL

    def test_summarizeAll(self):
        assert detectIntent("Fasse alles zusammen") == RetrievalIntent.SUMMARIZE_ALL
        assert detectIntent("Gib mir eine Zusammenfassung des gesamten Chats") == RetrievalIntent.SUMMARIZE_ALL

    def test_recallSession(self):
        assert detectIntent("Was haben wir am 01.02.2026 besprochen?") == RetrievalIntent.RECALL_SESSION
        assert detectIntent("Session vom 15.03.2026") == RetrievalIntent.RECALL_SESSION

    def test_recallTopic(self):
        assert detectIntent("Erinnerst du dich an das Thema Delegation?") == RetrievalIntent.RECALL_TOPIC
        assert detectIntent("Was war das Thema von vor 5 Sessions?") == RetrievalIntent.RECALL_TOPIC


class TestParseDateFromMessage:
    def test_dd_mm_yyyy(self):
        d = _parseDateFromMessage("am 01.02.2026")
        assert d is not None
        assert d.year == 2026
        assert d.month == 2
        assert d.day == 1

    def test_iso(self):
        d = _parseDateFromMessage("2026-02-01")
        assert d is not None
        assert d.year == 2026
        assert d.month == 2
        assert d.day == 1


class TestFindSessionByDate:
    def test_findsClosest(self):
        sessions = [
            {"id": "1", "status": "completed", "startedAt": "2026-02-01T10:00:00Z"},
            {"id": "2", "status": "completed", "startedAt": "2026-02-05T10:00:00Z"},
        ]
        target = datetime(2026, 2, 4)
        found = findSessionByDate(sessions, target)
        assert found is not None
        assert found["id"] == "2"

    def test_findsExactDate(self):
        sessions = [
            {"id": "1", "status": "completed", "startedAt": "2026-02-01T10:00:00Z"},
            {"id": "2", "status": "completed", "startedAt": "2026-02-05T10:00:00Z"},
        ]
        target = datetime(2026, 2, 1)
        found = findSessionByDate(sessions, target)
        assert found is not None
        assert found["id"] == "1"

    def test_returnsNoneForEmpty(self):
        assert findSessionByDate([], datetime(2026, 2, 1)) is None


class TestSearchSessionsByTopic:
    def test_matchesKeyTopics(self):
        sessions = [
            {"id": "1", "status": "completed", "summary": "Delegation besprochen", "keyTopics": '["Delegation", "Feedback"]'},
        ]
        result = searchSessionsByTopic(sessions, "Delegation")
        assert len(result) == 1
        assert result[0]["id"] == "1"

    def test_matchesSummary(self):
        sessions = [
            {"id": "1", "status": "completed", "summary": "Konflikt mit Vorgesetztem", "keyTopics": None},
        ]
        result = searchSessionsByTopic(sessions, "Konflikt")
        assert len(result) == 1


class TestBuildSessionSummariesForPrompt:
    def test_excludesSession(self):
        sessions = [
            {"id": "1", "status": "completed", "summary": "A", "startedAt": "2026-02-01T10:00:00Z"},
            {"id": "2", "status": "completed", "summary": "B", "startedAt": "2026-02-02T10:00:00Z"},
        ]
        result = buildSessionSummariesForPrompt(sessions, excludeSessionId="2", limit=5)
        assert len(result) == 1
        assert result[0]["summary"] == "A"
        assert result[0]["date"]
