from django.db import models
from common.models.base import TimeStampedModel
# from package_category.models.package_category import PackageCategory
from package_details.models.package_details import PackageDetails

class PackageDiscount(TimeStampedModel):
    package = models.OneToOneField(PackageDetails,on_delete=models.CASCADE,related_name='discount')
    discount_amount = models.DecimalField(max_digits=10,decimal_places=2,null=True,blank=True)
    discount_percent = models.DecimalField(max_digits=5,decimal_places=2,null=True,blank=True)

    class Meta:
        verbose_name_plural = 'package_discounts'

    def get_discounted_price(self):
        from decimal import Decimal, InvalidOperation
        
        try:
            # Convert string price to Decimal for calculations
            price = Decimal(str(self.package.price))
            
            if self.discount_amount:
                return max(price - self.discount_amount, 0)
            elif self.discount_percent:
                return max(price - (price * self.discount_percent / 100), 0)
            return price
        except (InvalidOperation, ValueError, TypeError):
            # If price conversion fails, return 0 or the original string
            return 0