from __future__ import annotations

import time
from pathlib import Path

from cybervisor.adapters.base import BaseAdapter, CompletedProcessHandle
from cybervisor.adapters.types import AdapterDescriptor, LaunchRequest, RunningProcess


class MockAdapter(BaseAdapter):
    descriptor = AdapterDescriptor(
        name="mock",
        display_name="Mock Agent",
        directory=Path(__file__).resolve().parent,
    )

    def start(self, request: LaunchRequest) -> RunningProcess:
        time.sleep(0.15)
        output = "Stage completed successfully"
        mock_payload = Path(".cybervisor/mock_artifact_payload.yaml")
        if mock_payload.exists():
            from cybervisor.config import contract_artifact_path

            artifact_path = Path(contract_artifact_path(request.stage_name))
            artifact_path.parent.mkdir(parents=True, exist_ok=True)
            artifact_path.write_text(mock_payload.read_text(encoding="utf-8"), encoding="utf-8")
        Path(request.log_file).parent.mkdir(parents=True, exist_ok=True)
        Path(request.log_file).write_text(output + "\n", encoding="utf-8")
        request.logger.log_to_stderr(f"[{request.stage_name}][mock] {output}")
        return CompletedProcessHandle(output=output, exit_code=0, delay_seconds=0.15)
