"""Address models"""

# Django
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import gettext_lazy as _

# Utils
from {{project_slug}}.utils.models import PLModel


class Address(PLModel):
    """Address model"""

    address = models.CharField(max_length=150)
    state = models.ForeignKey("locations.State", on_delete=models.PROTECT)
    municipality = models.ForeignKey("locations.Municipality", on_delete=models.PROTECT)


    def clean(self):
        super().clean()
        if self.state and self.municipality and self.municipality.state != self.state:
            raise ValidationError(
                _("The selected municipality does not belong to the chosen state.")
            )

    def save(self, *args, **kwargs):
        self.full_clean()
        super().save(*args, **kwargs)

    def __str__(self):
        return self.address
