#!/usr/bin/env python3
"""Install versioned HSKB V14 assets into a website public directory."""

from __future__ import annotations

import shutil
import sys
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]
LOGOS = ROOT / "assets" / "logo"
SPECS = ROOT / "assets" / "spec"
FRAME = ROOT / "assets" / "frame-system" / "hskb-v14-gallery-frame.svg"


def absolute_directory(value: str, label: str) -> Path:
    path = Path(value).expanduser()
    if not path.is_absolute():
        raise ValueError(f"{label} must be an absolute path")
    path.mkdir(parents=True, exist_ok=True)
    return path


def copy(source: Path, target: Path, copied: list[str]) -> None:
    destination = target / source.name
    shutil.copy2(source, destination)
    copied.append(str(destination))


def main() -> int:
    if len(sys.argv) not in {2, 3}:
        print("Usage: install_web_assets.py /absolute/path/to/public/brand [absolute/path/to/public/images]", file=sys.stderr)
        return 2
    try:
        brand = absolute_directory(sys.argv[1], "brand target")
        images = absolute_directory(sys.argv[2], "images target") if len(sys.argv) == 3 else None
    except ValueError as exc:
        print(exc, file=sys.stderr)
        return 2

    copied: list[str] = []
    for source in sorted(LOGOS.glob("hskb-v14-*")):
        if source.name == "hskb-v14-social-card.png":
            if images is not None:
                copy(source, images, copied)
            continue
        copy(source, brand, copied)
    for source in sorted(SPECS.glob("*.json")):
        copy(source, brand, copied)
    copy(FRAME, brand, copied)

    print(f"Copied {len(copied)} locked V14 assets.")
    if images is None:
        print("Social card skipped; pass public/images as the optional second target to install it.")
    print("Update Header, Footer, icons, manifest, metadata, JSON-LD, social cards, tests and cache rules together.")
    print("Run validate_brand_assets.py before handoff; public trademark use still requires rights clearance.")
    return 0


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