from django.utils import timezone

from shop_management.models.base import DataRoot, models
from shop_management.models.data.shop import Shop
from shop_management.models.data.shop_account import Account
from shop_management.models.data.utils import Currency


class PassiveIncome(DataRoot):
    shop = models.ForeignKey(
        Shop, on_delete=models.CASCADE, related_name="passive_incomes", null=True, blank=True
    )
    name = models.CharField(max_length=255)
    account = models.ForeignKey(
        Account,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="passive_incomes",
    )
    currency = models.ForeignKey(
        Currency,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="passive_incomes",
    )
    amount = models.DecimalField(max_digits=12, decimal_places=2)
    exchange_rate = models.DecimalField(max_digits=20, decimal_places=6, default=1)
    converted_amount = models.DecimalField(max_digits=18, decimal_places=2, default=0)
    income_date = models.DateTimeField(default=timezone.now)
    notes = models.TextField(blank=True, null=True)

    def __str__(self):
        return f"{self.name} - {self.amount}"
