#!/usr/bin/env python3
"""Update HexaHost file headers — fixed-width box, readable ASCII."""
from __future__ import annotations

import pathlib
import re

ROOT = pathlib.Path(__file__).resolve().parent.parent

# Clear, readable banner — spells "Axiom" literally
ASCII_LINES = [
    "+-+-+-+-+-+-+-+-+",
    "|H|e|x|a|H|o|s|t|",
    "+-+-+-+-+-+-+-+-+",
]

INNER = 66  # chars between ║ … ║ (total line length = 70 for .py)


def box_line(content: str, ts: bool = False) -> str:
    padded = content.center(INNER)
    if len(padded) != INNER:
        raise ValueError(f"Line width {len(padded)} != {INNER}: {content!r}")
    if ts:
        line = f" * ║{padded}║"
        assert len(line) == 71, line
        return line
    line = f"# ║{padded}║"
    assert len(line) == 70, line
    return line


def py_header() -> str:
    lines = [
        "# ╔══════════════════════════════════════════════════════════════════╗",
        box_line(""),
    ]
    lines.extend(box_line(art) for art in ASCII_LINES)
    lines.extend([
        box_line(""),
        box_line("© 2026 HexaHost — All Rights Reserved"),
        box_line(""),
        box_line("discord  ──  https://discord.gg/hexahost"),
        box_line("github   ──  https://github.com/theoneandonlymace"),
        box_line(""),
        "# ╚══════════════════════════════════════════════════════════════════╝",
    ])
    return "\n".join(lines)


def ts_header() -> str:
    lines = [
        "/**",
        " * ╔══════════════════════════════════════════════════════════════════╗",
        box_line("", ts=True),
    ]
    lines.extend(box_line(art, ts=True) for art in ASCII_LINES)
    lines.extend([
        box_line("", ts=True),
        box_line("© 2026 HexaHost — All Rights Reserved", ts=True),
        box_line("", ts=True),
        box_line("discord  ──  https://discord.gg/hexahost", ts=True),
        box_line("github   ──  https://github.com/theoneandonlymace", ts=True),
        box_line("", ts=True),
        " * ╚══════════════════════════════════════════════════════════════════╝",
        " */",
    ])
    return "\n".join(lines)


PY_HEADER_RE = re.compile(r"^# ╔═+[\s\S]*?^# ╚═+╝\n?", re.MULTILINE)
TS_HEADER_RE = re.compile(r"^/\*\*\n \* ╔═+[\s\S]*? \*/\n?", re.MULTILINE)


def update_file(path: pathlib.Path) -> bool:
    text = path.read_text(encoding="utf-8")
    if path.suffix in {".ts", ".tsx"}:
        if not TS_HEADER_RE.search(text):
            return False
        new_text = TS_HEADER_RE.sub(ts_header() + "\n", text, count=1)
    else:
        if not PY_HEADER_RE.search(text):
            return False
        new_text = PY_HEADER_RE.sub(py_header() + "\n", text, count=1)
    if new_text != text:
        path.write_text(new_text, encoding="utf-8")
        return True
    return False


def main() -> None:
    updated = 0
    for pattern in ("bot/**/*.py", "dashboard/**/*.ts", "dashboard/**/*.tsx"):
        for path in ROOT.glob(pattern):
            if "node_modules" in path.parts:
                continue
            if update_file(path):
                updated += 1
    print(f"Updated {updated} file headers")


if __name__ == "__main__":
    main()
