"""Inventory of Pages-hosted generated apps. Dry-run only.

Prints one JSON object per project that still has a successful Cloudflare Pages
deployment. It does not upload a Worker, write routing KV, or delete a Pages
project. Cohort execution waits for a separate approval.

    python -m scripts.pages_w4p_migration_dry_run
"""

from __future__ import annotations

import argparse
import json
from typing import Any


def inventory_row(
    *,
    project_id: str,
    project_ref: str,
    pages_project_name: str,
    preview_hostname: str | None,
    live_hostname: str | None,
    custom_domains: list[str],
    has_functions_api: bool,
    has_server_api: bool,
) -> dict[str, Any]:
    return {
        "project_id": project_id,
        "project_ref": project_ref,
        "pages_project_name": pages_project_name,
        "preview_hostname": preview_hostname,
        "live_hostname": live_hostname,
        "custom_domains": list(custom_domains),
        "repo_has_functions_api": has_functions_api,
        "repo_has_server_api": has_server_api,
        "action": "dry_run",
        "kv_write": False,
        "pages_delete": False,
    }


def main() -> None:
    parser = argparse.ArgumentParser(description="Print a Pages-to-W4P dry-run inventory. Does not mutate.")
    parser.add_argument("--project-id", default="")
    parser.add_argument("--project-ref", default="")
    parser.add_argument("--pages-project-name", default="")
    parser.add_argument("--preview-hostname", default="")
    parser.add_argument("--live-hostname", default="")
    parser.add_argument("--custom-domain", action="append", default=[])
    parser.add_argument("--functions-api", action="store_true")
    parser.add_argument("--server-api", action="store_true")
    args = parser.parse_args()
    if not args.project_id:
        print(json.dumps({
            "mode": "dry_run",
            "note": "Pass one project's identifiers to print its checkpoint row. This command never deletes Pages projects.",
            "pages_delete": False,
        }, indent=2))
        return
    print(json.dumps(inventory_row(
        project_id=args.project_id,
        project_ref=args.project_ref,
        pages_project_name=args.pages_project_name,
        preview_hostname=args.preview_hostname or None,
        live_hostname=args.live_hostname or None,
        custom_domains=args.custom_domain,
        has_functions_api=args.functions_api,
        has_server_api=args.server_api,
    ), indent=2))


if __name__ == "__main__":
    main()
