from django.db import models


class PublicBody(models.Model):
    name = models.CharField(max_length=255, unique=True)
    slug = models.SlugField(max_length=255, unique=True)
    website = models.URLField(blank=True)
    description = models.TextField(blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = "Gemeinde"
        verbose_name_plural = "Gemeinden"
        ordering = ["name"]

    def __str__(self) -> str:  # pragma: no cover - trivial
        return self.name


class Party(models.Model):
    name = models.CharField(max_length=255, unique=True)
    abbreviation = models.CharField(max_length=50, blank=True)
    color = models.CharField(max_length=7, blank=True, help_text="Hex-Farbe, z. B. #00AAFF")

    class Meta:
        verbose_name = "Partei"
        verbose_name_plural = "Parteien"
        ordering = ["name"]

    def __str__(self) -> str:  # pragma: no cover
        return self.abbreviation or self.name


class Member(models.Model):
    first_name = models.CharField(max_length=150)
    last_name = models.CharField(max_length=150)
    email = models.EmailField(blank=True)
    public_body = models.ForeignKey(
        PublicBody, related_name="members", on_delete=models.SET_NULL, null=True, blank=True
    )
    party = models.ForeignKey(Party, related_name="members", on_delete=models.SET_NULL, null=True, blank=True)

    class Meta:
        verbose_name = "Stadträt:in"
        verbose_name_plural = "Stadträt:innen"
        ordering = ["last_name", "first_name"]
        unique_together = ("first_name", "last_name", "public_body")

    def __str__(self) -> str:  # pragma: no cover
        return f"{self.first_name} {self.last_name}"


class Question(models.Model):
    title = models.CharField(max_length=255)
    body = models.TextField()
    member = models.ForeignKey(Member, related_name="questions", on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = "Frage"
        verbose_name_plural = "Fragen"
        ordering = ["-created_at"]

    def __str__(self) -> str:  # pragma: no cover
        return self.title


class Answer(models.Model):
    question = models.ForeignKey(Question, related_name="answers", on_delete=models.CASCADE)
    answered_by = models.ForeignKey(Member, related_name="answers", on_delete=models.SET_NULL, null=True, blank=True)
    body = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = "Antwort"
        verbose_name_plural = "Antworten"
        ordering = ["created_at"]

    def __str__(self) -> str:  # pragma: no cover
        return f"Antwort zu: {self.question.title}"


class Vote(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField(blank=True)
    public_body = models.ForeignKey(PublicBody, related_name="votes", on_delete=models.CASCADE)
    date = models.DateField(null=True, blank=True)
    members = models.ManyToManyField(Member, related_name="votes", blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = "Abstimmung"
        verbose_name_plural = "Abstimmungen"
        ordering = ["-date", "-created_at"]

    def __str__(self) -> str:  # pragma: no cover
        return self.title 