"""Install booking.appointment host paths into the existing three Starter runtimes."""
from __future__ import annotations

import json
from pathlib import Path
import sys

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))

from src.domain.app_blocks.hosts import merge_host_map


def main():
    for framework, impls in (
        ("html", {
            "book": "js/booking-blocks.js#mountBookingForm",
            "success": "js/starter-library.js#renderSuccessState",
            "empty": "js/starter-library.js#renderEmptyState",
        }),
        ("nuxt_app", {
            "book": "components/starter/blocks/BookingForm.vue",
            "success": "components/starter/SuccessState.vue",
            "empty": "components/starter/EmptyState.vue",
        }),
        ("react_app", {
            "book": "src/components/starter/booking-blocks.jsx#BookingForm",
            "success": "src/components/starter/library.jsx#SuccessState",
            "empty": "src/components/starter/library.jsx#EmptyState",
        }),
    ):
        path = ROOT / "boilerplates" / framework / "p2hs/app-block-hosts.json"
        hosts = merge_host_map(json.loads(path.read_text(encoding="utf-8")))
        for host in hosts["hosts"]:
            if host["block"] == "booking.appointment" and host["slot"] in impls:
                host["implementation"] = impls[host["slot"]]
        hosts["hosts"] = [host for host in hosts["hosts"] if host.get("implementation")]
        missing = [
            host for host in hosts["hosts"]
            if host["block"] == "booking.appointment" and host["slot"] in impls
        ]
        if len(missing) != 3:
            raise SystemExit(f"unmapped booking slots for {framework}: {missing}")
        path.write_text(json.dumps(hosts, indent=2) + "\n", encoding="utf-8")
    print("booking.appointment hosts materialized")


if __name__ == "__main__":
    main()
