import uuid

from django.db import models
from django_extensions.db.models import TimeStampedModel

from vbv_lernwelt.core.models import User
from vbv_lernwelt.course.models import CourseSessionUser


class LearningMentor(models.Model):
    mentor = models.ForeignKey(User, on_delete=models.CASCADE)
    course = models.ForeignKey("course.Course", on_delete=models.CASCADE)

    participants = models.ManyToManyField(
        CourseSessionUser,
        related_name="participants",
        blank=True,
    )

    class Meta:
        unique_together = [["mentor", "course"]]
        verbose_name = "Lernbegleiter"
        verbose_name_plural = "Lernbegleiter"

    def __str__(self):
        return f"{self.mentor} ({self.course.title})"

    @property
    def course_sessions(self):
        return self.participants.values_list("course_session", flat=True).distinct()


class MentorInvitation(TimeStampedModel):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    email = models.EmailField()
    participant = models.ForeignKey(CourseSessionUser, on_delete=models.CASCADE)

    def __str__(self):
        return f"{self.email} ({self.participant})"

    class Meta:
        verbose_name = "Lernbegleiter Einladung"
        verbose_name_plural = "Lernbegleiter Einladungen"
        unique_together = [["email", "participant"]]
