#!/usr/bin/env bash
#
# Check the theme pack that is ACTUALLY INSTALLED in code-server, and the theme the editor is
# ACTUALLY set to. Run this INSIDE the workspace.
#
# check-vscode-theme-names.sh compares the repo to itself, which cannot catch the failure that
# keeps happening: a theme name that exists in the repo but not on the machine. VS Code does not
# warn about an unknown `workbench.colorTheme` -- it silently falls back to its DEFAULT LIGHT
# theme. That is what turned the editor white twice, and it happened a third time during the
# 2026-07-25 rename (the settings still said "Adom Inverse" after the theme became "Adom Studio
# Bright Inverse"). Every one of those looked completely fine in the config.
#
# Exit 0 = the installed pack and the live setting agree with what HD expects.

set -euo pipefail

python3 - "$@" <<'PY'
import glob, json, os, sys

EXT  = os.path.expanduser('~/.local/share/code-server/extensions')
SET  = os.path.expanduser('~/.local/share/code-server/User/settings.json')

# The names HD will push. Keep in sync with VSCODE_THEME_FOR_SCHEME in src/lib/stores/settings.ts.
EXPECTED = [
    'Adom Studio',          # the default (slug 'studio' since the 2026-07-25 slug rename)
    'Adom Studio Bright',   # slug studio-bright
    'Adom Studio Dark',     # slug studio-dark (the pre-rename 'studio')
    'Adom Kickstand',
    'Adom Slate',
]

fail = False

# --- what does the machine actually have? ------------------------------------------------------
installed = {}                      # label -> (extension dir, theme file)
packs = sorted(glob.glob(os.path.join(EXT, 'adom.adom-themes-*')))
if not packs:
    print('[installed-themes] FAIL: no adom.adom-themes-* extension installed')
    fail = True
if len(packs) > 1:
    print(f'[installed-themes] FAIL: {len(packs)} theme packs installed, which makes the winner '
          f'undefined: {", ".join(os.path.basename(p) for p in packs)}')
    fail = True

for pack in packs:
    pkg = os.path.join(pack, 'package.json')
    try:
        meta = json.load(open(pkg))
    except Exception as e:
        print(f'[installed-themes] FAIL: cannot read {pkg}: {e}')
        fail = True
        continue
    print(f'[installed-themes] pack {os.path.basename(pack)} v{meta.get("version")}')
    for t in meta.get('contributes', {}).get('themes', []):
        path = os.path.join(pack, t['path'])
        installed[t['label']] = (pack, path)
        # A declared theme whose FILE is missing is the other half of the white-editor bug.
        if not os.path.exists(path):
            print(f'[installed-themes] FAIL: "{t["label"]}" declares {t["path"]}, which is absent')
            fail = True

# --- does it have everything HD will ask for? --------------------------------------------------
for label in EXPECTED:
    if label in installed:
        print(f'[installed-themes]   OK  {label}')
    else:
        print(f'[installed-themes]   MISSING {label} -- selecting this scheme in HD would turn '
              f'the editor WHITE')
        fail = True

extra = sorted(set(installed) - set(EXPECTED))
if extra:
    # Not fatal: a leftover only matters if something still points at it. Worth saying, because a
    # stale file is how a renamed theme keeps half-working on one machine and not another.
    print(f'[installed-themes] note: installed but not expected by HD: {", ".join(extra)}')

# --- and is the CURRENT setting one of them? ---------------------------------------------------
try:
    cur = json.load(open(SET)).get('workbench.colorTheme')
except Exception as e:
    print(f'[installed-themes] FAIL: cannot read {SET}: {e}')
    cur, fail = None, True

if cur is not None:
    if cur in installed:
        print(f'[installed-themes] live setting "{cur}" resolves')
    else:
        print(f'[installed-themes] FAIL: live setting "{cur}" matches no installed theme -- '
              f'VS Code is falling back to its default LIGHT theme right now')
        fail = True

sys.exit(1 if fail else 0)
PY
