"""
Unit тесты для сериализаторов расписания.
"""
import pytest
from django.utils import timezone
from datetime import timedelta
from apps.schedule.serializers import LessonSerializer, LessonCreateSerializer
from apps.schedule.models import Lesson, Subject
from apps.users.models import Client


@pytest.mark.django_db
@pytest.mark.unit
class TestLessonSerializer:
    """Тесты сериализатора Lesson."""
    
    def test_lesson_serialization(self, mentor_user, client_user):
        """Тест сериализации занятия."""
        client_profile = Client.objects.create(
            user=client_user,
            mentor=mentor_user
        )
        
        subject = Subject.objects.create(name='Математика')
        
        lesson = Lesson.objects.create(
            mentor=mentor_user,
            client=client_profile,
            subject=subject,
            title='Урок алгебры',
            start_time=timezone.now() + timedelta(hours=1),
            duration_minutes=60,
            status='scheduled'
        )
        
        serializer = LessonSerializer(lesson)
        data = serializer.data
        
        assert data['title'] == 'Урок алгебры'
        assert data['status'] == 'scheduled'
        assert data['duration_minutes'] == 60
        assert 'mentor' in data
        assert 'client' in data


@pytest.mark.django_db
@pytest.mark.unit
class TestLessonCreateSerializer:
    """Тесты сериализатора создания занятия."""
    
    def test_lesson_creation_validation(self, mentor_user, client_user):
        """Тест валидации при создании занятия."""
        client_profile = Client.objects.create(
            user=client_user,
            mentor=mentor_user
        )
        
        subject = Subject.objects.create(name='Физика')
        
        data = {
            'mentor': mentor_user.id,
            'client': client_profile.id,
            'subject': subject.id,
            'title': 'Новый урок',
            'start_time': (timezone.now() + timedelta(hours=1)).isoformat(),
            'duration_minutes': 60
        }
        
        serializer = LessonCreateSerializer(data=data)
        assert serializer.is_valid() is True
        
        lesson = serializer.save()
        assert lesson.title == 'Новый урок'
        assert lesson.mentor == mentor_user

