# ==============================================================================
# Makefile for Brightwheel Exporter Chrome Extension
# ==============================================================================

SHELL := /usr/bin/env bash

# Extract version from manifest.json
VERSION := $(shell node -p "require('./manifest.json').version" 2>/dev/null || echo "1.0.0")
DIST_DIR := dist
ZIP_NAME := brightwheel-exporter-v$(VERSION).zip
ZIP_PATH := $(DIST_DIR)/$(ZIP_NAME)

.PHONY: all build zip check clean help

all: build

## help: Display this help message
help:
	@echo "Available commands:"
	@echo "  make build (or make zip) - Build the Chrome Web Store production ZIP package"
	@echo "  make check               - Verify JavaScript syntax and manifest validity"
	@echo "  make clean               - Remove the dist directory and build artifacts"

## check: Verify JavaScript syntax and manifest validity
check:
	@echo "🔍 Validating manifest.json and JavaScript files..."
	@node -e "const m = JSON.parse(require('fs').readFileSync('manifest.json')); if (m.manifest_version !== 3) process.exit(1); if (m.description.length > 132) { console.error('❌ Description too long: ' + m.description.length + ' > 132'); process.exit(1); } console.log('✅ Manifest v' + m.version + ' OK (Description: ' + m.description.length + '/132 chars)');"
	@find scripts dashboard popup -name "*.js" | while read -r jsfile; do \
		node --check "$$jsfile" && echo "  ✓ $$jsfile valid"; \
	done
	@echo "✅ All syntax checks passed!"

## build: Package the extension into a clean production ZIP for Chrome Web Store
build: check
	@echo "📦 Packaging Chrome Extension (v$(VERSION))..."
	@mkdir -p $(DIST_DIR)
	@rm -f $(ZIP_PATH)
	@zip -r $(ZIP_PATH) . \
		-x "*.git*" \
		-x ".githooks/*" \
		-x ".github/*" \
		-x "$(DIST_DIR)/*" \
		-x "emmy/*" \
		-x "exports/*" \
		-x "data/*" \
		-x "test_data/*" \
		-x "screenshots/*" \
		-x "Makefile" \
		-x "*.md" \
		-x "LICENSE" \
		-x "*.zip" \
		-x "*.tar.gz" \
		-x "*.tmp" \
		-x ".DS_Store" \
		-x "*/.DS_Store"
	@echo ""
	@echo "🎉 Production ZIP created successfully!"
	@echo "📍 File location: $(ZIP_PATH)"
	@echo ""
	@echo "📋 ZIP Contents:"
	@unzip -l $(ZIP_PATH)

## zip: Alias for build
zip: build

## clean: Clean build output directory
clean:
	@echo "🧹 Cleaning $(DIST_DIR)..."
	@rm -rf $(DIST_DIR)
	@echo "Done."
