from django.db import models
from hr_department.models import Team, Vendors


class Asset(models.Model):
    CATEGORY_CHOICES = (
        ('Electronics', 'Electronics'),
        ('Furniture', 'Furniture'),
        ('Vehicle', 'Vehicle'),
        ('Other', 'Other'),
    )
    CONDITION_CHOICES = (
        ('New', 'New'),
        ('Good', 'Good'),
        ('Fair', 'Fair'),
        ('Damaged', 'Damaged'),
        ('Written-off', 'Written-off'),
    )
    STATUS_CHOICES = (
        ('In Store', 'In Store'),
        ('Issued', 'Issued'),
        ('Under Repair', 'Under Repair'),
        ('Disposed', 'Disposed'),
    )
    asset_name = models.CharField(max_length=255, blank=False, null=False)
    category = models.CharField(max_length=20, choices=CATEGORY_CHOICES, default='Other')
    asset_tag = models.CharField(max_length=100, unique=True, blank=False, null=False)
    purchase_date = models.DateField(blank=True, null=True)
    purchase_cost = models.CharField(max_length=255, blank=True, null=True, default="")
    vendor = models.ForeignKey(Vendors, on_delete=models.SET_NULL, blank=True, null=True)
    condition = models.CharField(max_length=20, choices=CONDITION_CHOICES, default='New')
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='In Store')
    assigned_to = models.ForeignKey(Team, on_delete=models.SET_NULL, blank=True, null=True, related_name='assigned_assets')
    issue_date = models.DateField(blank=True, null=True)
    return_date = models.DateField(blank=True, null=True)
    warranty_expiry = models.DateField(blank=True, null=True)
    photo = models.ImageField(upload_to="inventory/assets", blank=True, null=True)
    invoice = models.FileField(upload_to="inventory/assets", blank=True, null=True)
    notes = models.TextField(blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)


class Stock_Item(models.Model):
    CATEGORY_CHOICES = (
        ('Stationery', 'Stationery'),
        ('Medical', 'Medical'),
        ('Training Material', 'Training Material'),
        ('Other', 'Other'),
    )
    UNIT_CHOICES = (
        ('Pcs', 'Pcs'),
        ('Box', 'Box'),
        ('Kg', 'Kg'),
        ('Ltr', 'Ltr'),
    )
    item_name = models.CharField(max_length=255, blank=False, null=False)
    category = models.CharField(max_length=20, choices=CATEGORY_CHOICES, default='Other')
    unit = models.CharField(max_length=10, choices=UNIT_CHOICES, default='Pcs')
    reorder_level = models.IntegerField(default=0)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    @property
    def current_quantity(self):
        totals = self.transactions.aggregate(
            total_in=models.Sum('quantity', filter=models.Q(transaction_type='In')),
            total_out=models.Sum('quantity', filter=models.Q(transaction_type='Out')),
        )
        return (totals['total_in'] or 0) - (totals['total_out'] or 0)

    @property
    def is_low_stock(self):
        return self.current_quantity <= self.reorder_level


class Stock_Transaction(models.Model):
    TRANSACTION_TYPE_CHOICES = (
        ('In', 'In'),
        ('Out', 'Out'),
    )
    item = models.ForeignKey(Stock_Item, on_delete=models.CASCADE, related_name='transactions')
    transaction_type = models.CharField(max_length=10, choices=TRANSACTION_TYPE_CHOICES)
    quantity = models.IntegerField(blank=False, null=False)
    date = models.DateField(blank=False, null=False)
    vendor = models.ForeignKey(Vendors, on_delete=models.SET_NULL, blank=True, null=True)
    issued_to = models.ForeignKey(Team, on_delete=models.SET_NULL, blank=True, null=True)
    project = models.ForeignKey('program_department.Projects', on_delete=models.SET_NULL, blank=True, null=True)
    notes = models.CharField(max_length=255, blank=True, null=True, default="")
    created_at = models.DateTimeField(auto_now_add=True)
