from django.db import models
from apps.core.models import BaseModel

class OpportunityBundle(BaseModel):
    name = models.CharField(max_length=255, unique=True)
    opportunity_bundle_amount = models.DecimalField(max_digits=12, decimal_places=2, default=0)

    total_levels = models.PositiveIntegerField(default=18, null=True, blank=True)
    is_active = models.BooleanField(default=True)

    def __str__(self):
        return self.name

class OpportunityBundleDistribution(BaseModel):
    class ComponentType(models.TextChoices):
        DIRECT_INCENTIVE = 'DIRECT_INCENTIVE', 'Direct Incentive'
        ENGAGEMENT_POOL = 'ENGAGEMENT_POOL', 'Engagement Pool'
        POWER_STREAM = 'POWER_STREAM', 'Power Stream'
        PLATFORM_CHARGES = 'PLATFORM_CHARGES', 'Platform Charges'
        TAX = 'TAX', 'Tax'

    class ValueType(models.TextChoices):
        FIXED = 'FIXED', 'Fixed'
        PERCENT = 'PERCENT', 'Percent'

    opportunity_bundle = models.ForeignKey(OpportunityBundle, related_name='distributions', on_delete=models.CASCADE, db_index=True)
    component_type = models.CharField(max_length=32, choices=ComponentType.choices)
    value_type = models.CharField(max_length=16, choices=ValueType.choices)
    value = models.DecimalField(max_digits=12, decimal_places=2)

    def __str__(self):
        return f"{self.opportunity_bundle.name} - {self.component_type}"

class OpportunityBundleLevel(BaseModel):
    opportunity_bundle = models.ForeignKey(OpportunityBundle, related_name='levels', on_delete=models.CASCADE, db_index=True)
    level_number = models.PositiveIntegerField(db_index=True)
    display_order = models.PositiveIntegerField(default=1)
    required_positions = models.PositiveIntegerField()
    reward_amount = models.DecimalField(max_digits=12, decimal_places=2)
    has_reward = models.BooleanField(default=False)
    has_service = models.BooleanField(default=False)
    has_gift = models.BooleanField(default=False)
    auto_id_enabled = models.BooleanField(default=False)
    auto_id_count = models.PositiveIntegerField(default=0)
    auto_id_product = models.ForeignKey("products.Product", null=True, blank=True, on_delete=models.SET_NULL)
    requires_user_choice = models.BooleanField(default=False)

    # New fields for service/gift selection
    service_product = models.ForeignKey("products.Product", null=True, blank=True, on_delete=models.SET_NULL, related_name='service_levels')
    gift_type = models.CharField(max_length=16, choices=(('inhouse', 'In-house'), ('external', 'External')), null=True, blank=True)
    gift_product = models.ForeignKey("products.Product", null=True, blank=True, on_delete=models.SET_NULL, related_name='gift_levels')
    gift_external = models.CharField(max_length=255, null=True, blank=True)

    class Meta:
        ordering = ["display_order"]
        unique_together = ("opportunity_bundle", "level_number")
        indexes = [
            models.Index(fields=["opportunity_bundle"]),
            models.Index(fields=["level_number"]),
            models.Index(fields=["opportunity_bundle", "level_number"]),
            models.Index(fields=["display_order"]),
        ]

    def __str__(self):
        return f"{self.opportunity_bundle.name} - Level {self.level_number}"

    # Expose Rank model for import from models
from .rank import Rank
