"""Shared helpers for workspace server and MCP server.

Extracted to avoid duplication between server.py and mcp_server.py.
"""

import sys
from datetime import datetime, timezone
from pathlib import Path

_RECOIL_ROOT = Path(__file__).resolve().parent.parent
if str(_RECOIL_ROOT) not in sys.path:
    sys.path.insert(0, str(_RECOIL_ROOT))

from recoil.core.paths import ProjectPaths
from recoil.execution.execution_store import ExecutionStore

# ── Singleton store cache ─────────────────────────────────────

_stores: dict[str, ExecutionStore] = {}


def get_store(project: str) -> ExecutionStore:
    """Return a cached ExecutionStore for the given project."""
    if project not in _stores:
        _stores[project] = ExecutionStore(project=project)
    return _stores[project]


def get_ops_log_path(project: str) -> Path:
    return ProjectPaths.for_project(project).visual_state_dir / "ops.log.jsonl"


def now_iso() -> str:
    return datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")


def shot_status_color(status: str) -> str:
    if status in ("approved", "video_complete"):
        return "green"
    if any(k in status for k in ("generating", "submitted", "processing", "downloading")):
        return "amber"
    if "failed" in status or "rejected" in status:
        return "red"
    if status in ("needs_review", "icu_escalated", "pending_qc"):
        return "purple"
    return "gray"
