from django.db import models
import uuid

class BalanceDetail(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    user = models.ForeignKey('user.User', on_delete=models.CASCADE)
    work_balance = models.ForeignKey('worktimeservice.WorkBalance', on_delete=models.CASCADE, related_name='balance_details')
    paycode = models.ForeignKey('paycode.Paycode', on_delete=models.CASCADE)
    
    # Balance fields (same structure as WorkBalance) - stored in seconds for precision
    regular_work_seconds = models.IntegerField(default=0)
    total_work_seconds = models.IntegerField(default=0)
    daily_break_seconds = models.IntegerField(default=0)
    net_work_seconds = models.IntegerField(default=0)
    overtime_seconds = models.IntegerField(default=0)
    flex_seconds = models.IntegerField(default=0)
    bank_credited_seconds = models.IntegerField(default=0)
    bank_debited_seconds = models.IntegerField(default=0)
    overtime_balance_seconds = models.IntegerField(default=0)
    toil_seconds = models.IntegerField(default=0)
    vacation_days_accrued = models.DecimalField(max_digits=8, decimal_places=4, default=0)
    
    # Metadata
    date = models.DateField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    
    class Meta:
        unique_together = ['user', 'work_balance', 'paycode']
        ordering = ['paycode__name']
    
    def __str__(self):
        return f"{self.user.firstname} {self.user.lastname} - {self.date} - {self.paycode.name}"