.PHONY: install test typecheck build publish patch minor major clean help

# ── Config ───────────────────────────────────────────────────────────────────

PACKAGE_NAME := $(shell node -p "require('./package.json').name" 2>/dev/null)
VERSION      := $(shell node -p "require('./package.json').version" 2>/dev/null)

# ── Default ──────────────────────────────────────────────────────────────────

help:
	@echo "pi-themes — $(PACKAGE_NAME) v$(VERSION)"
	@echo ""
	@echo "  make install     Install dependencies"
	@echo "  make test        Validate all theme JSON files (51 tokens, no broken var refs)"
	@echo "  make typecheck   Type-check index.ts (no emit)"
	@echo "  make build       install + typecheck + test"
	@echo "  make patch       Bump patch version (0.1.0 → 0.1.1) and publish"
	@echo "  make minor       Bump minor version (0.1.0 → 0.2.0) and publish"
	@echo "  make major       Bump major version (0.1.0 → 1.0.0) and publish"
	@echo "  make publish     Publish current version to npm"
	@echo "  make clean       Remove node_modules"

# ── Core tasks ───────────────────────────────────────────────────────────────

install:
	npm install

test:
	@python3 -c "\
import json, glob, sys; \
errors = []; \
required = { \
    'accent','border','borderAccent','borderMuted','success','error','warning', \
    'muted','dim','text','thinkingText','selectedBg','userMessageBg','userMessageText', \
    'customMessageBg','customMessageText','customMessageLabel','toolPendingBg', \
    'toolSuccessBg','toolErrorBg','toolTitle','toolOutput','mdHeading','mdLink', \
    'mdLinkUrl','mdCode','mdCodeBlock','mdCodeBlockBorder','mdQuote','mdQuoteBorder', \
    'mdHr','mdListBullet','toolDiffAdded','toolDiffRemoved','toolDiffContext', \
    'syntaxComment','syntaxKeyword','syntaxFunction','syntaxVariable','syntaxString', \
    'syntaxNumber','syntaxType','syntaxOperator','syntaxPunctuation','thinkingOff', \
    'thinkingMinimal','thinkingLow','thinkingMedium','thinkingHigh','thinkingXhigh', \
    'bashMode' \
}; \
files = sorted(glob.glob('themes/*.json')); \
[errors.append(f'{p}: no themes/*.json found') for p in ['themes'] if not files]; \
[errors.extend([ \
    f'{path}: missing token \"{t}\"' \
    for t in required - set((lambda d: d['colors'])(json.load(open(path))).keys()) \
] + [ \
    f'{path}: colors.{k} = \"{v}\" (unresolved var)' \
    for k,v in (lambda d: d['colors'])(json.load(open(path))).items() \
    if v and not v.startswith('#') and not str(v).lstrip('-').isdigit() \
    and v not in (lambda d: d.get('vars', {}))(json.load(open(path))) \
]) for path in files]; \
[print('ERROR:', e) for e in errors]; \
sys.exit(1) if errors else print(f'✓ All {len(files)} themes valid — 51 tokens each, no broken var refs')"

typecheck:
	npx tsc --noEmit --strict --skipLibCheck --moduleResolution bundler --module esnext \
		--target esnext --allowImportingTsExtensions \
		index.ts

build: install typecheck test
	@echo "✓ Build complete — $(PACKAGE_NAME) v$(VERSION)"

# ── Publishing ───────────────────────────────────────────────────────────────

publish: clean build
	npm publish --access public
	@echo "✓ Published $(PACKAGE_NAME) v$(VERSION)"

patch: clean build
	npm version patch
	npm publish --access public
	@echo "✓ Published $(PACKAGE_NAME) v$(shell node -p "require('./package.json').version")"

minor: clean build
	npm version minor
	npm publish --access public
	@echo "✓ Published $(PACKAGE_NAME) v$(shell node -p "require('./package.json').version")"

major: clean build
	npm version major
	npm publish --access public
	@echo "✓ Published $(PACKAGE_NAME) v$(shell node -p "require('./package.json').version")"

# ── Cleanup ──────────────────────────────────────────────────────────────────

clean:
	rm -rf node_modules
