"""Seed the tartarus maintenance-shaft sublocations block into the bible.

Phase-1 (REC-134 C1) DATA deliverable, shipped as an idempotent migration so
a clean machine can reproduce the seed (the bible is project data on the
Dropbox data root — it cannot ride in the repo diff). Run:

    python3 recoil/pipeline/tools/seed_shaft_sublocations.py [--project tartarus]

Writes only when the block is absent; never overwrites existing descriptions.
"""
from __future__ import annotations

import argparse
import json
import os
import sys

_HERE = os.path.dirname(os.path.abspath(__file__))
_REPO_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(_HERE)))
if _REPO_ROOT not in sys.path:
    sys.path.insert(0, _REPO_ROOT)

from recoil.core.paths import ProjectPaths  # noqa: E402

LOCATION_ID = "int_lower_decks_maintenance_shaft"

SUBLOCATIONS = {
    "shaft_lip": {
        "description": "The grated rim where the maintenance corridor opens onto the shaft \u2014 the last solid footing before the void, metal grating underfoot and the unrailed edge dropping away into the abyss."
    },
    "pod_platform": {
        "description": "A metal grating platform jutting from the shaft wall without railings, the shaft's central foothold where anchor cables tie off and routes branch toward the catwalk and the drop. Cold abyss light rises across it from below."
    },
    "anchor_cables": {
        "description": "Anchor cables strung under tension across the shaft, groaning and vibrating with the ship's pulse, sparks spitting from the stressed joints where they meet the ceiling struts."
    },
    "upper_catwalk": {
        "description": "An elevated catwalk crossing the upper shaft among the ceiling struts that disappear into darkness above, reached by climbing the anchor cables \u2014 the highest vantage over the whole drop."
    },
    "the_drop": {
        "description": "The bottomless vertical void at the shaft's heart, anchor cables and struts plunging into darkness below as cool blue light rises from an unknown source far down and wind howls upward out of it."
    }
}


def seed(project: str) -> bool:
    """Returns True if the bible was modified."""
    path = ProjectPaths.for_project(project).global_bible_path
    bible = json.loads(path.read_text(encoding="utf-8"))
    loc = bible["locations"][LOCATION_ID]
    if loc.get("sublocations"):
        return False
    loc["sublocations"] = SUBLOCATIONS
    path.write_text(json.dumps(bible, indent=2) + "\n", encoding="utf-8")
    return True


def main() -> int:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--project", default="tartarus")
    args = parser.parse_args()
    changed = seed(args.project)
    print("seeded" if changed else "already seeded — no changes")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
