from __future__ import annotations

from datetime import datetime
from pathlib import Path

from tests.pipeline_setup_v4.core.artifacts import ArtifactLayout, ArtifactWriter
from tests.pipeline_setup_v4.core.case_loader import CaseDirectoryLoader
from tests.pipeline_setup_v4.core.models import V4CaseResult
from tests.pipeline_setup_v4.core.validators import CaseValidator
from tests.pipeline_setup_v4.executors.registry import ExecutorRegistry


class V4Runner:
    def __init__(self, cases_dir: Path, *, run_name: str) -> None:
        self._cases_dir = cases_dir
        self._validator = CaseValidator()
        self._executors = ExecutorRegistry()
        self._writer = ArtifactWriter(ArtifactLayout(run_name=run_name, started_at=datetime.now()))

    def run(self) -> tuple[list[V4CaseResult], list[Path]]:
        results: list[V4CaseResult] = []
        for case in CaseDirectoryLoader().load(self._cases_dir):
            payload = self._executors.execute(case.component, case)
            mismatches = self._validator.validate(case, payload.actual)
            result = V4CaseResult(
                case=case,
                actual=payload.actual,
                details=payload.details,
                passed=not mismatches,
                mismatches=mismatches,
            )
            self._writer.write_case(result)
            results.append(result)
        return results, self._writer.write_summaries(results)
