"""Read contract of an agent's calendar (`DATA_DIR/crm/schedules/<agent_id>.json`) for code outside the CRM calendar.

The CRM (`routes/agent_panel_scheduler.py`, which builds the same path in its own `_schedule_path`; a test keeps the two equal) owns the format and the semantics: a JSON list of events (`start`, optional `end`, ...),
written only through `modules/core/json_store.py`. Readers (today the public franchise booking of the Website) use this module instead of
knowing the path or parsing the file themselves; it adds no interpretation of the events.

* missing file          -> `[]`
* damaged / ill-typed   -> `JsonStoreCorrupt` (fail closed: it is never an empty calendar; the file is left untouched and json_store keeps its
                           one diagnostic copy next to it); a symlinked path -> `JsonStoreUnsafePath`
Callers must treat `JsonStoreError` / `OSError` as "calendar unknown" and must not offer or book time based on it.
"""
from pathlib import Path

from modules.core import storage_paths
from modules.core.json_store import read_json


def schedule_path(agent_id) -> Path:
    """Path of the agent's calendar file (resolved on every call, so DATA_DIR from the environment is honoured)."""
    return storage_paths.schedules_dir() / f"{agent_id}.json"


def read_schedule_events(agent_id) -> list:
    """The agent's calendar events as stored (a list), `[]` when the file does not exist. Raises on a damaged file (see module docstring)."""
    return read_json(schedule_path(agent_id), list)
