from django.contrib.gis.db import models


class Country(models.Model):
    """
    Country model
    """
    name = models.CharField(max_length = 100)
    geo = models.MultiPolygonField(null=True)

    # internal
    created = models.DateTimeField('date of creation', auto_now_add=True)
    updated = models.DateTimeField('date last update', auto_now=True)

    def __str__(self):
        return f'{self.name}'

    class Meta:
        verbose_name = "País"
        verbose_name_plural = "Paises"


class Region(models.Model):
    """
    Region model
    """
    name = models.CharField(max_length=250)
    country = models.ForeignKey(Country,on_delete=models.DO_NOTHING,related_name='regions')
    geo = models.MultiPolygonField(null=True)

    # internal
    created = models.DateTimeField('date of creation', auto_now_add=True)
    updated = models.DateTimeField('date last update', auto_now=True)

    def __str__(self):
        return f'{self.name} [{self.country}]'

    class Meta:
        verbose_name = "Región"
        verbose_name_plural = "Regiones"


class Province(models.Model):
    """
    Country model
    """
    name = models.CharField(max_length = 100)
    region = models.ForeignKey(Region, on_delete=models.DO_NOTHING, related_name='province')
    geo = models.MultiPolygonField(null=True)

    # internal
    created = models.DateTimeField('date of creation', auto_now_add=True)
    updated = models.DateTimeField('date last update', auto_now=True)

    def __str__(self):
        return f'{self.name} [{self.region}]'

    class Meta:
        verbose_name = "Provincia"
        verbose_name_plural = "Provincias"


class City(models.Model):
    """
    City model
    """
    name = models.CharField(max_length = 250)
    province = models.ForeignKey(Province, on_delete=models.DO_NOTHING, related_name='city', null=True)
    geo = models.MultiPolygonField(null=True)

    # internal
    created = models.DateTimeField('date of creation', auto_now_add=True)
    updated = models.DateTimeField('date last update', auto_now=True)

    def __str__(self):
        if self.province:
            return f'{self.name} [{self.province}]'
        else:
            return f'{self.name}'

    class Meta:
        verbose_name = "Municipio"
        verbose_name_plural = "Municipios"
