"""
Unit тесты для моделей домашних заданий.
"""
import pytest
from django.utils import timezone
from datetime import timedelta
from apps.homework.models import Homework, HomeworkSubmission, HomeworkFile
from apps.schedule.models import Lesson, Subject
from apps.users.models import User, Client


@pytest.mark.django_db
@pytest.mark.unit
class TestHomeworkModel:
    """Тесты модели Homework."""
    
    def test_create_homework(self, mentor_user):
        """Тест создания домашнего задания."""
        homework = Homework.objects.create(
            mentor=mentor_user,
            title='Решить задачи по алгебре',
            description='Учебник, страница 45, задачи 1-10',
            max_score=100,
            status='draft'
        )
        
        assert homework.mentor == mentor_user
        assert homework.title == 'Решить задачи по алгебре'
        assert homework.status == 'draft'
        assert homework.max_score == 100
    
    def test_homework_publish(self, mentor_user):
        """Тест публикации домашнего задания."""
        homework = Homework.objects.create(
            mentor=mentor_user,
            title='Тест',
            description='Описание',
            status='draft'
        )
        
        homework.publish()
        
        assert homework.status == 'published'
        assert homework.published_at is not None
    
    def test_homework_archive(self, mentor_user):
        """Тест архивирования домашнего задания."""
        homework = Homework.objects.create(
            mentor=mentor_user,
            title='Тест',
            description='Описание',
            status='published'
        )
        
        homework.archive()
        
        assert homework.status == 'archived'


@pytest.mark.django_db
@pytest.mark.unit
class TestHomeworkSubmissionModel:
    """Тесты модели HomeworkSubmission."""
    
    def test_create_submission(self, mentor_user, client_user):
        """Тест создания решения домашнего задания."""
        homework = Homework.objects.create(
            mentor=mentor_user,
            title='Тест',
            description='Описание',
            status='published'
        )
        
        submission = HomeworkSubmission.objects.create(
            homework=homework,
            student=client_user,
            text='Мое решение задачи',
            status='pending'
        )
        
        assert submission.homework == homework
        assert submission.student == client_user
        assert submission.status == 'pending'
        assert submission.text == 'Мое решение задачи'
    
    def test_submission_grade(self, mentor_user, client_user):
        """Тест оценивания решения."""
        homework = Homework.objects.create(
            mentor=mentor_user,
            title='Тест',
            description='Описание',
            status='published',
            max_score=100
        )
        
        submission = HomeworkSubmission.objects.create(
            homework=homework,
            student=client_user,
            text='Решение',
            status='pending'
        )
        
        submission.grade(score=85, feedback='Хорошая работа!')
        
        assert submission.status == 'graded'
        assert submission.score == 85
        assert submission.feedback == 'Хорошая работа!'
        assert submission.graded_at is not None

