from django.db import models
from BaseModels.base_models import BaseModel
from django.utils.translation import gettext_lazy as _

class Country(BaseModel):
    international_name = models.CharField(max_length=250, verbose_name=_('Международное название'), blank=True, null=True)
    official_name = models.CharField(max_length=250, verbose_name=_('Официальное название'), blank=True, null=True)

    short_code = models.CharField(max_length=2, verbose_name=_('Код страны по ISO3166-1:alpha2'))
    code = models.CharField(max_length=3, verbose_name=_('Код страны по ISO3166-1:alpha3'))
    num_code = models.CharField(max_length=3, verbose_name=_('Код страны по ISO3166-1:numeric'), blank=True, null=True)

    flag_img_url = models.URLField(verbose_name=_('Ссылка на изображение флага'), blank=True, null=True)

    geo_lat = models.CharField(max_length=20, verbose_name=_('GPS широта'), blank=True, null=True)
    geo_lon = models.CharField(max_length=20, verbose_name=_('GPS долгота'), blank=True, null=True)

    area_id = models.BigIntegerField(blank=True, null=True)

    parsing_finished_DT = models.DateTimeField(verbose_name=_('Дата и время завершения парсинга'), blank=True, null=True)

    def __str__(self):
        if self.name:
            return f'{self.name}'
        elif self.international_name:
            return f'{self.international_name}'
        elif self.official_name:
            return f'{self.official_name}'
        elif self.code:
            return f'{self.code}'


    class Meta:
        verbose_name = _('Страна')
        verbose_name_plural = _('Страны')
        ordering = ('name', 'code')


class City(BaseModel):

    country = models.ForeignKey(
        Country, verbose_name=_('Страна'), related_name='rel_cities_for_country', on_delete=models.CASCADE)

    geo_lat = models.CharField(max_length=20, verbose_name=_('GPS широта'), blank=True, null=True)
    geo_lon = models.CharField(max_length=20, verbose_name=_('GPS долгота'), blank=True, null=True)

    area_id = models.BigIntegerField(blank=True, null=True)

    parsing_finished_DT = models.DateTimeField(verbose_name=_('Дата и время завершения парсинга'), blank=True, null=True)

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

    def get_country_n_city_str(self):
        country = _('Неизвестно')
        city = self.name
        if self.country:
            country = self.country

        return f'{city} / {country}'


    class Meta:
        verbose_name = _('Город')
        verbose_name_plural = _('Города')
        ordering = ('name',)


class Airport(BaseModel):

    city = models.ForeignKey(
        City, verbose_name=_('Город'), related_name='rel_airports_for_city', on_delete=models.CASCADE,
        blank=True, null=True)

    international_name = models.CharField(max_length=250, verbose_name=_('Международное название'), blank=True, null=True)

    iata_code = models.CharField(max_length=3, verbose_name='IATA')
    icao_code = models.CharField(max_length=4, verbose_name='ICAO')

    geo_lat = models.CharField(max_length=20, verbose_name=_('GPS широта'), blank=True, null=True)
    geo_lon = models.CharField(max_length=20, verbose_name=_('GPS долгота'), blank=True, null=True)

    area_id = models.BigIntegerField(blank=True, null=True)

    parsing_finished_DT = models.DateTimeField(verbose_name=_('Дата и время завершения парсинга'), blank=True, null=True)

    def __str__(self):
        if self.name:
            return f'{self.name}'
        elif self.international_name:
            return f'{self.international_name}'
        elif self.iata_code:
            return f'{self.iata_code}'
        elif self.icao_code:
            return f'{self.icao_code}'
        else:
            return f'{self.id}'


    class Meta:
        verbose_name = _('Аэропорт')
        verbose_name_plural = _('Аэропорты')
        ordering = ('name', 'iata_code')