"""CLI flag-wiring smoke tests: every option must reach its run_* kwarg.

The commands lazily import their stage modules, so each test monkeypatches
the stage function at its source module and asserts the received kwargs —
a transposed or dropped pass-through fails HERE, not on a real run.
"""

from __future__ import annotations

from typer.testing import CliRunner

from bggpipe.cli import app

runner = CliRunner()


def _capture(monkeypatch, module: str, func: str) -> dict:
    received: dict = {}

    def fake(cfg, **kwargs):
        received.update(kwargs)
        received["cfg"] = cfg
        return []

    monkeypatch.setattr(f"bggpipe.{module}.{func}", fake)
    return received


def test_extract_flags(monkeypatch):
    received = _capture(monkeypatch, "extract", "run_extract")
    result = runner.invoke(app, ["extract", "--only", "x.jpg", "--force"])
    assert result.exit_code == 0
    assert received["only"] == "x.jpg" and received["force"] is True


def test_resolve_force(monkeypatch):
    received = _capture(monkeypatch, "resolve", "run_resolve")
    assert runner.invoke(app, ["resolve", "--force"]).exit_code == 0
    assert received["force"] is True


def test_diff_wiring(monkeypatch):
    received = _capture(monkeypatch, "diff", "run_diff")
    assert runner.invoke(app, ["diff"]).exit_code == 0
    assert "cfg" in received


def test_upload_flags(monkeypatch):
    received = _capture(monkeypatch, "upload", "run_upload")
    result = runner.invoke(
        app, ["upload", "--dry-run", "--retry-failed", "--limit", "3", "--headless"]
    )
    assert result.exit_code == 0
    assert received["dry_run"] is True
    assert received["retry_failed"] is True
    assert received["limit"] == 3
    assert received["headless"] is True
    assert received["verify"] is False


def test_enrich_refresh(monkeypatch):
    received = _capture(monkeypatch, "enrich", "run_enrich")
    assert runner.invoke(app, ["enrich", "--refresh"]).exit_code == 0
    assert received["refresh"] is True
