from __future__ import annotations

import json
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
DATA = ROOT / "data"
TOOLS = ROOT / "tools"
if str(TOOLS) not in sys.path:
    sys.path.insert(0, str(TOOLS))


def main() -> int:
    result = {
        "ok": False,
        "generate_app_icons": {"ok": False},
        "verify_icon_assets": {"ok": False},
        "desktop_doctor_available": (TOOLS / "desktop_doctor.py").exists(),
    }

    try:
        from generate_app_icons import generate_icons
        generate_icons()
        result["generate_app_icons"] = {"ok": True}
    except Exception as exc:
        result["generate_app_icons"] = {"ok": False, "error": str(exc)}

    try:
        from verify_icon_assets import verify_icons
        ok, messages = verify_icons()
        result["verify_icon_assets"] = {"ok": bool(ok), "messages": messages}
    except Exception as exc:
        result["verify_icon_assets"] = {"ok": False, "error": str(exc)}

    result["ok"] = bool(result["generate_app_icons"].get("ok")) and bool(result["verify_icon_assets"].get("ok"))

    try:
        DATA.mkdir(parents=True, exist_ok=True)
        (DATA / "desktop_preflight_latest.json").write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
    except Exception:
        pass

    print(json.dumps(result, ensure_ascii=False, indent=2))
    return 0 if result["ok"] else 1


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