from django.db import models
from django.utils import timezone
from decimal import Decimal


# ---- WorkBalance: aggregated computed balances for a user and date ----
class WorkBalance(models.Model):
    # we key by user_id (string) to match stamps.user_id format
    user_id = models.CharField(max_length=32)
    date = models.DateField()

    # computed fields (seconds where applicable)
    regular_work_seconds = models.PositiveIntegerField(default=0)    # up to scheduled day seconds
    total_work_seconds = models.PositiveIntegerField(default=0)      # total raw worked seconds (sum of in/out)
    daily_break_seconds = models.PositiveIntegerField(default=0)
    net_work_seconds = models.PositiveIntegerField(default=0)        # total_work - unpaid breaks (or includes paid breaks depending on policy)
    overtime_seconds = models.IntegerField(default=0)                # extra (positive) or negative (deficit)
    flex_seconds = models.IntegerField(default=0)                    # flex balance change for that day
    bank_credited_seconds = models.IntegerField(default=0)          # seconds added to time bank that day
    bank_debited_seconds = models.IntegerField(default=0)           # seconds taken from bank that day
    overtime_balance_seconds = models.IntegerField(default=0)       # cumulative overtime balance (weekly/monthly calculation handled outside or with scheduled job)
    toil_seconds = models.IntegerField(default=0)                   # time-off-in-lieu seconds earned that day
    vacation_days_accrued = models.DecimalField(max_digits=5, decimal_places=2, default=0.0)
    # flags / violations
    lunch_break_missing_flag = models.BooleanField(default=False)
    daily_rest_violation_flag = models.BooleanField(default=False)
    weekly_rest_violation_flag = models.BooleanField(default=False)

    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'workbalances'
        unique_together = ('user_id', 'date')
        indexes = [
            models.Index(fields=['user_id','date']),
        ]

    def __str__(self):
        return f"{self.user_id} @ {self.date} - net {self.net_work_seconds/3600:.2f}h"
from django.db import models

# Create your models here.
