from django.db import models

from vaccine_card.vaccination.models import Vaccine


class State(models.Model):
    name = models.CharField(max_length=20, verbose_name='Nome')

    class Meta:
        verbose_name = 'Unidade Federativa'

    def __str__(self):
        return self.name


class City(models.Model):
    name = models.CharField(max_length=50, verbose_name='Nome')
    state = models.ForeignKey(State, on_delete=models.CASCADE, verbose_name=State._meta.verbose_name)

    class Meta:
        verbose_name = 'Município'

    def __str__(self):
        return self.name


class Address(models.Model):
    logradouro = models.CharField(max_length=150, verbose_name='Logradouro')
    numero = models.CharField(max_length=4, verbose_name='Número')
    complemento = models.CharField(max_length=50, null=True, blank=True, verbose_name='Complemento')
    bairro = models.CharField(max_length=150, verbose_name='Bairro')
    cep = models.CharField(max_length=8, verbose_name='CEP')

    # state = models.ForeignKey(State, on_delete=models.CASCADE, verbose_name=State._meta.verbose_name)
    city = models.ForeignKey(City, on_delete=models.CASCADE, verbose_name=City._meta.verbose_name)

    class Meta:
        verbose_name = 'Endereço'


class HealthCenter(models.Model):
    cnes = models.CharField(max_length=7, verbose_name='CNES')
    cnpj = models.CharField(max_length=14, verbose_name='CNPJ')
    name = models.CharField(max_length=255, verbose_name='Razão Social')

    created_at = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name='Criado em:')
    updated_at = models.DateTimeField(auto_now_add=False, auto_now=True, verbose_name='Atualizado em:')

    address = models.ManyToManyField(Address, verbose_name=Address._meta.verbose_name)

    class Meta:
        verbose_name = 'Estabelecimento de Saúde'
        verbose_name_plural = 'Estabelecimentos de Saúde'

    def __str__(self):
        return self.name


class Stock(models.Model):
    lot = models.PositiveSmallIntegerField(verbose_name='Lote')

    created_at = models.DateTimeField(auto_now_add=True, auto_now=False, verbose_name='Criado em:')
    updated_at = models.DateTimeField(auto_now_add=False, auto_now=True, verbose_name='Atualizado em:')

    health_center = models.ForeignKey(HealthCenter, on_delete=models.CASCADE,
                                      verbose_name=HealthCenter._meta.verbose_name)

    vaccines = models.ManyToManyField(Vaccine, through='VaccineStock', verbose_name=Vaccine._meta.verbose_name)

    class Meta:
        verbose_name = 'Estoque'


class VaccineStock(models.Model):
    amount = models.PositiveSmallIntegerField(verbose_name='Quantidade recebida')
    remaining = models.PositiveSmallIntegerField(verbose_name='Quantidade restante')

    vaccine = models.ForeignKey(Vaccine, on_delete=models.DO_NOTHING, verbose_name=Vaccine._meta.verbose_name)
    stock = models.ForeignKey(Stock, on_delete=models.DO_NOTHING, verbose_name=Stock._meta.verbose_name)

    class Meta:
        verbose_name = 'Estoque de Vacina'

    def __str__(self):
        return self.vaccine.name

[(7, 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(20)', 'verbose_name': '"""Nome"""'}), False, 'from django.db import models\n'), (17, 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(50)', 'verbose_name': '"""Nome"""'}), False, 'from django.db import models\n'), (18, 'django.db.models.ForeignKey', 'models.ForeignKey', (['State'], {'on_delete': 'models.CASCADE', 'verbose_name': 'State._meta.verbose_name'}), False, 'from django.db import models\n'), (28, 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(150)', 'verbose_name': '"""Logradouro"""'}), False, 'from django.db import models\n'), (29, 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(4)', 'verbose_name': '"""Número"""'}), False, 'from django.db import models\n'), (30, 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(50)', 'null': '(True)', 'blank': '(True)', 'verbose_name': '"""Complemento"""'}), False, 'from django.db import models\n'), (31, 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(150)', 'verbose_name': '"""Bairro"""'}), False, 'from django.db import models\n'), (32, 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(8)', 'verbose_name': '"""CEP"""'}), False, 'from django.db import models\n'), (35, 'django.db.models.ForeignKey', 'models.ForeignKey', (['City'], {'on_delete': 'models.CASCADE', 'verbose_name': 'City._meta.verbose_name'}), False, 'from django.db import models\n'), (42, 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(7)', 'verbose_name': '"""CNES"""'}), False, 'from django.db import models\n'), (43, 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(14)', 'verbose_name': '"""CNPJ"""'}), False, 'from django.db import models\n'), (44, 'django.db.models.CharField', 'models.CharField', ([], {'max_length': '(255)', 'verbose_name': '"""Razão Social"""'}), False, 'from django.db import models\n'), (46, 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now_add': '(True)', 'auto_now': '(False)', 'verbose_name': '"""Criado em:"""'}), False, 'from django.db import models\n'), (47, 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now_add': '(False)', 'auto_now': '(True)', 'verbose_name': '"""Atualizado em:"""'}), False, 'from django.db import models\n'), (49, 'django.db.models.ManyToManyField', 'models.ManyToManyField', (['Address'], {'verbose_name': 'Address._meta.verbose_name'}), False, 'from django.db import models\n'), (60, 'django.db.models.PositiveSmallIntegerField', 'models.PositiveSmallIntegerField', ([], {'verbose_name': '"""Lote"""'}), False, 'from django.db import models\n'), (62, 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now_add': '(True)', 'auto_now': '(False)', 'verbose_name': '"""Criado em:"""'}), False, 'from django.db import models\n'), (63, 'django.db.models.DateTimeField', 'models.DateTimeField', ([], {'auto_now_add': '(False)', 'auto_now': '(True)', 'verbose_name': '"""Atualizado em:"""'}), False, 'from django.db import models\n'), (65, 'django.db.models.ForeignKey', 'models.ForeignKey', (['HealthCenter'], {'on_delete': 'models.CASCADE', 'verbose_name': 'HealthCenter._meta.verbose_name'}), False, 'from django.db import models\n'), (68, 'django.db.models.ManyToManyField', 'models.ManyToManyField', (['Vaccine'], {'through': '"""VaccineStock"""', 'verbose_name': 'Vaccine._meta.verbose_name'}), False, 'from django.db import models\n'), (75, 'django.db.models.PositiveSmallIntegerField', 'models.PositiveSmallIntegerField', ([], {'verbose_name': '"""Quantidade recebida"""'}), False, 'from django.db import models\n'), (76, 'django.db.models.PositiveSmallIntegerField', 'models.PositiveSmallIntegerField', ([], {'verbose_name': '"""Quantidade restante"""'}), False, 'from django.db import models\n'), (78, 'django.db.models.ForeignKey', 'models.ForeignKey', (['Vaccine'], {'on_delete': 'models.DO_NOTHING', 'verbose_name': 'Vaccine._meta.verbose_name'}), False, 'from django.db import models\n'), (79, 'django.db.models.ForeignKey', 'models.ForeignKey', (['Stock'], {'on_delete': 'models.DO_NOTHING', 'verbose_name': 'Stock._meta.verbose_name'}), False, 'from django.db import models\n')]