from shop_management.models.base import DataRoot, models
from shop_management.models.data.shop_account import Account
from shop_management.models.data.utils import Currency
from shop_management.models.data.employee import Employee
from shop_management.models.data.shop import Shop
from django.utils import timezone


class Advance(DataRoot):
    shop = models.ForeignKey(
        Shop, on_delete=models.CASCADE, related_name="advances", null=True, blank=True
    )
    employee = models.ForeignKey(
        Employee,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="advance",
    )
    amount = models.DecimalField(max_digits=12, decimal_places=2)
    account = models.ForeignKey(
        Account,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="advances",
    )
    currency = models.ForeignKey(
        Currency, on_delete=models.SET_NULL, null=True, blank=True
    )
    exchange_rate = models.DecimalField(max_digits=20, decimal_places=6, default=1)
    converted_amount = models.DecimalField(max_digits=18, decimal_places=2, default=0)
    year = models.CharField(max_length=10, blank=True, null=True)
    month = models.CharField(max_length=10, blank=True, null=True)
    payment_date = models.DateTimeField(default=timezone.now, null=True, blank=True)
    notes = models.TextField(blank=True, null=True)

    def __str__(self):
        employee_name = self.employee.name if self.employee else "Unknown Employee"
        return f"{employee_name} - {self.amount}"
