"""run configuration and resolution. deliberately no `Path.home()` —
model comes from arg/env, `zigman` from PATH, the skill dir by walking up.
"""

from __future__ import annotations

import shutil
from dataclasses import dataclass
from pathlib import Path


@dataclass
class Config:
    model: str  # provider model id (e.g. a path or "qwen2.5-coder:7b"); required
    provider: str = "local"  # Pi provider name (from ~/.pi/agent/models.json)
    base_url: str = "http://localhost:1234/v1"  # endpoint, for the reachability check
    skill_dir: Path | None = None  # skills/zigman; None => bare-only ablation
    zigman_bin: str | None = None  # resolved zigman path, or None (rely on inherited PATH)
    zig: str = "zig"
    timeout: int = 300  # hard per-run seconds
    out_dir: Path = Path("reports")


def find_skill_dir(start: Path | None = None) -> Path | None:
    """walk up from `start` (default cwd) looking for skills/zigman."""
    start = (start or Path.cwd()).resolve()
    for p in (start, *start.parents):
        cand = p / "skills" / "zigman"
        if cand.is_dir():
            return cand
    return None


def find_zigman() -> str | None:
    """locate the zigman CLI on PATH (no home assumptions)."""
    return shutil.which("zigman")


def find_pi() -> str:
    return shutil.which("pi") or "pi"
