from __future__ import annotations

from pathlib import Path

from ..util import load_json
from .base import BackendResult, TranscriptionRequest


class FixtureBackend:
    name = "fixture"

    def __init__(self, fixture_directory: Path) -> None:
        self.fixture_directory = fixture_directory

    def transcribe(self, request: TranscriptionRequest) -> BackendResult:
        fixture = self.fixture_directory / f"{request.page_id}.json"
        if not fixture.is_file():
            return BackendResult(
                success=False,
                exit_code=1,
                final_message=None,
                stdout="",
                stderr="",
                command=["fixture", str(fixture)],
                error=f"Missing fixture: {fixture}",
            )
        value = load_json(fixture)
        import json

        return BackendResult(
            success=True,
            exit_code=0,
            final_message=json.dumps(value, ensure_ascii=False),
            stdout="",
            stderr="",
            command=["fixture", str(fixture)],
        )

