# Copyright (c) 2025 Patrick Motsch
# All rights reserved.
"""
Tests for CommCoach data models.
Validates model creation, defaults, enums, and serialization.
"""

import pytest
import json
from ..datamodelCommcoach import (
    CoachingContext, CoachingContextStatus, CoachingContextCategory,
    CoachingSession, CoachingSessionStatus,
    CoachingMessage, CoachingMessageRole, CoachingMessageContentType,
    CoachingTask, CoachingTaskStatus, CoachingTaskPriority,
    CoachingScore, CoachingScoreTrend,
    CoachingUserProfile,
    CreateContextRequest, SendMessageRequest, CreateTaskRequest,
    UpdateTaskStatusRequest, DashboardData,
)


class TestCoachingContext:
    def test_createWithDefaults(self):
        ctx = CoachingContext(
            userId="user-1",
            mandateId="mandate-1",
            instanceId="instance-1",
            title="Test Topic",
        )
        assert ctx.userId == "user-1"
        assert ctx.title == "Test Topic"
        assert ctx.status == CoachingContextStatus.ACTIVE
        assert ctx.category == CoachingContextCategory.CUSTOM
        assert ctx.sessionCount == 0
        assert ctx.id is not None and len(ctx.id) > 0

    def test_createWithAllFields(self):
        ctx = CoachingContext(
            userId="user-1",
            mandateId="mandate-1",
            instanceId="instance-1",
            title="Conflict Management",
            description="Working on conflict with team",
            category=CoachingContextCategory.CONFLICT,
            status=CoachingContextStatus.PAUSED,
            goals=json.dumps([{"id": "g1", "text": "Resolve conflict", "status": "open"}]),
        )
        assert ctx.category == CoachingContextCategory.CONFLICT
        assert ctx.status == CoachingContextStatus.PAUSED
        goals = json.loads(ctx.goals)
        assert len(goals) == 1

    def test_modelDump(self):
        ctx = CoachingContext(
            userId="u1", mandateId="m1", instanceId="i1", title="T"
        )
        data = ctx.model_dump()
        assert isinstance(data, dict)
        assert data["userId"] == "u1"
        assert "id" in data


class TestCoachingSession:
    def test_createWithDefaults(self):
        session = CoachingSession(
            contextId="ctx-1",
            userId="user-1",
            mandateId="m1",
            instanceId="i1",
        )
        assert session.status == CoachingSessionStatus.ACTIVE
        assert session.messageCount == 0
        assert session.durationSeconds == 0
        assert session.emailSent is False

    def test_competenceScoreRange(self):
        session = CoachingSession(
            contextId="ctx-1", userId="u1", mandateId="m1", instanceId="i1",
            competenceScore=75.5,
        )
        assert session.competenceScore == 75.5


class TestCoachingMessage:
    def test_createUserMessage(self):
        msg = CoachingMessage(
            sessionId="s1", contextId="c1", userId="u1",
            role=CoachingMessageRole.USER,
            content="Hello coach",
        )
        assert msg.role == CoachingMessageRole.USER
        assert msg.contentType == CoachingMessageContentType.TEXT

    def test_createAssistantMessage(self):
        msg = CoachingMessage(
            sessionId="s1", contextId="c1", userId="u1",
            role=CoachingMessageRole.ASSISTANT,
            content="Welcome to coaching!",
        )
        assert msg.role == CoachingMessageRole.ASSISTANT


class TestCoachingTask:
    def test_createWithDefaults(self):
        task = CoachingTask(
            contextId="c1", userId="u1", mandateId="m1",
            title="Practice active listening",
        )
        assert task.status == CoachingTaskStatus.OPEN
        assert task.priority == CoachingTaskPriority.MEDIUM

    def test_allStatuses(self):
        for status in CoachingTaskStatus:
            task = CoachingTask(
                contextId="c1", userId="u1", mandateId="m1",
                title="Task", status=status,
            )
            assert task.status == status


class TestCoachingScore:
    def test_createScore(self):
        score = CoachingScore(
            contextId="c1", sessionId="s1", userId="u1", mandateId="m1",
            dimension="empathy", score=72.5,
            trend=CoachingScoreTrend.IMPROVING,
            evidence="Shows increasing empathy",
        )
        assert score.dimension == "empathy"
        assert score.score == 72.5
        assert score.trend == CoachingScoreTrend.IMPROVING


class TestCoachingUserProfile:
    def test_createWithDefaults(self):
        profile = CoachingUserProfile(
            userId="u1", mandateId="m1", instanceId="i1",
        )
        assert profile.dailyReminderEnabled is False
        assert profile.emailSummaryEnabled is True
        assert profile.streakDays == 0


class TestApiModels:
    def test_createContextRequest(self):
        req = CreateContextRequest(title="Test", goals=["Goal 1", "Goal 2"])
        assert req.title == "Test"
        assert len(req.goals) == 2

    def test_sendMessageRequest(self):
        req = SendMessageRequest(content="Hello")
        assert req.content == "Hello"
        assert req.contentType == CoachingMessageContentType.TEXT

    def test_createTaskRequest(self):
        req = CreateTaskRequest(title="Task 1")
        assert req.title == "Task 1"
        assert req.priority == CoachingTaskPriority.MEDIUM

    def test_updateTaskStatusRequest(self):
        req = UpdateTaskStatusRequest(status=CoachingTaskStatus.DONE)
        assert req.status == CoachingTaskStatus.DONE

    def test_dashboardData(self):
        data = DashboardData(totalContexts=3, activeContexts=2, totalSessions=10)
        assert data.totalContexts == 3
        assert data.openTasks == 0
