"""
RideSubType model for categorizing rides by sub-type within each category.

This model replaces the legacy Supabase ride_sub_types table,
providing a lookup table for ride sub-types (e.g., 'Flying Coaster', 
'Inverted Coaster' for roller coasters).
"""

from django.conf import settings
from django.db import models

from apps.core.choices import RichChoiceField
from apps.core.models import TrackedModel


class RideSubType(TrackedModel):
    """
    Lookup table for ride sub-types categorized by ride category.
    
    Examples:
        - Roller Coaster: Flying Coaster, Inverted Coaster, Dive Coaster
        - Water Ride: Log Flume, River Rapids, Splash Battle
        - Dark Ride: Trackless, Omnimover, Boat Ride
    """

    name = models.CharField(
        max_length=100,
        help_text="Name of the ride sub-type (e.g., 'Flying Coaster')",
    )
    category = RichChoiceField(
        choice_group="categories",
        domain="rides",
        max_length=2,
        help_text="Ride category this sub-type belongs to",
    )
    description = models.TextField(
        blank=True,
        help_text="Description of this ride sub-type",
    )
    created_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        related_name="created_ride_sub_types",
        help_text="User who created this sub-type",
    )

    class Meta(TrackedModel.Meta):
        verbose_name = "Ride Sub-Type"
        verbose_name_plural = "Ride Sub-Types"
        ordering = ["category", "name"]
        unique_together = [["category", "name"]]

    def __str__(self) -> str:
        return f"{self.name} ({self.get_category_display()})"

    def get_category_display(self) -> str:
        """Get human-readable category label."""
        from apps.core.choices.registry import get_choice_group
        
        group = get_choice_group("categories", domain="rides")
        if group:
            for choice in group.choices:
                if choice.value == self.category:
                    return choice.label
        return self.category
