"""``mgf-fed provision-ci-host`` — the deprecation shim (Wave-2 P0.4).

The implementation migrated to mgf-cloud-provision (``ci-host``). What is pinned
here is the SHIM contract: the deprecation notice always prints; the call
delegates (kwargs intact) when the successor is importable; and a missing
successor yields an actionable error, exit 1, and NO cloud action. The successor
is injected as a stub module so these tests hold whether or not
mgf-cloud-provision is installed in the environment.
"""

from __future__ import annotations

import sys
import types
from typing import Any

import pytest

from mgf.fed._provision import DEPRECATION_NOTICE, provision_ci_host


def _install_stub(monkeypatch: pytest.MonkeyPatch, rc: int = 0) -> dict[str, Any]:
    """Mount a fake mgf.cloud_provision.ci_host capturing the delegated kwargs."""
    captured: dict[str, Any] = {}

    def fake_provision_ci_host(**kwargs: Any) -> int:
        captured.update(kwargs)
        return rc

    stub = types.ModuleType("mgf.cloud_provision.ci_host")
    stub.provision_ci_host = fake_provision_ci_host  # type: ignore[attr-defined]
    pkg = types.ModuleType("mgf.cloud_provision")
    pkg.ci_host = stub  # type: ignore[attr-defined]
    monkeypatch.setitem(sys.modules, "mgf.cloud_provision", pkg)
    monkeypatch.setitem(sys.modules, "mgf.cloud_provision.ci_host", stub)
    return captured


def test_prints_deprecation_and_delegates(
    monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
    captured = _install_stub(monkeypatch)
    rc = provision_ci_host(dry_run=True)
    assert rc == 0
    out = capsys.readouterr().out
    assert "DEPRECATED" in out
    assert "mgf-cloud-provision ci-host" in out
    assert captured["dry_run"] is True
    assert captured["hostname"] == "woodpecker"


def test_kwargs_forwarded_intact(monkeypatch: pytest.MonkeyPatch) -> None:
    captured = _install_stub(monkeypatch)
    sentinel = object()
    rc = provision_ci_host(
        hostname="wp",
        env="staging",
        flavor="big",
        volume_size_gb=99,
        floating_ip=True,
        key_name="k",
        provider_name="fake",
        yes=True,
        provider=sentinel,
    )
    assert rc == 0
    assert captured["hostname"] == "wp"
    assert captured["env"] == "staging"
    assert captured["flavor"] == "big"
    assert captured["volume_size_gb"] == 99
    assert captured["floating_ip"] is True
    assert captured["key_name"] == "k"
    assert captured["provider_name"] == "fake"
    assert captured["yes"] is True
    assert captured["provider"] is sentinel
    # the stale deploy/woodpecker bundle is NEVER forwarded implicitly — only an
    # explicit --bundle-dir would be (None = the successor's packaged bundle).
    assert captured["bundle_dir"] is None


def test_delegate_exit_code_passthrough(monkeypatch: pytest.MonkeyPatch) -> None:
    _install_stub(monkeypatch, rc=3)
    assert provision_ci_host(dry_run=True) == 3


def test_missing_successor_is_actionable(
    monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
) -> None:
    # Force the import to fail even where mgf-cloud-provision is installed.
    monkeypatch.setitem(sys.modules, "mgf.cloud_provision", None)
    monkeypatch.setitem(sys.modules, "mgf.cloud_provision.ci_host", None)
    rc = provision_ci_host(yes=True)
    assert rc == 1
    out = capsys.readouterr().out
    assert "DEPRECATED" in out  # notice prints on every path
    assert "pip install mgf-cloud-provision" in out
    assert "No cloud action was taken" in out


def test_notice_names_the_successor_verb() -> None:
    assert "mgf-cloud-provision ci-host" in DEPRECATION_NOTICE
    assert "DEPRECATED" in DEPRECATION_NOTICE
