#!/usr/bin/env python3
"""Check whether the configured WordPress site profiles resolve cleanly.

This prints non-secret status only.
Run from the repo root or anywhere with the repo available.
"""

from __future__ import annotations

import importlib.util
import sys
import types
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
PUBLISHER = ROOT / "03_publish_to_wordpress.py"
ROOT_ENV_CANDIDATES = [
    Path("/Users/bertmahoney/.hermes/.env"),
    Path.home() / ".hermes" / ".env",
]

# Keep this checker usable even on minimal systems where requests is not yet installed.
try:
    import requests  # noqa: F401
except ModuleNotFoundError:  # pragma: no cover - only used on minimal systems
    requests_stub = types.ModuleType("requests")
    requests_stub.__dict__["request"] = lambda *args, **kwargs: None
    requests_stub.__dict__["utils"] = types.SimpleNamespace(quote=lambda value: value)
    sys.modules["requests"] = requests_stub

spec = importlib.util.spec_from_file_location("publish_to_wordpress", PUBLISHER)
assert spec and spec.loader
publisher = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = publisher
spec.loader.exec_module(publisher)
for hermes_env in ROOT_ENV_CANDIDATES:
    publisher.load_env_file(hermes_env, override=True)

site_keys = ["BERTMAHONEY", "BERCHMAN", "BERT_FORSALE"]

exit_code = 0
for site_key in site_keys:
    try:
        site_url, username, _, source = publisher.resolve_wp_credentials(site_key)
        print(f"{site_key}: OK | source={source} | site={site_url} | user={username}")
    except SystemExit as exc:
        exit_code = 1
        print(f"{site_key}: MISSING | {exc}")

raise SystemExit(exit_code)
