from __future__ import annotations

from dataclasses import dataclass
from typing import TYPE_CHECKING

from django.utils.translation import gettext_lazy as _

from .constants import CAMERA_CAPACITOR_PACKAGE, FILE_PICKER_CAPACITOR_PACKAGE

if TYPE_CHECKING:
    from .models import MobileApp

CAPABILITY_STATUS_IMPLEMENTED = "implemented"
CAPABILITY_STATUS_PLANNED = "planned"
CAPABILITY_STATUS_EXPERIMENTAL = "experimental"


@dataclass(frozen=True)
class CapabilityDefinition:
    key: str
    display_name: object
    description: object
    required_capacitor_packages: tuple[str, ...]
    supported_platforms: tuple[str, ...]
    implementation_status: str
    runtime_verification_required: bool
    settings_field: str | None = None
    always_enabled: bool = False

    def is_enabled(self, mobile_app: MobileApp) -> bool:
        if self.settings_field:
            return bool(getattr(mobile_app, self.settings_field, False))
        return self.always_enabled

    def serialize(self, mobile_app: MobileApp) -> dict:
        return {
            "key": self.key,
            "display_name": str(self.display_name),
            "description": str(self.description),
            "required_capacitor_packages": list(self.required_capacitor_packages),
            "supported_platforms": list(self.supported_platforms),
            "implementation_status": self.implementation_status,
            "runtime_verification_required": self.runtime_verification_required,
            "settings_field": self.settings_field or "",
            "enabled": self.is_enabled(mobile_app),
        }


CAPABILITY_REGISTRY: tuple[CapabilityDefinition, ...] = (
    CapabilityDefinition(
        key="camera",
        display_name=_("Camera"),
        description=_(
            "Native image capture is generated through the mobile bridge with a browser file-input fallback."
        ),
        required_capacitor_packages=(CAMERA_CAPACITOR_PACKAGE,),
        supported_platforms=("web", "android", "ios"),
        implementation_status=CAPABILITY_STATUS_IMPLEMENTED,
        runtime_verification_required=True,
        settings_field="enable_camera_uploads",
    ),
    CapabilityDefinition(
        key="file_picker",
        display_name=_("File picker"),
        description=_(
            "Native file selection is generated through the mobile bridge with a browser file-input fallback."
        ),
        required_capacitor_packages=(FILE_PICKER_CAPACITOR_PACKAGE,),
        supported_platforms=("web", "android", "ios"),
        implementation_status=CAPABILITY_STATUS_IMPLEMENTED,
        runtime_verification_required=True,
        settings_field="enable_file_picker",
    ),
    CapabilityDefinition(
        key="downloads",
        display_name=_("Downloads"),
        description=_(
            "Generated metadata defines download handling expectations, but native download-manager integration still needs device verification."
        ),
        required_capacitor_packages=(),
        supported_platforms=("web", "android", "ios"),
        implementation_status=CAPABILITY_STATUS_EXPERIMENTAL,
        runtime_verification_required=True,
        always_enabled=True,
    ),
    CapabilityDefinition(
        key="external_links",
        display_name=_("External links"),
        description=_(
            "The generated shell includes deterministic external-link policy metadata and diagnostics for system-browser handoff."
        ),
        required_capacitor_packages=(),
        supported_platforms=("web", "android", "ios"),
        implementation_status=CAPABILITY_STATUS_IMPLEMENTED,
        runtime_verification_required=True,
        always_enabled=True,
    ),
    CapabilityDefinition(
        key="offline",
        display_name=_("Offline shell"),
        description=_(
            "The generated app shell includes offline state handling and a defensive offline fallback page."
        ),
        required_capacitor_packages=(),
        supported_platforms=("web", "android", "ios"),
        implementation_status=CAPABILITY_STATUS_IMPLEMENTED,
        runtime_verification_required=False,
        always_enabled=True,
    ),
    CapabilityDefinition(
        key="pwa",
        display_name=_("PWA"),
        description=_(
            "The generated package includes a manifest and service worker for installable web-app behavior."
        ),
        required_capacitor_packages=(),
        supported_platforms=("web", "android", "ios"),
        implementation_status=CAPABILITY_STATUS_IMPLEMENTED,
        runtime_verification_required=False,
        settings_field="enable_pwa",
    ),
    CapabilityDefinition(
        key="deep_links",
        display_name=_("Deep links"),
        description=_(
            "Deep-link metadata and rollout notes are generated, but Android App Links and iOS Universal Links still need platform association files."
        ),
        required_capacitor_packages=(),
        supported_platforms=("android", "ios"),
        implementation_status=CAPABILITY_STATUS_PLANNED,
        runtime_verification_required=True,
        settings_field="enable_deep_links",
    ),
    CapabilityDefinition(
        key="push_notifications",
        display_name=_("Push notifications"),
        description=_(
            "Push notifications remain a planning-only capability until native delivery setup and permission copy are implemented."
        ),
        required_capacitor_packages=("@capacitor/push-notifications",),
        supported_platforms=("android", "ios"),
        implementation_status=CAPABILITY_STATUS_PLANNED,
        runtime_verification_required=True,
        settings_field="enable_push_notifications",
    ),
    CapabilityDefinition(
        key="secure_storage",
        display_name=_("Secure storage"),
        description=_(
            "The generated bridge exposes a secure-storage API that prefers native secure storage and falls back only to documented in-memory storage in browsers."
        ),
        required_capacitor_packages=("capacitor-secure-storage-plugin",),
        supported_platforms=("android", "ios"),
        implementation_status=CAPABILITY_STATUS_IMPLEMENTED,
        runtime_verification_required=True,
        settings_field="enable_secure_storage",
    ),
    CapabilityDefinition(
        key="biometrics",
        display_name=_("Biometrics"),
        description=_(
            "Biometric authentication still needs a real account workflow, permission copy, and platform verification."
        ),
        required_capacitor_packages=("@aparajita/capacitor-biometric-auth",),
        supported_platforms=("android", "ios"),
        implementation_status=CAPABILITY_STATUS_PLANNED,
        runtime_verification_required=True,
        settings_field="enable_biometrics",
    ),
    CapabilityDefinition(
        key="qr_scanner",
        display_name=_("QR scanner"),
        description=_(
            "The generated bridge can invoke QR scanning through a Capacitor barcode scanner plugin when available and reports a clear unsupported error otherwise."
        ),
        required_capacitor_packages=("@capacitor-mlkit/barcode-scanning",),
        supported_platforms=("android", "ios"),
        implementation_status=CAPABILITY_STATUS_IMPLEMENTED,
        runtime_verification_required=True,
        settings_field="enable_qr_scanner",
    ),
    CapabilityDefinition(
        key="share_sheet",
        display_name=_("Share sheet"),
        description=_(
            "The generated bridge can share page title, text, and URLs through Capacitor Share or browser navigator.share when available."
        ),
        required_capacitor_packages=("@capacitor/share",),
        supported_platforms=("web", "android", "ios"),
        implementation_status=CAPABILITY_STATUS_IMPLEMENTED,
        runtime_verification_required=True,
        settings_field="enable_share_sheet",
    ),
    CapabilityDefinition(
        key="analytics",
        display_name=_("Analytics"),
        description=_(
            "Analytics instrumentation is intentionally deferred until an explicit telemetry strategy is chosen."
        ),
        required_capacitor_packages=(),
        supported_platforms=("web", "android", "ios"),
        implementation_status=CAPABILITY_STATUS_PLANNED,
        runtime_verification_required=True,
    ),
    CapabilityDefinition(
        key="crash_reporting",
        display_name=_("Crash reporting"),
        description=_(
            "Crash reporting is reserved for a later release that adds explicit mobile observability tooling."
        ),
        required_capacitor_packages=(),
        supported_platforms=("android", "ios"),
        implementation_status=CAPABILITY_STATUS_PLANNED,
        runtime_verification_required=True,
    ),
)


def get_capability_registry() -> tuple[CapabilityDefinition, ...]:
    return CAPABILITY_REGISTRY


def serialize_capability_registry(mobile_app: MobileApp) -> list[dict]:
    return [capability.serialize(mobile_app) for capability in CAPABILITY_REGISTRY]
