"""
ThrillWiki API v1 serializers module.

This module provides a unified interface to all serializers across different domains
while maintaining the modular structure for better organization and maintainability.
"""

import importlib
from typing import Any

# --- Companies and ride models domain ---
from .companies import (
    CompanyCreateInputSerializer,
    CompanyDetailOutputSerializer,
    CompanyUpdateInputSerializer,
    RideModelCreateInputSerializer,
    RideModelDetailOutputSerializer,
    RideModelUpdateInputSerializer,
)  # noqa: F401

# --- Parks domain ---
from .parks import (
    ParkAreaCreateInputSerializer,
    ParkAreaDetailOutputSerializer,
    ParkAreaUpdateInputSerializer,
    ParkCreateInputSerializer,
    ParkDetailOutputSerializer,
    ParkFilterInputSerializer,
    ParkListOutputSerializer,
    ParkLocationCreateInputSerializer,
    ParkLocationOutputSerializer,
    ParkLocationUpdateInputSerializer,
    ParkSuggestionOutputSerializer,
    ParkSuggestionSerializer,
    ParkUpdateInputSerializer,
)  # noqa: F401

# --- Rides domain ---
from .rides import (
    RideCreateInputSerializer,
    RideDetailOutputSerializer,
    RideFilterInputSerializer,
    RideListOutputSerializer,
    RideLocationCreateInputSerializer,
    RideLocationOutputSerializer,
    RideLocationUpdateInputSerializer,
    RideModelOutputSerializer,
    RideParkOutputSerializer,
    RideReviewCreateInputSerializer,
    RideReviewOutputSerializer,
    RideReviewUpdateInputSerializer,
    RideUpdateInputSerializer,
    RollerCoasterStatsCreateInputSerializer,
    RollerCoasterStatsOutputSerializer,
    RollerCoasterStatsUpdateInputSerializer,
)  # noqa: F401
from .services import (
    CoordinateInputSerializer,
    DistanceCalculationInputSerializer,
    DistanceCalculationOutputSerializer,
    EmailSendInputSerializer,
    EmailTemplateOutputSerializer,
    GeocodeInputSerializer,
    GeocodeOutputSerializer,
    HealthCheckOutputSerializer,
    HistoryCreateInputSerializer,
    HistoryEntryOutputSerializer,
    HistoryEventSerializer,
    MapDataOutputSerializer,
    ModerationSubmissionOutputSerializer,
    ModerationSubmissionSerializer,
    PerformanceMetricsOutputSerializer,
    RoadtripCreateInputSerializer,
    RoadtripOutputSerializer,
    RoadtripParkSerializer,
    SimpleHealthOutputSerializer,
)  # noqa: F401

# --- Shared utilities and base classes ---
from .shared import (
    FilterOptionSerializer,
    FilterRangeSerializer,
    StandardizedFilterMetadataSerializer,
    ensure_filter_option_format,
    validate_filter_metadata_contract,
)  # noqa: F401

# --- Accounts domain: try multiple likely locations, fall back to placeholders ---
_ACCOUNTS_SYMBOLS: list[str] = [
    "UserProfileOutputSerializer",
    "UserProfileCreateInputSerializer",
    "UserProfileUpdateInputSerializer",
    "UserOutputSerializer",
    "LoginInputSerializer",
    "LoginOutputSerializer",
    "SignupInputSerializer",
    "SignupOutputSerializer",
    "PasswordResetInputSerializer",
    "PasswordResetOutputSerializer",
    "PasswordChangeInputSerializer",
    "PasswordChangeOutputSerializer",
    "LogoutOutputSerializer",
    "SocialProviderOutputSerializer",
    "AuthStatusOutputSerializer",
]


def _import_accounts_symbols() -> dict[str, Any]:
    """
    Try a list of candidate module paths and return a dict mapping expected symbol
    names to the objects found. If no candidate provides a symbol, the symbol maps to None.
    """
    candidates = [
        f"{__package__}.accounts",
        f"{__package__}.auth",
        "apps.accounts.serializers",
        "apps.api.v1.auth.serializers",
    ]

    # Prepare default placeholders
    result: dict[str, Any] = dict.fromkeys(_ACCOUNTS_SYMBOLS)

    for modname in candidates:
        try:
            module = importlib.import_module(modname)
        except Exception:
            continue

        # Fill in any symbols that exist on this module (don't require all)
        for name in _ACCOUNTS_SYMBOLS:
            if hasattr(module, name):
                result[name] = getattr(module, name)

        # If we've found at least one real object (not all None), stop trying further candidates.
        if any(result[name] is not None for name in _ACCOUNTS_SYMBOLS):
            break

    return result


_accounts = _import_accounts_symbols()

# Bind account symbols into the module namespace (only if they exist)
for _name in _ACCOUNTS_SYMBOLS:
    if _accounts.get(_name) is not None:
        globals()[_name] = _accounts[_name]

# --- Services domain ---

# --- Optionally try importing other domain modules and inject serializer-like names ---
_optional_domains = [
    "other",
    "media",
    "parks_media",
    "rides_media",
    "search",
    "history",
]
for domain in _optional_domains:
    modname = f"{__package__}.{domain}"
    try:
        module = importlib.import_module(modname)
    except Exception:
        continue
    # Inject any attribute that looks like a serializer or matches uppercase naming used by exported symbols
    for attr in dir(module):
        if attr.startswith("_"):
            continue
        # Heuristic: export classes/constants that end with 'Serializer' or are uppercase constants
        if (
            attr.endswith("Serializer")
            or attr.isupper()
            or attr.endswith("OutputSerializer")
            or attr.endswith("InputSerializer")
        ):
            globals()[attr] = getattr(module, attr)

# --- Construct a conservative __all__ based on explicit lists and discovered serializer names ---
_SHARED_EXPORTS = [
    "FilterOptionSerializer",
    "FilterRangeSerializer",
    "StandardizedFilterMetadataSerializer",
    "validate_filter_metadata_contract",
    "ensure_filter_option_format",
]

_PARKS_EXPORTS = [
    "ParkListOutputSerializer",
    "ParkDetailOutputSerializer",
    "ParkCreateInputSerializer",
    "ParkUpdateInputSerializer",
    "ParkFilterInputSerializer",
    "ParkAreaDetailOutputSerializer",
    "ParkAreaCreateInputSerializer",
    "ParkAreaUpdateInputSerializer",
    "ParkLocationOutputSerializer",
    "ParkLocationCreateInputSerializer",
    "ParkLocationUpdateInputSerializer",
    "ParkSuggestionSerializer",
    "ParkSuggestionOutputSerializer",
]

_COMPANIES_EXPORTS = [
    "CompanyDetailOutputSerializer",
    "CompanyCreateInputSerializer",
    "CompanyUpdateInputSerializer",
    "RideModelDetailOutputSerializer",
    "RideModelCreateInputSerializer",
    "RideModelUpdateInputSerializer",
]

_RIDES_EXPORTS = [
    "RideParkOutputSerializer",
    "RideModelOutputSerializer",
    "RideListOutputSerializer",
    "RideDetailOutputSerializer",
    "RideCreateInputSerializer",
    "RideUpdateInputSerializer",
    "RideFilterInputSerializer",
    "RollerCoasterStatsOutputSerializer",
    "RollerCoasterStatsCreateInputSerializer",
    "RollerCoasterStatsUpdateInputSerializer",
    "RideLocationOutputSerializer",
    "RideLocationCreateInputSerializer",
    "RideLocationUpdateInputSerializer",
    "RideReviewOutputSerializer",
    "RideReviewCreateInputSerializer",
    "RideReviewUpdateInputSerializer",
]

_SERVICES_EXPORTS = [
    "HealthCheckOutputSerializer",
    "PerformanceMetricsOutputSerializer",
    "SimpleHealthOutputSerializer",
    "EmailSendInputSerializer",
    "EmailTemplateOutputSerializer",
    "MapDataOutputSerializer",
    "CoordinateInputSerializer",
    "HistoryEventSerializer",
    "HistoryEntryOutputSerializer",
    "HistoryCreateInputSerializer",
    "ModerationSubmissionSerializer",
    "ModerationSubmissionOutputSerializer",
    "RoadtripParkSerializer",
    "RoadtripCreateInputSerializer",
    "RoadtripOutputSerializer",
    "GeocodeInputSerializer",
    "GeocodeOutputSerializer",
    "DistanceCalculationInputSerializer",
    "DistanceCalculationOutputSerializer",
]

# Build a static __all__ list with only the serializers we know exist
__all__ = [
    # Shared exports
    "FilterOptionSerializer",
    "FilterRangeSerializer",
    "StandardizedFilterMetadataSerializer",
    "validate_filter_metadata_contract",
    "ensure_filter_option_format",
    # Parks exports
    "ParkListOutputSerializer",
    "ParkDetailOutputSerializer",
    "ParkCreateInputSerializer",
    "ParkUpdateInputSerializer",
    "ParkFilterInputSerializer",
    "ParkAreaDetailOutputSerializer",
    "ParkAreaCreateInputSerializer",
    "ParkAreaUpdateInputSerializer",
    "ParkLocationOutputSerializer",
    "ParkLocationCreateInputSerializer",
    "ParkLocationUpdateInputSerializer",
    "ParkSuggestionSerializer",
    "ParkSuggestionOutputSerializer",
    # Companies exports
    "CompanyDetailOutputSerializer",
    "CompanyCreateInputSerializer",
    "CompanyUpdateInputSerializer",
    "RideModelDetailOutputSerializer",
    "RideModelCreateInputSerializer",
    "RideModelUpdateInputSerializer",
    # Rides exports
    "RideParkOutputSerializer",
    "RideModelOutputSerializer",
    "RideListOutputSerializer",
    "RideDetailOutputSerializer",
    "RideCreateInputSerializer",
    "RideUpdateInputSerializer",
    "RideFilterInputSerializer",
    "RollerCoasterStatsOutputSerializer",
    "RollerCoasterStatsCreateInputSerializer",
    "RollerCoasterStatsUpdateInputSerializer",
    "RideLocationOutputSerializer",
    "RideLocationCreateInputSerializer",
    "RideLocationUpdateInputSerializer",
    "RideReviewOutputSerializer",
    "RideReviewCreateInputSerializer",
    "RideReviewUpdateInputSerializer",
    # Services exports
    "HealthCheckOutputSerializer",
    "PerformanceMetricsOutputSerializer",
    "SimpleHealthOutputSerializer",
    "EmailSendInputSerializer",
    "EmailTemplateOutputSerializer",
    "MapDataOutputSerializer",
    "CoordinateInputSerializer",
    "HistoryEventSerializer",
    "HistoryEntryOutputSerializer",
    "HistoryCreateInputSerializer",
    "ModerationSubmissionSerializer",
    "ModerationSubmissionOutputSerializer",
    "RoadtripParkSerializer",
    "RoadtripCreateInputSerializer",
    "RoadtripOutputSerializer",
    "GeocodeInputSerializer",
    "GeocodeOutputSerializer",
    "DistanceCalculationInputSerializer",
    "DistanceCalculationOutputSerializer",
]

# Add any accounts serializers that actually exist
for name in _ACCOUNTS_SYMBOLS:
    if name in globals():
        __all__.append(name)
