from __future__ import annotations

from pathlib import Path

from pyimgano.workbench.config import WorkbenchConfig


def test_workbench_preflight_non_manifest_root_missing(tmp_path: Path) -> None:
    from pyimgano.workbench.preflight import run_preflight

    cfg = WorkbenchConfig.from_dict(
        {
            "recipe": "industrial-adapt",
            "seed": 123,
            "dataset": {
                "name": "mvtec",
                "root": str(tmp_path / "missing_root"),
                "category": "bottle",
                "resize": [16, 16],
                "input_mode": "paths",
            },
            "model": {"name": "vision_ecod", "device": "cpu", "pretrained": False},
            "output": {"save_run": False},
        }
    )

    report = run_preflight(config=cfg)
    codes = {i.code for i in report.issues}
    assert "DATASET_ROOT_MISSING" in codes


def test_workbench_preflight_non_manifest_invalid_category(tmp_path: Path) -> None:
    from pyimgano.workbench.preflight import run_preflight

    root = tmp_path / "mvtec"
    root.mkdir(parents=True, exist_ok=True)

    cfg = WorkbenchConfig.from_dict(
        {
            "recipe": "industrial-adapt",
            "seed": 123,
            "dataset": {
                "name": "mvtec",
                "root": str(root),
                "category": "not_a_category",
                "resize": [16, 16],
                "input_mode": "paths",
            },
            "model": {"name": "vision_ecod", "device": "cpu", "pretrained": False},
            "output": {"save_run": False},
        }
    )

    report = run_preflight(config=cfg)
    codes = {i.code for i in report.issues}
    assert "DATASET_CATEGORY_EMPTY" in codes


def test_workbench_preflight_custom_dataset_structure_validation(tmp_path: Path) -> None:
    from pyimgano.workbench.preflight import run_preflight

    root = tmp_path / "custom"
    root.mkdir(parents=True, exist_ok=True)

    cfg = WorkbenchConfig.from_dict(
        {
            "recipe": "industrial-adapt",
            "seed": 123,
            "dataset": {
                "name": "custom",
                "root": str(root),
                "category": "custom",
                "resize": [16, 16],
                "input_mode": "paths",
            },
            "model": {"name": "vision_ecod", "device": "cpu", "pretrained": False},
            "output": {"save_run": False},
        }
    )

    report = run_preflight(config=cfg)
    codes = {i.code for i in report.issues}
    assert "CUSTOM_DATASET_INVALID_STRUCTURE" in codes
    assert report.summary["dataset_readiness"]["status"] == "error"
    assert report.summary["dataset_readiness"]["issue_codes"] == ["CUSTOM_DATASET_INVALID_STRUCTURE"]


def test_workbench_preflight_non_manifest_uses_workbench_category_boundary(
    monkeypatch, tmp_path: Path
) -> None:
    import pyimgano.workbench.non_manifest_category_listing as listing_module
    import pyimgano.workbench.preflight as preflight_module

    root = tmp_path / "mvtec"
    root.mkdir(parents=True, exist_ok=True)

    calls: list[str] = []

    def _fake_list_workbench_categories(*, config):  # noqa: ANN001 - test seam
        calls.append(str(config.dataset.name))
        return ["via_loader"]

    monkeypatch.setattr(
        listing_module,
        "list_workbench_categories",
        _fake_list_workbench_categories,
    )

    cfg = WorkbenchConfig.from_dict(
        {
            "recipe": "industrial-adapt",
            "seed": 123,
            "dataset": {
                "name": "mvtec",
                "root": str(root),
                "category": "bottle",
                "resize": [16, 16],
                "input_mode": "paths",
            },
            "model": {"name": "vision_ecod", "device": "cpu", "pretrained": False},
            "output": {"save_run": False},
        }
    )

    report = preflight_module.run_preflight(config=cfg)

    assert report.summary["categories"] == ["via_loader"]
    assert calls == ["mvtec"]
