from django.db import models
from django.db.models import Q

from .base import Currency, Term, Unit
from .providers import CloudProvider
from .services import Service


class ComputePlanGroup(models.Model):
    name = models.CharField(max_length=200)
    description = models.TextField(blank=True)
    node_label = models.CharField(
        max_length=100, help_text="Kubernetes node label for this group"
    )
    order = models.IntegerField(default=0)

    class Meta:
        ordering = ["order", "name"]

    def __str__(self):
        return self.name


class ComputePlanPrice(models.Model):
    compute_plan = models.ForeignKey(
        "ComputePlan", on_delete=models.CASCADE, related_name="prices"
    )
    currency = models.CharField(
        max_length=3,
        choices=Currency.choices,
    )
    amount = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        help_text="Price in the specified currency, excl. VAT",
    )

    class Meta:
        unique_together = ("compute_plan", "currency")
        ordering = ["currency"]

    def __str__(self):
        return f"{self.compute_plan.name} - {self.amount} {self.currency}"


class ComputePlan(models.Model):
    name = models.CharField(max_length=200)
    vcpus = models.FloatField(help_text="Number of available vCPUs")
    ram = models.FloatField(help_text="Amount of RAM available")
    cpu_mem_ratio = models.FloatField(
        help_text="vCPU to Memory ratio. How much vCPU per GiB RAM is available?"
    )
    active = models.BooleanField(default=True, help_text="Is the plan active?")
    term = models.CharField(
        max_length=3,
        default=Term.MTH,
        choices=Term.choices,
    )

    cloud_provider = models.ForeignKey(
        CloudProvider, on_delete=models.CASCADE, related_name="compute_plans"
    )

    group = models.ForeignKey(
        ComputePlanGroup,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="compute_plans",
        help_text="Group this compute plan belongs to",
    )

    valid_from = models.DateTimeField(blank=True, null=True)
    valid_to = models.DateTimeField(blank=True, null=True)

    class Meta:
        ordering = ["name"]

    def __str__(self):
        return self.name

    def get_price(self, currency_code: str):
        try:
            return self.prices.get(currency=currency_code).amount
        except ComputePlanPrice.DoesNotExist:
            return None


class StoragePlanPrice(models.Model):
    storage_plan = models.ForeignKey(
        "StoragePlan", on_delete=models.CASCADE, related_name="prices"
    )
    currency = models.CharField(
        max_length=3,
        choices=Currency.choices,
    )
    amount = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        help_text="Price in the specified currency, excl. VAT",
    )

    class Meta:
        unique_together = ("storage_plan", "currency")
        ordering = ["currency"]

    def __str__(self):
        return f"{self.storage_plan.name} - {self.amount} {self.currency}"


class StoragePlan(models.Model):
    name = models.CharField(max_length=200)
    cloud_provider = models.ForeignKey(
        CloudProvider, on_delete=models.CASCADE, related_name="storage_plans"
    )
    term = models.CharField(
        max_length=3,
        default=Term.MTH,
        choices=Term.choices,
    )
    unit = models.CharField(
        max_length=3,
        default=Unit.GIB,
        choices=Unit.choices,
    )

    valid_from = models.DateTimeField(blank=True, null=True)
    valid_to = models.DateTimeField(blank=True, null=True)

    class Meta:
        unique_together = ("cloud_provider", "term", "unit", "valid_from", "valid_to")
        ordering = ["name"]

    def __str__(self):
        return self.name

    def get_price(self, currency_code: str):
        try:
            return self.prices.get(currency=currency_code).amount
        except StoragePlanPrice.DoesNotExist:
            return None


class ProgressiveDiscountModel(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField(blank=True)
    active = models.BooleanField(default=True)

    class Meta:
        verbose_name = "Discount Model"

    def __str__(self):
        return self.name

    def calculate_discount(self, base_rate, units):
        """Calculate price using progressive percentage discounts."""
        final_price = 0
        remaining_units = units

        discount_tiers = self.tiers.all().order_by("min_units")

        for tier in discount_tiers:
            if remaining_units <= 0:
                break

            # Determine how many units fall into this tier
            tier_min = tier.min_units
            tier_max = tier.max_units if tier.max_units else float("inf")

            # Skip if we haven't reached this tier yet
            if units < tier_min:
                continue

            # Calculate units in this tier
            units_start = max(0, units - remaining_units)
            units_end = min(units, tier_max if tier.max_units else units)
            tier_units = max(0, units_end - max(units_start, tier_min - 1))

            if tier_units > 0:
                discounted_rate = base_rate * (1 - tier.discount_percent / 100)
                final_price += discounted_rate * tier_units
                remaining_units -= tier_units

        return final_price

    def get_discount_breakdown(self, base_rate, units):
        """Get detailed breakdown of discount calculation."""
        breakdown = []
        remaining_units = units

        discount_tiers = self.tiers.all().order_by("min_units")

        for tier in discount_tiers:
            if remaining_units <= 0:
                break

            tier_min = tier.min_units
            tier_max = tier.max_units if tier.max_units else float("inf")

            if units < tier_min:
                continue

            units_start = max(0, units - remaining_units)
            units_end = min(units, tier_max if tier.max_units else units)
            tier_units = max(0, units_end - max(units_start, tier_min - 1))

            if tier_units > 0:
                discounted_rate = base_rate * (1 - tier.discount_percent / 100)
                tier_total = discounted_rate * tier_units

                breakdown.append(
                    {
                        "tier_range": f"{tier_min}-{tier_max-1 if tier.max_units else '∞'}",
                        "units": tier_units,
                        "discount_percent": tier.discount_percent,
                        "rate": discounted_rate,
                        "subtotal": tier_total,
                    }
                )

                remaining_units -= tier_units

        return breakdown


class DiscountTier(models.Model):
    discount_model = models.ForeignKey(
        ProgressiveDiscountModel, on_delete=models.CASCADE, related_name="tiers"
    )
    min_units = models.PositiveIntegerField(
        help_text="Minimum unit count for this tier (inclusive)", default=0
    )
    max_units = models.PositiveIntegerField(
        null=True,
        blank=True,
        help_text="Maximum unit count for this tier (exclusive). Leave blank for unlimited.",
    )
    discount_percent = models.DecimalField(
        max_digits=5, decimal_places=2, help_text="Percentage discount applied (0-100)"
    )

    class Meta:
        ordering = ["min_units"]
        unique_together = ["discount_model", "min_units"]

    def __str__(self):
        if self.max_units:
            return f"{self.discount_model.name}: {self.min_units}-{self.max_units-1} units → {self.discount_percent}% discount"
        else:
            return f"{self.discount_model.name}: {self.min_units}+ units → {self.discount_percent}% discount"


class VSHNAppCatPrice(models.Model):
    class VariableUnit(models.TextChoices):
        RAM = "RAM", "Memory (RAM)"
        CPU = "CPU", "CPU (vCPU)"
        USER = "USR", "Users"

    class ServiceLevel(models.TextChoices):
        BEST_EFFORT = "BE", "Best Effort"
        GUARANTEED = "GA", "Guaranteed Availability"

    service = models.ForeignKey(
        Service, on_delete=models.CASCADE, related_name="vshn_appcat_price"
    )
    variable_unit = models.CharField(
        max_length=3,
        choices=VariableUnit.choices,
        default=VariableUnit.RAM,
    )
    term = models.CharField(
        max_length=3,
        default=Term.MTH,
        choices=Term.choices,
    )
    discount_model = models.ForeignKey(
        ProgressiveDiscountModel,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="price_configs",
    )

    ha_replica_min = models.IntegerField(
        default=1, help_text="Minimum of replicas for HA"
    )
    ha_replica_max = models.IntegerField(
        default=1, help_text="Maximum supported replicas"
    )

    public_display_enabled = models.BooleanField(
        default=True,
        help_text="Enable public display of price calculator on offering detail page",
    )

    valid_from = models.DateTimeField(blank=True, null=True)
    valid_to = models.DateTimeField(blank=True, null=True)

    class Meta:
        verbose_name = "AppCat Price"

    def __str__(self):
        return f"{self.service.name} - {self.get_variable_unit_display()} based pricing"

    def get_unit_rate(self, currency_code: str, service_level: str):
        try:
            return self.unit_rates.get(
                currency=currency_code, service_level=service_level
            ).amount
        except VSHNAppCatUnitRate.DoesNotExist:
            return None

    def calculate_final_price(
        self,
        currency_code: str,
        service_level: str,
        number_of_units: int,
        addon_ids=None,
    ):
        base_fee = self.get_base_fee(currency_code, service_level)
        unit_rate = self.get_unit_rate(currency_code, service_level)

        if base_fee is None or unit_rate is None:
            return None

        if number_of_units < 0:
            raise ValueError("Number of units cannot be negative")

        # Apply discount model if available
        if self.discount_model and self.discount_model.active:
            discounted_price = self.discount_model.calculate_discount(
                unit_rate, number_of_units
            )
            total_price = base_fee + discounted_price
        else:
            total_price = base_fee + (unit_rate * number_of_units)

        # Add prices for mandatory addons and selected addons
        addon_total = 0
        addon_breakdown = []

        # Query all active addons related to this price config
        addons_query = self.addons.filter(active=True)

        # Include mandatory addons and explicitly selected addons
        if addon_ids:
            addons = addons_query.filter(Q(mandatory=True) | Q(id__in=addon_ids))
        else:
            addons = addons_query.filter(mandatory=True)

        for addon in addons:
            addon_price = 0
            if addon.addon_type == VSHNAppCatAddon.AddonType.BASE_FEE:
                addon_price_value = addon.get_price(currency_code, service_level)
                if addon_price_value:
                    addon_price = addon_price_value
            elif addon.addon_type == VSHNAppCatAddon.AddonType.UNIT_RATE:
                addon_price_value = addon.get_price(currency_code, service_level)
                if addon_price_value:
                    addon_price = addon_price_value * number_of_units

            addon_total += addon_price
            addon_breakdown.append(
                {
                    "id": addon.id,
                    "name": addon.name,
                    "description": addon.description,
                    "commercial_description": addon.commercial_description,
                    "mandatory": addon.mandatory,
                    "price": addon_price,
                }
            )

        total_price += addon_total

        return {
            "total_price": total_price,
            "addon_total": addon_total,
            "addon_breakdown": addon_breakdown,
        }

    def get_base_fee(self, currency_code: str, service_level: str):
        """
        Get the base fee for the given currency and service level.
        """
        try:
            return self.base_fees.get(currency=currency_code, service_level=service_level).amount
        except VSHNAppCatBaseFee.DoesNotExist:
            return None


class VSHNAppCatUnitRate(models.Model):
    vshn_appcat_price_config = models.ForeignKey(
        VSHNAppCatPrice, on_delete=models.CASCADE, related_name="unit_rates"
    )
    currency = models.CharField(
        max_length=3,
        choices=Currency.choices,
    )
    service_level = models.CharField(
        max_length=2,
        choices=VSHNAppCatPrice.ServiceLevel.choices,
    )
    amount = models.DecimalField(
        max_digits=10,
        decimal_places=4,
        help_text="Price per unit in the specified currency and service level, excl. VAT",
    )

    class Meta:
        verbose_name = "Service Unit Rate"
        unique_together = ("vshn_appcat_price_config", "currency", "service_level")
        ordering = ["currency", "service_level"]

    def __str__(self):
        return f"{self.vshn_appcat_price_config.service.name} - {self.get_service_level_display()} Unit Rate - {self.amount} {self.currency}"


class VSHNAppCatAddon(models.Model):
    """
    Addon pricing model for VSHNAppCatPrice. Can be added to a service price configuration
    to provide additional features or resources with their own pricing.
    """

    class AddonType(models.TextChoices):
        BASE_FEE = "BF", "Base Fee"  # Fixed amount regardless of units
        UNIT_RATE = "UR", "Unit Rate"  # Price per unit

    vshn_appcat_price_config = models.ForeignKey(
        VSHNAppCatPrice, on_delete=models.CASCADE, related_name="addons"
    )
    name = models.CharField(max_length=100, help_text="Name of the addon")
    description = models.TextField(
        blank=True, help_text="Technical description of the addon"
    )
    commercial_description = models.TextField(
        blank=True, help_text="Commercial description displayed in the frontend"
    )
    addon_type = models.CharField(
        max_length=2,
        choices=AddonType.choices,
        help_text="Type of addon pricing (fixed fee or per-unit)",
    )
    mandatory = models.BooleanField(default=False, help_text="Is this addon mandatory?")
    active = models.BooleanField(
        default=True, help_text="Is this addon active and available for selection?"
    )
    order = models.IntegerField(default=0, help_text="Display order in the frontend")
    valid_from = models.DateTimeField(blank=True, null=True)
    valid_to = models.DateTimeField(blank=True, null=True)

    class Meta:
        verbose_name = "Service Addon"
        ordering = ["order", "name"]

    def __str__(self):
        return f"{self.vshn_appcat_price_config.service.name} - {self.name}"

    def get_price(self, currency_code: str, service_level: str = None):
        """
        Get the price for this addon in the specified currency and service level.
        For base fee addons, service_level is required and used.
        For unit rate addons, service_level is required and used.
        """
        try:
            if self.addon_type == self.AddonType.BASE_FEE:
                if not service_level:
                    raise ValueError("Service level is required for base fee addons")
                return self.base_fees.get(currency=currency_code, service_level=service_level).amount
            elif self.addon_type == self.AddonType.UNIT_RATE:
                if not service_level:
                    raise ValueError("Service level is required for unit rate addons")
                return self.unit_rates.get(
                    currency=currency_code, service_level=service_level
                ).amount
        except (
            VSHNAppCatAddonBaseFee.DoesNotExist,
            VSHNAppCatAddonUnitRate.DoesNotExist,
        ):
            return None


class VSHNAppCatAddonBaseFee(models.Model):
    """
    Base fee for an addon (fixed amount regardless of units), specified per currency and service level.
    """
    addon = models.ForeignKey(
        VSHNAppCatAddon, on_delete=models.CASCADE, related_name="base_fees"
    )
    currency = models.CharField(
        max_length=3,
        choices=Currency.choices,
    )
    service_level = models.CharField(
        max_length=2,
        choices=VSHNAppCatPrice.ServiceLevel.choices,
        default=VSHNAppCatPrice.ServiceLevel.BEST_EFFORT,
    )
    amount = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        help_text="Base fee in the specified currency and service level, excl. VAT",
    )

    class Meta:
        verbose_name = "Addon Base Fee"
        unique_together = ("addon", "currency", "service_level")
        ordering = ["currency", "service_level"]

    def __str__(self):
        return f"{self.addon.name} Base Fee - {self.amount} {self.currency} ({self.get_service_level_display()})"

    def get_service_level_display(self):
        return dict(VSHNAppCatPrice.ServiceLevel.choices).get(self.service_level, self.service_level)


class VSHNAppCatAddonUnitRate(models.Model):
    """Unit rate for an addon (price per unit)"""

    addon = models.ForeignKey(
        VSHNAppCatAddon, on_delete=models.CASCADE, related_name="unit_rates"
    )
    currency = models.CharField(
        max_length=3,
        choices=Currency.choices,
    )
    service_level = models.CharField(
        max_length=2,
        choices=VSHNAppCatPrice.ServiceLevel.choices,
    )
    amount = models.DecimalField(
        max_digits=10,
        decimal_places=4,
        help_text="Price per unit in the specified currency and service level, excl. VAT",
    )

    class Meta:
        verbose_name = "Addon Unit Rate"
        unique_together = ("addon", "currency", "service_level")
        ordering = ["currency", "service_level"]

    def __str__(self):
        return f"{self.addon.name} - {self.get_service_level_display()} Unit Rate - {self.amount} {self.currency}"


class VSHNAppCatBaseFee(models.Model):
    """
    Base fee for a service, specified per currency and service level.
    """
    vshn_appcat_price_config = models.ForeignKey(
        "VSHNAppCatPrice", on_delete=models.CASCADE, related_name="base_fees"
    )
    currency = models.CharField(
        max_length=3,
        choices=Currency.choices,
    )
    service_level = models.CharField(
        max_length=2,
        choices=VSHNAppCatPrice.ServiceLevel.choices,
        default=VSHNAppCatPrice.ServiceLevel.BEST_EFFORT,
    )
    amount = models.DecimalField(
        max_digits=10,
        decimal_places=2,
        help_text="Base fee in the specified currency and service level, excl. VAT",
    )

    class Meta:
        verbose_name = "Service Base Fee"
        unique_together = ("vshn_appcat_price_config", "currency", "service_level")
        ordering = ["currency", "service_level"]

    def __str__(self):
        return f"{self.vshn_appcat_price_config.service.name} Base Fee - {self.amount} {self.currency} ({self.get_service_level_display()})"

    def get_service_level_display(self):
        return dict(VSHNAppCatPrice.ServiceLevel.choices).get(self.service_level, self.service_level)


class ExternalPricePlans(models.Model):
    plan_name = models.CharField()
    description = models.CharField(max_length=200, blank=True, null=True)
    source = models.URLField(blank=True, null=True)
    date_retrieved = models.DateField(blank=True, null=True)

    ## Relations
    cloud_provider = models.ForeignKey(
        CloudProvider, on_delete=models.CASCADE, related_name="external_price"
    )
    service = models.ForeignKey(
        Service, on_delete=models.CASCADE, related_name="external_price"
    )
    compare_to = models.ManyToManyField(
        ComputePlan, related_name="external_prices", blank=True
    )
    vshn_appcat_price = models.ForeignKey(
        VSHNAppCatPrice,
        on_delete=models.CASCADE,
        related_name="external_comparisons",
        blank=True,
        null=True,
        help_text="Specific VSHN AppCat price configuration to compare against",
    )
    service_level = models.CharField(
        max_length=2,
        choices=VSHNAppCatPrice.ServiceLevel.choices,
        blank=True,
        null=True,
        help_text="Service level equivalent for comparison",
    )

    ## Money
    currency = models.CharField(
        max_length=3,
        default=Currency.CHF,
        choices=Currency.choices,
    )
    term = models.CharField(
        max_length=3,
        default=Term.MTH,
        choices=Term.choices,
    )
    amount = models.DecimalField(
        max_digits=10,
        decimal_places=4,
        help_text="Price per unit in the specified currency, excl. VAT",
    )

    ## Offering
    vcpus = models.FloatField(
        help_text="Number of included vCPUs", blank=True, null=True
    )
    ram = models.FloatField(
        help_text="Amount of GiB RAM included", blank=True, null=True
    )
    storage = models.FloatField(
        help_text="Amount of GiB included", blank=True, null=True
    )
    competitor_sla = models.CharField(blank=True, null=True)
    replicas = models.IntegerField(blank=True, null=True)

    class Meta:
        verbose_name = "External Price"

    def __str__(self):
        return f"{self.cloud_provider.name} - {self.service.name} - {self.plan_name}"
