from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from course.models import Course
from instructor.models import Instructor
from student.models import Student
from django.utils import timezone

class Lesson(models.Model):
    course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='lessons')
    instructor = models.ForeignKey(Instructor, on_delete=models.CASCADE, related_name='lessons')
    date = models.DateField(default=timezone.now)
    start_time = models.TimeField()
    end_time = models.TimeField()
    location = models.CharField(max_length=200, default='Учебный класс')
    max_students = models.PositiveIntegerField(default=10)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        ordering = ['date', 'start_time']
        verbose_name = 'Занятие'
        verbose_name_plural = 'Занятия'

    def __str__(self):
        return f"{self.course.title} - {self.date} {self.start_time}"

class LessonEnrollment(models.Model):
    lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE, related_name='enrollments')
    student = models.ForeignKey(Student, on_delete=models.CASCADE, related_name='enrollments')
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = ['lesson', 'student']
        verbose_name = 'Запись на занятие'
        verbose_name_plural = 'Записи на занятия'

    def __str__(self):
        return f"{self.student} - {self.lesson}" 