from django.db import models

from account.models import SiteUser


class City(models.Model):
    code = models.CharField(primary_key=True, max_length=20, verbose_name="Код города")
    name = models.CharField(unique=True, max_length=50, verbose_name="Название города")

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

    @staticmethod
    def to_choices():
        return City.objects.order_by('name').values_list('code', 'name')


class Order(models.Model):
    # основные поля: название и описание
    name = models.CharField(max_length=200)
    description = models.TextField(blank=True)

    # площадь в квадратных метрах
    square = models.DecimalField(max_digits=5, decimal_places=2, blank=False)

    work_time = models.CharField(max_length=100, blank=True)

    # дальше вид дома, тип ремонта, тип квартиры, требуется дизайн проект, закуп материала, тип исполнителя

    CHOICE_UNDEFINED = ''

    # тип ремонта
    TYPE_OF_RENOVATION_OVERHAUL = 'overhaul'
    TYPE_OF_RENOVATION_PARTIAL = 'partial'
    TYPE_OF_RENOVATION_REDECOR = 'redecor'
    TYPE_OF_RENOVATION_PREMIUM = 'premium'
    TYPE_OF_RENOVATION_DESIGN = 'design'

    TYPE_OF_RENOVATION_CHOICES = [
        (CHOICE_UNDEFINED, 'Не указан'),
        (TYPE_OF_RENOVATION_OVERHAUL, 'Капитальный'),
        (TYPE_OF_RENOVATION_PARTIAL, 'Частичный'),
        (TYPE_OF_RENOVATION_REDECOR, 'Косметический'),
        (TYPE_OF_RENOVATION_PREMIUM, 'Премиальный'),
        (TYPE_OF_RENOVATION_DESIGN, 'Дизайнерский'),
    ]
    type_of_renovation = models.CharField(max_length=10, choices=TYPE_OF_RENOVATION_CHOICES, default=CHOICE_UNDEFINED)

    # тип дома
    TYPE_OF_HOUSE_BLOCK = 'block'
    TYPE_OF_HOUSE_BRICK = 'brick'
    TYPE_OF_HOUSE_MONOLITH = 'monolith'
    TYPE_OF_HOUSE_PANEL = 'panel'

    TYPE_OF_HOUSE_CHOICES = [
        (CHOICE_UNDEFINED, 'Не указан'),
        (TYPE_OF_HOUSE_BLOCK, 'Блочный'),
        (TYPE_OF_HOUSE_BRICK, 'Кирпичный'),
        (TYPE_OF_HOUSE_MONOLITH, 'Монолит'),
        (TYPE_OF_HOUSE_PANEL, 'Панельный'),
    ]
    type_of_house = models.CharField(max_length=10, choices=TYPE_OF_HOUSE_CHOICES)

    # тип квартиры
    TYPE_OF_ROOM_PRIMARY = 'primary'
    TYPE_OF_ROOM_SECONDARY = 'secondary'

    TYPE_OF_ROOM_CHOICES = [
        (CHOICE_UNDEFINED, 'Не указан'),
        (TYPE_OF_ROOM_PRIMARY, 'Первичка'),
        (TYPE_OF_ROOM_SECONDARY, 'Вторичка')
    ]
    type_of_room = models.CharField(max_length=10, choices=TYPE_OF_ROOM_CHOICES)

    # требуется дизайн проект
    REQUIRED_DESIGN_CHOICES = ((None, 'Не указано'), (True, 'Да'), (False, 'Нет'))
    is_require_design = models.BooleanField(default=None, blank=True, null=True, choices=REQUIRED_DESIGN_CHOICES)

    # закуп материала
    PURCHASE_OF_MATERIAL_EXECUTOR = 'executor'
    PURCHASE_OF_MATERIAL_CUSTOMER = 'customer'

    PURCHASE_OF_MATERIAL_CHOICES = [
        (CHOICE_UNDEFINED, 'Не указано'),
        (PURCHASE_OF_MATERIAL_EXECUTOR, 'Исполнитель'),
        (PURCHASE_OF_MATERIAL_CUSTOMER, 'Заказчик')
    ]
    purchase_of_material = models.CharField(max_length=10, choices=PURCHASE_OF_MATERIAL_CHOICES)

    # тип исполнителя
    TYPE_OF_EXECUTOR_INDIVIDUAL = 'individual'
    TYPE_OF_EXECUTOR_COMPANY = 'company'

    TYPE_OF_EXECUTOR_CHOICES = [
        (CHOICE_UNDEFINED, 'Не указан'),
        (TYPE_OF_EXECUTOR_INDIVIDUAL, 'Самозанятый/бригада'),
        (TYPE_OF_EXECUTOR_COMPANY, 'Компания')
    ]
    type_of_executor = models.CharField(max_length=10, choices=TYPE_OF_EXECUTOR_CHOICES)

    # дальше отдельные параметры
    is_with_warranty = models.BooleanField(default=True, verbose_name="С гарантией")
    is_with_contract = models.BooleanField(default=False, verbose_name="Работа по договору")
    is_with_trade = models.BooleanField(default=False, verbose_name="Возможен торг")
    is_with_cleaning = models.BooleanField(default=False, verbose_name="С уборкой")
    is_with_garbage_removal = models.BooleanField(default=False, verbose_name="С вывозом мусора")

    # примерная цена
    approximate_price = models.DecimalField(max_digits=9, decimal_places=2, blank=False, verbose_name="Цена")

    # address_city = models.ForeignKey(City, on_delete=models.CASCADE, blank=False, related_name="address_city")
    address_text = models.CharField(max_length=70, blank=True, verbose_name="Улица, дом")

    owner = models.ForeignKey(SiteUser, on_delete=models.CASCADE, null=True, related_name="owner",
                              verbose_name="Владелец")

