"""
API тесты для расписания.
"""
import pytest
from django.utils import timezone
from datetime import timedelta
from rest_framework import status
from apps.schedule.models import Lesson, Subject
from apps.users.models import Client


@pytest.mark.django_db
@pytest.mark.api
class TestLessonAPI:
    """Тесты API занятий."""
    
    def test_list_lessons(self, authenticated_client, mentor_user, client_user):
        """Тест получения списка занятий."""
        client_profile = Client.objects.create(
            user=client_user,
            mentor=mentor_user
        )
        
        subject = Subject.objects.create(name='Математика')
        
        Lesson.objects.create(
            mentor=mentor_user,
            client=client_profile,
            subject=subject,
            title='Урок 1',
            start_time=timezone.now() + timedelta(hours=1),
            duration_minutes=60,
            status='scheduled'
        )
        
        response = authenticated_client.get('/api/schedule/lessons/')
        
        assert response.status_code == status.HTTP_200_OK
        assert len(response.data['results']) > 0
    
    def test_create_lesson(self, authenticated_client, mentor_user, client_user):
        """Тест создания занятия."""
        client_profile = Client.objects.create(
            user=client_user,
            mentor=mentor_user
        )
        
        subject = Subject.objects.create(name='Физика')
        
        data = {
            'client': client_profile.id,
            'subject': subject.id,
            'title': 'Новый урок',
            'start_time': (timezone.now() + timedelta(hours=1)).isoformat(),
            'duration_minutes': 60
        }
        
        response = authenticated_client.post('/api/schedule/lessons/', data)
        
        assert response.status_code == status.HTTP_201_CREATED
        assert response.data['title'] == 'Новый урок'
    
    def test_unauthorized_access(self, api_client):
        """Тест доступа без аутентификации."""
        response = api_client.get('/api/schedule/lessons/')
        
        assert response.status_code == status.HTTP_401_UNAUTHORIZED
    
    def test_update_lesson(self, authenticated_client, 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'
        )
        
        data = {'title': 'Новое название'}
        response = authenticated_client.patch(f'/api/schedule/lessons/{lesson.id}/', data)
        
        assert response.status_code == status.HTTP_200_OK
        assert response.data['title'] == 'Новое название'

