#!/usr/bin/env python3
"""Task 17 — FARM GAME re-theme sweep.

Maps the old neon-green esports palette to the FARM GAME visual language:
violet→indigo metallic gradient, plum-charcoal darks, dusty paper lights.
Case-insensitive, handles hex / rgba() / bare JS tuples / 0x literals.
"""
import os
import re

ROOT = "/home/z/my-project/fightskill"
TARGETS = []
for sub in ("app", "scripts/make_pwa_icons.py"):
    p = os.path.join(ROOT, sub)
    if os.path.isdir(p):
        for dirpath, _, fns in os.walk(p):
            for fn in fns:
                if fn.endswith((".css", ".html", ".js", ".webmanifest", ".svg", ".py")):
                    if fn in ("inventory_colors.py", "sample_theme.py"):
                        continue  # tooling must keep its green detector
                    TARGETS.append(os.path.join(dirpath, fn))
    else:
        TARGETS.append(p)

# (old, new) applied literally; hex pairs are also matched case-insensitively
PAIRS = [
    # ── brand accent: neon green → violet family ──
    ("56F569", "A678E8"),
    ("3ED551", "8F5FD6"),
    ("1FBF36", "7B52D6"),
    ("17A02C", "6A41C2"),
    ("2EA84A", "7B52D6"),
    ("00FF88", "8C64EB"),
    ("00C46A", "5F6FD3"),
    ("0B3315", "2A1B4E"),
    ("334433", "3A3352"),
    ("DFFFE6", "EFE8FB"),
    ("05070A", "0A0812"),
    # ── rgba / bare tuples: green → violet/indigo ──
    ("86, 245, 105", "166, 120, 232"),
    ("86,245,105", "166,120,232"),
    ("0, 255, 136", "140, 100, 235"),
    ("0,255,136", "140,100,235"),
    ("57, 255, 20", "171, 124, 245"),
    ("57,255,20", "171,124,245"),
    ("31, 191, 54", "123, 82, 214"),
    ("31,191,54", "123,82,214"),
    ("29, 212, 123", "150, 110, 230"),
    ("29,212,123", "150,110,230"),
    ("0, 204, 106", "120, 90, 220"),
    ("0,204,106", "120,90,220"),
    ("134, 255, 156", "190, 160, 250"),
    ("134,255,156", "190,160,250"),
    # ── dark surfaces: blue-black → plum-charcoal ──
    ("080A0D", "100D16"),
    ("0D1116", "16121F"),
    ("101419", "1A1524"),
    ("0A0F14", "131019"),
    ("16, 20, 25", "26, 21, 36"),
    ("16,20,25", "26,21,36"),
    ("13, 17, 22", "22, 18, 31"),
    ("13,17,22", "22,18,31"),
    ("5, 8, 16", "12, 10, 18"),
    ("5,8,16", "12,10,18"),
    # ── light theme: cool grey → dusty warm paper ──
    ("F0F2F5", "EAE3DA"),
    ("E4E6E8", "DFD6CB"),
    ("216, 216, 216", "214, 203, 190"),
    ("216,216,216", "214,203,190"),
    ("1A1D22", "2A2433"),
    ("4A5057", "5D5568"),
    ("757B82", "8B8496"),
    # ── neutral grey → grey-violet dust ──
    ("606060", "6E6488"),
]

changed = {}
for path in TARGETS:
    src = open(path, encoding="utf-8", errors="replace").read()
    out = src
    hits = 0
    for old, new in PAIRS:
        # case-insensitive for hex tokens (CSS accepts any case)
        if re.fullmatch(r"[0-9A-F]{6}", old):
            out, n = re.subn(re.escape(old), new, out, flags=re.IGNORECASE)
        else:
            n = out.count(old)
            out = out.replace(old, new)
        hits += n
    if out != src:
        open(path, "w", encoding="utf-8").write(out)
        changed[os.path.relpath(path, ROOT)] = hits

total = 0
for f, n in sorted(changed.items(), key=lambda kv: -kv[1]):
    print(f"  {n:4d}  {f}")
    total += n
print(f"\nTOTAL replacements: {total} across {len(changed)} files")
