#!/usr/bin/env python3
"""Validate dedicated server target and Ubuntu gameplay host bootstrap wiring."""

from pathlib import Path
import sys

ROOT = Path(__file__).resolve().parents[1]


def read(relative_path: str) -> str:
    path = ROOT / relative_path
    if not path.exists():
        raise AssertionError(f"Missing required file: {relative_path}")
    return path.read_text(encoding="utf-8")


def require(content: str, needle: str, context: str) -> None:
    if needle not in content:
        raise AssertionError(f"Missing {needle!r} in {context}")


def main() -> int:
    errors: list[str] = []
    checks = {
        "Source/AgrarianGameServer.Target.cs": [
            "public class AgrarianGameServerTarget",
            "Type = TargetType.Server",
            'ExtraModuleNames.Add("AgrarianGame")',
        ],
        "Scripts/BuildLinuxDedicatedServer-Windows.bat": [
            "AgrarianGameServer Linux Development",
            "Builds\\LinuxServerDevelopment",
            "-serverplatform=Linux",
            "-serverconfig=Development",
            "/Game/Agrarian/Maps/L_GroundZeroTerrain_Test",
        ],
        "Operations/cloud-game-server/bootstrap_ubuntu_game_server.sh": [
            "play.agrariangame.com",
            "AGRARIAN_GAME_PORT:-7777",
            "/opt/agrarian/server",
            "agrarian-game-server.service",
            "ufw allow",
            "systemctl restart agrarian-game-server",
        ],
        "Docs/Ops/DedicatedServerBuildRunbook.md": [
            "play.agrariangame.com",
            "7777/udp",
            "Operations/cloud-game-server/bootstrap_ubuntu_game_server.sh",
            "DigitalOcean",
            "agrarian-game-server",
        ],
        "AGRARIAN_DEVELOPMENT_ROADMAP.md": [
            "[x] Create dedicated server build target if needed.",
            "AgrarianGameServer",
            "play.agrariangame.com",
            "7777/udp",
        ],
    }

    for relative_path, needles in checks.items():
        try:
            content = read(relative_path)
            for needle in needles:
                require(content, needle, relative_path)
        except AssertionError as exc:
            errors.append(str(exc))

    if errors:
        for error in errors:
            print(f"ERROR: {error}", file=sys.stderr)
        return 1

    print("PASS: dedicated server target and Ubuntu gameplay bootstrap are wired.")
    return 0


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