"""Persist raw and redacted cassette stores."""

import json
from pathlib import Path

from recording.model import RecordedInteraction
from recording.redaction import redact_cassette


def write_cassettes(
    fixture_root: Path,
    scenario_key: str,
    recordings: dict[str, list[RecordedInteraction]],
) -> tuple[Path, Path]:
    """Write ignored raw data first, then separately mapped safe cassettes."""
    raw_root = fixture_root / "raw" / scenario_key
    redacted_root = fixture_root / scenario_key
    raw_root.mkdir(parents=True, exist_ok=True)
    redacted_root.mkdir(parents=True, exist_ok=True)
    for service, interactions in recordings.items():
        raw_payload = [interaction.as_dict() for interaction in interactions]
        _write_json(raw_root / f"{service}.json", raw_payload)
        redacted_payload = redact_cassette(raw_payload)
        _write_json(redacted_root / f"{service}.json", redacted_payload)
    return raw_root, redacted_root


def _write_json(path: Path, payload: object) -> None:
    path.write_text(
        json.dumps(payload, ensure_ascii=True, indent=2, sort_keys=True) + "\n",
        encoding="ascii",
    )
