from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
PRESET_FILE = ROOT / "Config" / "AgrarianRenderingPresets.ini"
DOCS_FILE = ROOT / "Docs" / "Rendering" / "InvestorRenderingPresets.md"
ROADMAP_FILE = ROOT / "AGRARIAN_DEVELOPMENT_ROADMAP.md"


def read_presets():
    presets = {}
    active_section = None
    for raw_line in PRESET_FILE.read_text(encoding="utf-8").splitlines():
        line = raw_line.strip()
        if not line or line.startswith(";"):
            continue
        if line.startswith("[") and line.endswith("]"):
            active_section = line[1:-1]
            presets[active_section] = {"CVars": []}
            continue
        if not active_section or "=" not in line:
            continue

        key, value = line.split("=", 1)
        if key == "+CVars":
            presets[active_section]["CVars"].append(value)
        else:
            presets[active_section][key] = value
    return presets


def main():
    failures = []
    presets = read_presets()
    default = presets.get("AgrarianRenderingPreset.Default")
    recommended = presets.get("AgrarianRenderingPreset.Recommended")
    cinematic = presets.get("AgrarianRenderingPreset.Cinematic")

    if not default:
        failures.append("missing AgrarianRenderingPreset.Default")
    if not recommended:
        failures.append("missing AgrarianRenderingPreset.Recommended")
    if not cinematic:
        failures.append("missing AgrarianRenderingPreset.Cinematic")

    for label, section in [("Default", default), ("Recommended", recommended)]:
        if not section:
            continue
        cvars = set(section.get("CVars", []))
        if section.get("RayTracing") != "Disabled":
            failures.append(f"{label} RayTracing must be Disabled")
        for cvar in ["r.RayTracing=0", "r.Lumen.HardwareRayTracing=0", "r.PathTracing=0"]:
            if cvar not in cvars:
                failures.append(f"{label} missing non-ray-tracing CVar {cvar}")
        if "r.RayTracing=1" in cvars or "r.Lumen.HardwareRayTracing=1" in cvars:
            failures.append(f"{label} enables ray tracing")

    if cinematic:
        cvars = set(cinematic.get("CVars", []))
        if cinematic.get("RayTracing") != "Optional":
            failures.append("Cinematic RayTracing must be Optional")
        if "r.RayTracing=1" not in cvars:
            failures.append("Cinematic must be the only preset that opts into r.RayTracing=1")

    docs_text = DOCS_FILE.read_text(encoding="utf-8")
    for snippet in [
        "Default is the investor demo baseline",
        "Ray tracing: disabled.",
        "Cinematic is optional",
        "No gameplay feature should require ray tracing.",
    ]:
        if snippet not in docs_text:
            failures.append(f"{DOCS_FILE} missing snippet: {snippet}")

    roadmap_text = ROADMAP_FILE.read_text(encoding="utf-8")
    if "[x] Verify the non-ray-traced compatibility/default path" not in roadmap_text:
        failures.append("roadmap item is not marked complete")

    if failures:
        raise SystemExit("Non-ray-traced default verification failed:\n" + "\n".join(failures))

    print("PASS: default and recommended investor rendering paths are non-ray-traced; cinematic is optional.")


if __name__ == "__main__":
    main()
