"""``mgf-fed new-project-prompt`` / ``adopt-prompt`` — the two federation
entry-point prompts that flank :mod:`mgf.fed.cli._sync_prompt`.

Three onboarding cases, one shipped prompt each (all self-updating — resolved
from the pinned ``mgf-standard`` via :func:`mgf.standard.read_recipe`, never a
hand-copied body that drifts):

* **existing member** → ``sync-prompt`` (``federation-sync-prompt``)
* **new/empty repo → member** → ``new-project-prompt`` (``federation-new-project-prompt``)
* **non-member adopting** → ``adopt-prompt`` (``federation-adopt-prompt``)

Like ``sync-prompt`` these are flag-less: resolve → print. They reach only the
documented :func:`mgf.standard.read_recipe` seam (never the private ``_recipes``
data dir), keeping the closed-box rule intact, and degrade with an actionable
error if the ``mgf-standard`` extra is missing.
"""

from __future__ import annotations

import sys
from typing import TYPE_CHECKING

from mgf.common.cli import EXIT_SUCCESS, EXIT_UNKNOWN

if TYPE_CHECKING:
    import argparse


def _resolve_recipe(recipe: str) -> str | None:
    """Return a recipe's text via the public ``mgf.standard.read_recipe`` seam, or ``None``.

    ``None`` when ``mgf-standard`` is missing, too old to carry the recipes seam
    (no ``read_recipe`` — pre-0.1.3), or the data cannot be located, so the
    caller can print an actionable error.
    """
    try:
        import mgf.standard
    except (ModuleNotFoundError, ImportError):
        return None
    read_recipe = getattr(mgf.standard, "read_recipe", None)
    if read_recipe is None:
        return None
    try:
        result = read_recipe(recipe)
    except (FileNotFoundError, ValueError, OSError):
        return None
    return result if isinstance(result, str) else None


def _print_recipe(recipe: str) -> int:
    """Resolve + print ``recipe`` from ``mgf-standard``; actionable error if unresolvable."""
    text = _resolve_recipe(recipe)
    if text is None:
        print(
            f"error: the `{recipe}` prompt is not installed. It ships in the "
            "`mgf-standard` extra — install it:\n"
            "    pip install -U 'mgf-common[standards]'   "
            "(or: uv add 'mgf-common[standards]')\n"
            "The corpus is an optional, Codeberg-only extra; a plain mgf-common "
            "install does not carry it. Editable-sibling alternative: read "
            f"../mgf-standard/docs/recipes/{recipe}.md.",
            file=sys.stderr,
        )
        return EXIT_UNKNOWN
    print(text, end="" if text.endswith("\n") else "\n")
    return EXIT_SUCCESS


def add_new_project_prompt_args(_subparser: argparse.ArgumentParser) -> None:
    """Register flags for ``mgf-fed new-project-prompt`` — none today (pure print)."""


def run_new_project_prompt(_args: argparse.Namespace) -> int:
    """Print the new-project bootstrap prompt (Case B: empty repo → federation member)."""
    return _print_recipe("federation-new-project-prompt")


def add_adopt_prompt_args(_subparser: argparse.ArgumentParser) -> None:
    """Register flags for ``mgf-fed adopt-prompt`` — none today (pure print)."""


def run_adopt_prompt(_args: argparse.Namespace) -> int:
    """Print the adopt prompt (Case C: a non-member understands/adopts the standards)."""
    return _print_recipe("federation-adopt-prompt")


def add_onboard_prompt_args(_subparser: argparse.ArgumentParser) -> None:
    """Register flags for ``mgf-fed onboard-prompt`` — none today (pure print)."""


def run_onboard_prompt(_args: argparse.Namespace) -> int:
    """Print the onboard prompt (an EXISTING external project becomes a federation member)."""
    return _print_recipe("federation-onboard-prompt")


__all__ = [
    "add_adopt_prompt_args",
    "add_new_project_prompt_args",
    "add_onboard_prompt_args",
    "run_adopt_prompt",
    "run_new_project_prompt",
    "run_onboard_prompt",
]
