"""NexaOffice year-view readability preflight.

The annual calendar must adapt without making day cells too small on common
Windows laptop sizes.  This check prevents regressions where height/low-power
mode turns the year view into an overly tiny layout or where high-DPI
Configure timing leaves a blank footer below the 12-month grid.
"""
from __future__ import annotations

import json
import re
import sys
from datetime import datetime
from pathlib import Path

ROOT = Path(__file__).resolve().parent.parent
REPORT_PATH = ROOT / "data" / "year_view_fit_latest.json"
CALENDAR = ROOT / "pages" / "calendar_page.py"


def main() -> int:
    text = CALENDAR.read_text(encoding="utf-8", errors="ignore") if CALENDAR.exists() else ""
    issues = []
    if "compact_year = low_power" in text:
        issues.append("year view still shrinks because of low-power mode")
    if re.search(r"compact_year\s*=.*height\s*<\s*6[0-9]{2}", text):
        issues.append("year view still shrinks directly because of small height")
    if "width >= 1220" not in text or "elif width >= 860" not in text or "elif width >= 620" not in text:
        issues.append("year view column thresholds do not match readable laptop layout")
    if "min_card_h = 218 if compact_year else (262 if cols >= 3 else 248)" not in text:
        issues.append("year view comfortable card height floor is too small or missing")
    if "root_remaining_h" not in text:
        issues.append("year view does not use root-relative remaining height for DPI-safe viewport measurement")
    if "fill_based_h = max(min_card_h, viewport_fill_h)" not in text or "draw_gap = max(gap" not in text:
        issues.append("year view does not grow/distribute cards to fill available viewport height")
    if "day_font = 10 if compact_year else (12 if card_h >= 270 else 11)" not in text:
        issues.append("year view adaptive day number font floor is too small or missing")
    if "height should be handled by vertical scrolling" not in text:
        issues.append("year view readability comment/guard missing")
    if "large empty band under December" not in text:
        issues.append("year view blank-space regression guard missing")
    if "height_candidates = [canvas.winfo_height() - 8, wrap.winfo_height() - 8, frame_h, root_remaining_h]" not in text:
        issues.append("year view does not use stable parent/canvas/root height candidates")
    if "blank_footer = visible_h - total_h" not in text:
        issues.append("year view persistent blank-footer guard missing")
    if "one delayed stabilization repaint is enough" not in text or "canvas.tag_bind(\"year_date\"" not in text:
        issues.append("year view performance-friendly canvas redraw/bind guard missing")

    report = {
        "time": datetime.now().isoformat(timespec="seconds"),
        "status": "ok" if not issues else "failed",
        "issues": issues,
        "scenarios": [
            {"window": "1366x768", "expectation": "readable 3-column year view with vertical scroll, not tiny shrink"},
            {"window": "1280x720", "expectation": "readable 3-column or 2-column year view with vertical scroll"},
            {"window": "1920x1080", "expectation": "4-column year view fills the main canvas without a large blank footer"},
            {"window": "960x640", "expectation": "2-column year view, right panel may collapse"},
        ],
    }
    if "--write-report" in sys.argv:
        REPORT_PATH.parent.mkdir(parents=True, exist_ok=True)
        REPORT_PATH.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
    print(json.dumps(report, ensure_ascii=False, indent=2))
    return 0 if not issues else 1


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