from __future__ import annotations

import argparse
import sys
from pathlib import Path

_agent_root = Path(__file__).resolve().parents[2]
if str(_agent_root) not in sys.path:
    sys.path.insert(0, str(_agent_root))
_src = _agent_root / "src"
if _src.exists() and str(_src) not in sys.path:
    sys.path.insert(0, str(_src))

from tests.pipeline_setup.env_loader import load_pipeline_setup_env
from tests.pipeline_setup_v3.core.runner import V3Runner


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(description="Run agent-backed pipeline_setup_v3 cases")
    parser.add_argument("--cases-dir", required=True, help="Directory or file with YAML case files")
    parser.add_argument("--run-name", default="manual_run", help="Output directory prefix")
    parser.add_argument("--results-dir", default=None, help="Override results root directory")
    ns = parser.parse_args(argv)

    cases_dir = Path(str(ns.cases_dir)).expanduser().resolve()
    results_dir = Path(str(ns.results_dir)).expanduser().resolve() if ns.results_dir else Path(__file__).resolve().parent / "test_results"
    load_pipeline_setup_env(start_dir=_agent_root / "tests" / "pipeline_setup")

    runner = V3Runner(cases_dir=cases_dir, results_dir=results_dir, run_name=str(ns.run_name).strip() or "manual_run")
    print(f"Cases dir: {cases_dir}")
    print(f"Run dir: {runner.run_dir}")
    results, summary_path = runner.run()
    passed = sum(1 for item in results if item.passed)
    print(f"Passed: {passed}/{len(results)}")
    print(f"Summary: {summary_path}")
    for item in results:
        if not item.passed:
            print(f"FAIL {item.case.case_id}: {'; '.join(item.mismatches)}")
    return 0 if passed == len(results) else 1


if __name__ == "__main__":
    raise SystemExit(main())
