#!/usr/bin/env python3
"""Sync the canonical observability CSS to the Flask-served static path.

The project keeps the design-system source at:
  static/css/observability-system.css

Production Flask static serving currently exposes:
  web/static/css/observability-system.css

This helper keeps the two files byte-identical so deploys do not regress into
"HTML links the CSS but /static/css/observability-system.css is 404/stale".
"""

from __future__ import annotations

import argparse
import shutil
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
SOURCE = ROOT / "static/css/observability-system.css"
TARGET = ROOT / "web/static/css/observability-system.css"


def main() -> int:
    parser = argparse.ArgumentParser(description="Sync or check observability CSS mirror")
    parser.add_argument(
        "--check",
        action="store_true",
        help="Only verify source and Flask static mirror are byte-identical.",
    )
    args = parser.parse_args()

    if not SOURCE.exists():
        print(f"ERROR: missing source CSS: {SOURCE.relative_to(ROOT)}")
        return 1

    if not args.check:
        TARGET.parent.mkdir(parents=True, exist_ok=True)

    source_bytes = SOURCE.read_bytes()
    target_bytes = TARGET.read_bytes() if TARGET.exists() else b""

    if source_bytes == target_bytes:
        action = "check" if args.check else "sync"
        print(f"Observability CSS {action}: already in sync")
        print(f"- source: {SOURCE.relative_to(ROOT)}")
        print(f"- target: {TARGET.relative_to(ROOT)}")
        return 0

    if args.check:
        print("Observability CSS check: FAIL")
        print(f"- source: {SOURCE.relative_to(ROOT)}")
        print(f"- target: {TARGET.relative_to(ROOT)}")
        print("- fix: python3 scripts/sync_observability_css.py")
        return 1

    shutil.copyfile(SOURCE, TARGET)
    print("Observability CSS sync: synced")
    print(f"- source: {SOURCE.relative_to(ROOT)}")
    print(f"- target: {TARGET.relative_to(ROOT)}")
    print(f"- bytes: {len(source_bytes)}")
    return 0


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