# pyright: reportMissingImports=false
"""FastAPI framework plugin for the Passwordless module."""

from __future__ import annotations

from pathlib import Path
from typing import Any, Dict, List, Mapping

from modules.shared.frameworks import FrameworkPlugin
from modules.shared.utils.health import ensure_health_package, ensure_vendor_health_shim
from modules.shared.utils.health_specs import build_standard_health_spec


class FastAPIPlugin(FrameworkPlugin):
    """Plugin for generating FastAPI-oriented passwordless authentication integrations."""

    @property
    def name(self) -> str:
        return "fastapi"

    @property
    def language(self) -> str:
        return "python"

    @property
    def display_name(self) -> str:
        return "FastAPI"

    def get_template_mappings(self) -> Dict[str, str]:
        return {
            "passwordless": "templates/variants/fastapi/passwordless.py.j2",
            "passwordless_types": "templates/base/passwordless_types.py.j2",
            "routes": "templates/variants/fastapi/passwordless_routes.py.j2",
            "config": "templates/variants/fastapi/passwordless_config.yaml.j2",
            "integration_tests": "templates/tests/integration/test_passwordless_integration.j2",
        }

    def get_output_paths(self) -> Dict[str, str]:
        return {
            "passwordless": "src/modules/free/auth/passwordless/passwordless.py",
            "passwordless_types": "src/modules/free/auth/passwordless/passwordless_types.py",
            "routes": "src/modules/free/auth/passwordless/routers/passwordless.py",
            "config": "config/passwordless.yaml",
            "integration_tests": "tests/modules/integration/auth/passwordless/test_passwordless_integration.py",
        }

    def get_context_enrichments(self, base_context: Mapping[str, Any]) -> Dict[str, Any]:
        return {
            **base_context,
            "framework": "fastapi",
            "framework_display_name": "FastAPI",
            "language": "python",
            "module_class_name": "Passwordless",
            "auth_methods": ["email", "sms", "magic_link"],
            "token_expiry": 900,  # 15 minutes
            "rate_limiting": True,
        }

    def validate_requirements(self) -> List[str]:
        return []

    def pre_generation_hook(self, output_dir: Path) -> None:
        module_root = output_dir / "src" / "modules" / "free" / "auth" / "passwordless"
        module_root.mkdir(parents=True, exist_ok=True)
        (module_root / "routers").mkdir(parents=True, exist_ok=True)
        (output_dir / "config").mkdir(parents=True, exist_ok=True)
        (output_dir / "tests" / "modules" / "integration" / "auth" / "passwordless").mkdir(
            parents=True, exist_ok=True
        )
        try:
            spec = build_standard_health_spec(Path(__file__).resolve().parents[1])
            ensure_vendor_health_shim(output_dir, spec=spec)
        except (RuntimeError, OSError):
            pass

        ensure_health_package(
            output_dir,
            extra_imports=[
                ("src.health.passwordless", "register_passwordless_health"),
            ],
        )

    def post_generation_hook(self, output_dir: Path) -> None:  # noqa: ARG002
        del output_dir
