from django.db import models
from apps.core.models import BaseModel


class CompanyProfile(BaseModel):
    """Singleton-ish company profile used in invoice headers."""
    name = models.CharField(max_length=255)
    gstin = models.CharField(max_length=15, blank=True)
    pan = models.CharField(max_length=10, blank=True)
    address_line1 = models.CharField(max_length=255, blank=True)
    address_line2 = models.CharField(max_length=255, blank=True)
    city = models.CharField(max_length=120, blank=True)
    state = models.CharField(max_length=120, blank=True)
    country = models.CharField(max_length=120, default='India')
    postcode = models.CharField(max_length=10, blank=True)
    phone1 = models.CharField(max_length=20, blank=True)
    phone2 = models.CharField(max_length=20, blank=True)
    email = models.EmailField(blank=True)
    website = models.URLField(blank=True)
    logo = models.ImageField(upload_to='invoice/company/', null=True, blank=True)
    declaration_text = models.TextField(
        blank=True,
        default=(
            "We declare that this invoice shows the actual price of the goods described and that all particulars are true and correct."
        ),
    )

    class Meta:
        verbose_name = 'Company Profile'
        verbose_name_plural = 'Company Profile'

    def __str__(self):
        return self.name


class DispatchAddress(BaseModel):
    """Admin-managed dispatch/warehouse addresses selectable per product."""
    label = models.CharField(max_length=100, help_text="e.g. Hyderabad Warehouse")
    contact_name = models.CharField(max_length=255, blank=True)
    address_line1 = models.CharField(max_length=255)
    address_line2 = models.CharField(max_length=255, blank=True)
    city = models.CharField(max_length=120)
    state = models.CharField(max_length=120)
    country = models.CharField(max_length=120, default='India')
    postcode = models.CharField(max_length=10, blank=True)
    phone = models.CharField(max_length=20, blank=True)
    is_default = models.BooleanField(default=False)

    class Meta:
        ordering = ['-is_default', 'label']
        verbose_name = 'Dispatch Address'
        verbose_name_plural = 'Dispatch Addresses'

    def __str__(self):
        return self.label


class PlaceOfSupply(BaseModel):
    """Admin-managed GST places of supply."""
    code = models.CharField(max_length=10, help_text="GST state code, e.g. 36")
    name = models.CharField(max_length=120, help_text="State name, e.g. Telangana")
    is_default = models.BooleanField(default=False)

    class Meta:
        ordering = ['-is_default', 'name']
        verbose_name = 'Place of Supply'
        verbose_name_plural = 'Places of Supply'

    def __str__(self):
        return f"{self.code}-{self.name}"


class InvoiceTerm(BaseModel):
    """Individual Terms & Conditions line for invoice footer."""
    line = models.TextField()
    sort_order = models.PositiveIntegerField(default=0)

    class Meta:
        ordering = ['sort_order', 'created_at']
        verbose_name = 'Invoice Term'
        verbose_name_plural = 'Invoice Terms'

    def __str__(self):
        return self.line[:80]
