"""
Market model
"""

from django.core.validators import MinValueValidator
from django.db import models

from core.apps.eggs.models import location
from core.http.models import base
from core.http.models.user import User


class Market(base.AbstractBaseModel):
    name = models.CharField(max_length=255)
    company_name = models.CharField(max_length=255)
    user_id = models.ForeignKey(to=User, on_delete=models.CASCADE)
    avatar = models.ImageField(
        upload_to="market_avatar/",
        blank=True,
        null=True,
        default="market_avatar/default.png",
    )
    phone = models.CharField(max_length=20)
    location_id = models.ForeignKey(
        to=location.Location,
        on_delete=models.CASCADE,
        related_name="market_location",
    )
    debt_paid = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        default=0,
        validators=[MinValueValidator(0)],
    )
    debt_unpaid = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        default=0,
        validators=[MinValueValidator(0)],
    )

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

    class Meta:
        verbose_name = "Market"
        verbose_name_plural = "Markets"
        db_table = "market"
