from django.db import models
from django.contrib.auth.models import User
import uuid
# Create your models here.
# User.add_to_class('profiles',  models.FileField(upload_to='uploads/', null=True, blank=True, default="../static/dummy.png"))


from decimal import Decimal


class Profiles(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True, related_name='user_profiles')

    name = models.CharField(max_length=255, null=True)
    phone = models.CharField(max_length=255, null=True)
    email = models.CharField(max_length=255, null=True)
    address = models.CharField(max_length=255, null=True)
    servicename = models.CharField(max_length=255, null=True)
    
    gender = models.CharField(max_length=255, null=True)
    profiles = models.FileField(upload_to='uploads/', null=True, blank=True, default="../static/dummy.png")

    password = models.CharField(max_length=255, null=True)
    gstnumber = models.CharField(max_length=255, null=True)
    pincode = models.CharField(max_length=255, null=True)


    doj = models.DateField(null=True)
    
    gst = models.FileField(upload_to='uploads/', null=True, blank=True)
    companylogo = models.FileField(upload_to='uploads/', null=True, blank=True)
    alteremail = models.CharField(max_length=255, null=True)
    alteremail1 = models.CharField(max_length=255, null=True)
    alteremail2 = models.CharField(max_length=255, null=True)


    pan = models.FileField(upload_to='uploads/', null=True, blank=True)
    role = models.SmallIntegerField(default=0)



    status = models.SmallIntegerField(default=1)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'profile'







class Company(models.Model):
    company_name = models.CharField(max_length=200)
    address = models.TextField(blank=True, null=True)
    mobile = models.CharField(max_length=20, blank=True, null=True)
    email = models.EmailField(blank=True, null=True)
    gst_number = models.CharField(max_length=50, blank=True, null=True)
    bank_details = models.TextField(blank=True, null=True)
    logo = models.ImageField(upload_to="company/", blank=True, null=True)

    # --- added to match the client's Excel invoice header ---
    dealers_in = models.CharField(
        max_length=255, blank=True, null=True,
        help_text="Shown on the invoice header, e.g. 'Iron & Steel'"
    )
    invoice_terms = models.TextField(
        blank=True, null=True,
        default=(
            "Interest @ 30% pa will be charged if payment not made within 20 days.\n"
            "Goods once sold cannot be taken back or exchanged.\n"
            "Our responsibility ceases once goods leave our godown.\n"
            "All disputes are subject to Chennai jurisdiction."
        ),
        help_text="Printed as footer notes/terms on every invoice PDF.",
    )

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.company_name


class Customer(models.Model):
    name = models.CharField(max_length=200)
    address = models.TextField(blank=True, null=True)
    gst_number = models.CharField(max_length=50, blank=True, null=True)
    mobile = models.CharField(max_length=20, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name


class Product(models.Model):
    name = models.CharField(max_length=200)
    hsn_code = models.CharField(max_length=50, blank=True, null=True)
    default_rate = models.DecimalField(max_digits=12, decimal_places=2, default=Decimal("0.00"))
    unit = models.CharField(max_length=50, default="Kg")
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name


class Invoice(models.Model):
    invoice_number = models.CharField(max_length=50, unique=True)
    invoice_date = models.DateField()
    customer = models.ForeignKey(Customer, on_delete=models.PROTECT, related_name="invoices")

    # --- added to match the client's Excel invoice header ---
    eway_bill_no = models.CharField(max_length=50, blank=True, null=True)
    eway_bill_date = models.DateField(blank=True, null=True)
    vehicle_no = models.CharField(max_length=50, blank=True, null=True)

    subtotal = models.DecimalField(max_digits=12, decimal_places=2, default=Decimal("0.00"))  # "Gross Total"
    cgst = models.DecimalField(max_digits=12, decimal_places=2, default=Decimal("0.00"))
    sgst = models.DecimalField(max_digits=12, decimal_places=2, default=Decimal("0.00"))
    igst = models.DecimalField(max_digits=12, decimal_places=2, default=Decimal("0.00"))  # inter-state tax
    round_off = models.DecimalField(max_digits=12, decimal_places=2, default=Decimal("0.00"))
    grand_total = models.DecimalField(max_digits=12, decimal_places=2, default=Decimal("0.00"))  # "Net Total"

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.invoice_number


class InvoiceItem(models.Model):
    invoice = models.ForeignKey(Invoice, on_delete=models.CASCADE, related_name="items")
    product = models.ForeignKey(Product, on_delete=models.PROTECT, related_name="invoice_items")
    quantity = models.DecimalField(max_digits=12, decimal_places=2)
    rate = models.DecimalField(max_digits=12, decimal_places=2)
    amount = models.DecimalField(max_digits=12, decimal_places=2, default=Decimal("0.00"))

    def __str__(self):
        return f"{self.invoice.invoice_number} - {self.product.name}"