"""Scanner for discovering jobs and trials in a folder."""

from pathlib import Path

from harbor.models.job.config import JobConfig
from harbor.models.job.result import JobResult
from harbor.models.trial.result import TrialResult


class JobScanner:
    """Scans a folder for job and trial data."""

    def __init__(self, jobs_dir: Path):
        self.jobs_dir = jobs_dir

    def list_jobs(self) -> list[str]:
        """List all job names (directory names) in the jobs folder."""
        if not self.jobs_dir.exists():
            return []
        return sorted(
            [d.name for d in self.jobs_dir.iterdir() if d.is_dir()],
            reverse=True,  # Most recent first
        )

    def get_job_config(self, job_name: str) -> JobConfig | None:
        """Load job config from disk."""
        config_path = self.jobs_dir / job_name / "config.json"
        if not config_path.exists():
            return None
        return JobConfig.model_validate_json(config_path.read_text())

    def get_job_result(self, job_name: str) -> JobResult | None:
        """Load job result from disk."""
        result_path = self.jobs_dir / job_name / "result.json"
        if not result_path.exists():
            return None
        return JobResult.model_validate_json(result_path.read_text())

    def list_trials(self, job_name: str) -> list[str]:
        """List all trial names in a job folder."""
        job_dir = self.jobs_dir / job_name
        if not job_dir.exists():
            return []
        return sorted(
            [
                d.name
                for d in job_dir.iterdir()
                if d.is_dir() and (d / "result.json").exists()
            ]
        )

    def get_trial_result(self, job_name: str, trial_name: str) -> TrialResult | None:
        """Load trial result from disk."""
        result_path = self.jobs_dir / job_name / trial_name / "result.json"
        if not result_path.exists():
            return None
        return TrialResult.model_validate_json(result_path.read_text())
