"""
Island component schemas.

Mirrors lightwave-ui/src/islands/.

Islands are React components that are hydrated on the client side
within Django templates.

Usage:
    from lightwave.schema.pydantic.ui.islands import (
        BaseHeroProps,
        BaseFooterProps,
        IslandConfig,
        get_island_schema,
    )
"""

from typing import Any

from pydantic import BaseModel, ConfigDict, Field

# Import all island base schemas - only what exists in each module
from lightwave.schema.pydantic.ui.islands.auth import BaseAuthProps
from lightwave.schema.pydantic.ui.islands.banners import BaseBannersProps
from lightwave.schema.pydantic.ui.islands.blog import BaseBlogProps
from lightwave.schema.pydantic.ui.islands.careers import BaseCareersProps
from lightwave.schema.pydantic.ui.islands.contact import BaseContactProps
from lightwave.schema.pydantic.ui.islands.cta import BaseCtaProps
from lightwave.schema.pydantic.ui.islands.error import BaseErrorProps
from lightwave.schema.pydantic.ui.islands.faq import BaseFaqProps
from lightwave.schema.pydantic.ui.islands.features import BaseFeaturesProps
from lightwave.schema.pydantic.ui.islands.footer import BaseFooterProps
from lightwave.schema.pydantic.ui.islands.gallery import BaseGalleryProps
from lightwave.schema.pydantic.ui.islands.header import BaseHeaderProps
from lightwave.schema.pydantic.ui.islands.hero import BaseHeroProps
from lightwave.schema.pydantic.ui.islands.inbox import BaseInboxProps
from lightwave.schema.pydantic.ui.islands.metrics import BaseMetricsProps
from lightwave.schema.pydantic.ui.islands.newsletter import BaseNewsletterProps
from lightwave.schema.pydantic.ui.islands.pricing import BasePricingProps
from lightwave.schema.pydantic.ui.islands.sidebar import BaseSidebarProps
from lightwave.schema.pydantic.ui.islands.social_proof import BaseSocialProofProps
from lightwave.schema.pydantic.ui.islands.team import BaseTeamProps
from lightwave.schema.pydantic.ui.islands.testimonials import BaseTestimonialsProps

# Aliases for backward compatibility and consistent naming
BaseBannerProps = BaseBannersProps
BaseCTAProps = BaseCtaProps
BaseFAQProps = BaseFaqProps


class IslandProps(BaseModel):
    """
    Base island component props.

    Islands receive their props from Django via JSON serialization.
    """

    # Component identification
    component_name: str = Field(
        ...,
        serialization_alias="componentName",
        description="Island component name from registry",
    )

    # Component props (passed through to React)
    props: dict[str, Any] = Field(default_factory=dict, description="Component-specific props")

    # Hydration settings
    hydration: str = Field("idle", description="Hydration strategy (idle, visible, load)")

    model_config = ConfigDict(
        populate_by_name=True,
        extra="forbid",
    )


class IslandConfig(BaseModel):
    """
    Configuration for a single island in Page.islands JSONField.

    This is the schema for how islands are stored in the database.
    Each island has a type (category), variant (specific component), and props.

    Example:
        {
            "type": "hero",
            "variant": "HeroScreenMockup05",
            "order": 0,
            "props": {
                "headline": "Beautiful analytics to grow smarter",
                "subheadline": "Powerful analytics...",
            }
        }
    """

    type: str = Field(..., description="Island category (hero, features, faq, etc.)")
    variant: str = Field(..., description="Specific component variant (HeroScreenMockup05, etc.)")
    order: int = Field(0, description="Rendering order (0 = first)")
    props: dict[str, Any] = Field(default_factory=dict, description="Component-specific props")

    model_config = ConfigDict(
        extra="allow",  # Allow extra fields for flexibility
        populate_by_name=True,
    )


# =============================================================================
# Island Schema Registry
# =============================================================================

# Map of island types to their base schema classes
ISLAND_TYPE_SCHEMAS: dict[str, type[BaseModel]] = {
    "hero": BaseHeroProps,
    "features": BaseFeaturesProps,
    "faq": BaseFAQProps,
    "testimonials": BaseTestimonialsProps,
    "pricing": BasePricingProps,
    "footer": BaseFooterProps,
    "header": BaseHeaderProps,
    "social-proof": BaseSocialProofProps,
    "blog": BaseBlogProps,
    "contact": BaseContactProps,
    "cta": BaseCTAProps,
    "team": BaseTeamProps,
    "newsletter": BaseNewsletterProps,
    "auth": BaseAuthProps,
    "error": BaseErrorProps,
    "metrics": BaseMetricsProps,
    "gallery": BaseGalleryProps,
    "banner": BaseBannerProps,
    "careers": BaseCareersProps,
    "sidebar": BaseSidebarProps,
    "inbox": BaseInboxProps,
}


def get_island_schema(island_type: str) -> type[BaseModel] | None:
    """
    Get the Pydantic schema for an island type.

    Args:
        island_type: The island type (hero, features, faq, etc.)

    Returns:
        The schema class for the island type, or None if not found.

    Example:
        schema = get_island_schema("hero")
        if schema:
            validated_props = schema(**props)
    """
    return ISLAND_TYPE_SCHEMAS.get(island_type)


__all__ = [
    # Base
    "IslandProps",
    "IslandConfig",
    "get_island_schema",
    "ISLAND_TYPE_SCHEMAS",
    # Auth
    "BaseAuthProps",
    # Banners
    "BaseBannerProps",
    "BaseBannersProps",
    # Blog
    "BaseBlogProps",
    # Careers
    "BaseCareersProps",
    # Contact
    "BaseContactProps",
    # CTA
    "BaseCTAProps",
    "BaseCtaProps",
    # Error
    "BaseErrorProps",
    # FAQ
    "BaseFAQProps",
    "BaseFaqProps",
    # Features
    "BaseFeaturesProps",
    # Footer
    "BaseFooterProps",
    # Gallery
    "BaseGalleryProps",
    # Header
    "BaseHeaderProps",
    # Hero
    "BaseHeroProps",
    # Inbox
    "BaseInboxProps",
    # Metrics
    "BaseMetricsProps",
    # Newsletter
    "BaseNewsletterProps",
    # Pricing
    "BasePricingProps",
    # Sidebar
    "BaseSidebarProps",
    # Social Proof
    "BaseSocialProofProps",
    # Team
    "BaseTeamProps",
    # Testimonials
    "BaseTestimonialsProps",
]
