from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
FRONTEND_CPP = ROOT / "Source" / "AgrarianGame" / "AgrarianMvpFrontendWidget.cpp"
ROADMAP = ROOT / "AGRARIAN_DEVELOPMENT_ROADMAP.md"

COMMON_RESOLUTIONS = [
    (1280, 720),
    (1366, 768),
    (1600, 900),
    (1920, 1080),
    (2560, 1440),
]
SUPPORTED_SCALES = [0.75, 1.0, 1.25, 1.5]


def panel_size(width, height, scale):
    minimum_margin = 24.0
    preferred_width = 780.0
    preferred_height = 430.0
    available_width = max(320.0, width - (minimum_margin * 2.0))
    available_height = max(240.0, height - (minimum_margin * 2.0))
    return min(available_width, preferred_width * scale), min(available_height, preferred_height * scale)


def main():
    frontend_text = FRONTEND_CPP.read_text(encoding="utf-8")
    roadmap_text = ROADMAP.read_text(encoding="utf-8")

    expected_source = [
        "MinimumPanelMargin = 24.0f",
        "PreferredPanelWidth = 780.0f",
        "PreferredPanelHeight = 430.0f",
        "AvailablePanelSize",
        "FMath::Clamp(UiScale, 0.75f, 1.5f)",
    ]
    missing = [snippet for snippet in expected_source if snippet not in frontend_text]

    for width, height in COMMON_RESOLUTIONS:
        for scale in SUPPORTED_SCALES:
            panel_width, panel_height = panel_size(width, height, scale)
            if panel_width > width - 48.0:
                missing.append(f"panel overflows width at {width}x{height} scale {scale}")
            if panel_height > height - 48.0:
                missing.append(f"panel overflows height at {width}x{height} scale {scale}")
            if panel_width < 320.0 or panel_height < 240.0:
                missing.append(f"panel below minimum at {width}x{height} scale {scale}")

    if "[x] Ensure UI scales on common resolutions." not in roadmap_text:
        missing.append("roadmap item not complete")

    if missing:
        raise RuntimeError("MVP UI common resolution scaling verification failed: " + "; ".join(missing))

    print("Agrarian MVP UI common resolution scaling verification complete.")


if __name__ == "__main__":
    main()
