"""SSOT for all path prefix translations across layout versions.
Both migrate_v3_layout.py and sidecar.restore_from_archive import from here."""

PREFIX_MAP_V2_TO_V3 = {
    "sequences/": "prep/",
    "state/": "_pipeline/state/",
    "bible/": "scripting/bible/",
    "episodes/": "scripting/episodes/",
    "development/": "scripting/development/",
    "compiled/": "scripting/compiled/",
    "storyboards/": "_pipeline/shot_plans/",
    "annotations/": "_pipeline/annotations/",
    "sessions/": "_pipeline/sessions/",
    "audio/": "_pipeline/audio/",
    "tests/": "_pipeline/tests/",
    "archive/": "_pipeline/archive/",
    "visual/": "_pipeline/visual/",
    # Asset-class normalization: legacy character classes ("identity" v2,
    # "characters" from the v1 output/refs swap) → the v3 canonical "char".
    # (REC-185: assets/identity is the stale v2 tree; assets/char is canonical.)
    "assets/identity/": "assets/char/",
    "assets/characters/": "assets/char/",
}

# v1 prefixes (legacy archives may reference these)
PREFIX_MAP_V1_TO_V2 = {
    "output/refs/": "assets/",
    "output/frames/": "sequences/",
    "output/previs/": "sequences/",
    "output/video/": "renders/",
    "output/bundles/": "state/bundles/",
    "output/manifests/": "state/manifests/",
}


def translate_legacy_path(path_str: str) -> str:
    """Translate a v1 or v2 path string to v3. Chains v1->v2->v3."""
    # First apply v1->v2
    for old, new in PREFIX_MAP_V1_TO_V2.items():
        if path_str.startswith(old):
            path_str = new + path_str[len(old):]
            break
    # Then apply v2->v3
    for old, new in PREFIX_MAP_V2_TO_V3.items():
        if path_str.startswith(old):
            path_str = new + path_str[len(old):]
            break
    return path_str
