"""``mgf-fed provision-ci-host`` — DEPRECATED shim → ``mgf-cloud-provision ci-host``.

The verb migrated to **mgf-cloud-provision** (Wave-2 P0.4; Court: "migrate, one
provisioner"). The version that lived here rendered the *pre-Postgres* SQLite
compose from ``deploy/woodpecker/`` — a rebuild would have reintroduced the
known-bad single-writer topology. The successor renders the CURRENT production
bundle (Postgres compose + two-vhost Caddyfile), byte-pinned to its
``examples/``, and can converge ``woodpecker-agent2`` in the same run.

This shim keeps the CLI surface alive for one deprecation cycle: it prints the
deprecation notice and **delegates** to
:func:`mgf.cloud_provision.ci_host.provision_ci_host` when mgf-cloud-provision
is installed, else it errors with the exact install/usage instructions. It
deliberately does NOT forward mgf-fed's stale ``deploy/woodpecker/`` bundle —
an explicit ``--bundle-dir`` is forwarded; the default is the successor's
packaged current bundle.
"""

from __future__ import annotations

import importlib
from typing import TYPE_CHECKING

from mgf.fed._logging import log_operation

if TYPE_CHECKING:
    from pathlib import Path

__all__ = ["DEPRECATION_NOTICE", "provision_ci_host"]

# The successor module path. Imported dynamically: mgf-cloud-provision is NOT a
# dependency of mgf-fed (deliberately — the delegation is the deprecation path,
# not a coupling), so a static import would fail mypy --strict in environments
# without it. importlib keeps the checker neutral either way.
_SUCCESSOR_MODULE = "mgf.cloud_provision.ci_host"

DEPRECATION_NOTICE = (
    "mgf-fed provision-ci-host is DEPRECATED — the verb moved to mgf-cloud-provision\n"
    "(Wave-2 P0.4: one provisioner, rendering the CURRENT Postgres topology).\n"
    "Use:  mgf-cloud-provision ci-host [--with-agent] [--dry-run|--yes] …\n"
    "Docs: mgf-cloud-provision docs/woodpecker-ci-runbook.md §10"
)

_MISSING_SUCCESSOR = (
    "mgf-fed: provision-ci-host moved to mgf-cloud-provision, which is not installed.\n"
    "  pip install mgf-cloud-provision   # resolves from the devpi index (MGF_INFRA.md §1)\n"
    "  mgf-cloud-provision ci-host --dry-run\n"
    "The legacy in-repo implementation was removed: it rendered the pre-Postgres\n"
    "SQLite compose (a known-bad rebuild). No cloud action was taken."
)


def provision_ci_host(
    *,
    hostname: str = "woodpecker",
    env: str = "prod",
    flavor: str | None = None,
    image: str | None = None,
    network: str | None = None,
    volume_size_gb: int | None = 20,
    floating_ip: bool = False,
    key_name: str | None = None,
    bundle_dir: Path | None = None,
    provider_name: str = "infomaniak",
    clouds_yaml: str | None = None,
    cloud: str = "openstack",
    dry_run: bool = False,
    yes: bool = False,
    provider: object | None = None,
) -> int:
    """Deprecation shim: delegate to mgf-cloud-provision's ``ci-host`` verb.

    Signature preserved from the pre-migration verb so ``mgf-fed
    provision-ci-host``'s flags keep working for the deprecation cycle.
    Returns the delegate's exit code, or 1 when the successor is missing.
    """
    print(DEPRECATION_NOTICE, flush=True)
    with log_operation("provision.ci_host", hostname=hostname, env=env, dry_run=dry_run):
        try:
            ci_host = importlib.import_module(_SUCCESSOR_MODULE)
        except ImportError:
            print(_MISSING_SUCCESSOR, flush=True)
            return 1
        return int(
            ci_host.provision_ci_host(
                hostname=hostname,
                env=env,
                flavor=flavor,
                image=image,
                network=network,
                volume_size_gb=volume_size_gb,
                floating_ip=floating_ip,
                key_name=key_name,
                bundle_dir=bundle_dir,  # only an EXPLICIT override; never the stale bundle
                provider_name=provider_name,
                clouds_yaml=clouds_yaml,
                cloud=cloud,
                dry_run=dry_run,
                yes=yes,
                provider=provider,
            )
        )
