.PHONY: help install test test-watch test-coverage test-ui clean serve lint format deps-check deps-update

# Default target
help:
	@echo "Timeline SVG Generator - Make Targets"
	@echo "====================================="
	@echo ""
	@echo "Development:"
	@echo "  install       Install development dependencies"
	@echo "  serve         Start local development server"
	@echo "  clean         Clean node_modules and coverage"
	@echo ""
	@echo "Testing:"
	@echo "  test          Run all tests once"
	@echo "  test-watch    Run tests in watch mode (TDD)"
	@echo "  test-coverage Run tests with coverage report"
	@echo "  test-ui       Open browser-based test UI"
	@echo ""
	@echo "Quality:"
	@echo "  lint          Run code linting (if configured)"
	@echo "  format        Format code (if configured)"
	@echo ""
	@echo "Dependencies:"
	@echo "  deps-check    Check for outdated dependencies"
	@echo "  deps-update   Update dependencies"
	@echo "  deps-audit    Security audit of dependencies"
	@echo ""

# Development setup
install:
	@echo "Installing development dependencies..."
	@# Workaround for Node.js v24 + OpenSSL 3.x in WSL (ERR_SSL_CIPHER_OPERATION_FAILED)
	@export NODE_OPTIONS="--openssl-legacy-provider" && npm install
	@echo "✅ Dependencies installed"

# Testing targets
test:
	@echo "Running tests..."
	@if [ ! -d "node_modules" ]; then \
		echo "❌ Dependencies not installed. Run 'make install' first."; \
		exit 1; \
	fi
	npm test

test-watch:
	@echo "Starting test watch mode (press 'q' to quit)..."
	@if [ ! -d "node_modules" ]; then \
		echo "❌ Dependencies not installed. Run 'make install' first."; \
		exit 1; \
	fi
	npm run test:watch

test-coverage:
	@echo "Running tests with coverage..."
	@if [ ! -d "node_modules" ]; then \
		echo "❌ Dependencies not installed. Run 'make install' first."; \
		exit 1; \
	fi
	npm run test:coverage
	@echo "📊 Coverage report generated in coverage/ directory"

test-ui:
	@echo "Opening test UI in browser..."
	@if [ ! -d "node_modules" ]; then \
		echo "❌ Dependencies not installed. Run 'make install' first."; \
		exit 1; \
	fi
	npm run test:ui

# Development server (simple HTTP server for testing)
serve:
	@if command -v python3 >/dev/null 2>&1; then \
		echo "Starting development server on http://localhost:8000..."; \
		echo "📂 Serving files from current directory"; \
		echo "Press Ctrl+C to stop"; \
		python3 -m http.server 8000; \
	elif command -v python >/dev/null 2>&1; then \
		echo "Starting development server on http://localhost:8000..."; \
		echo "📂 Serving files from current directory"; \
		echo "Press Ctrl+C to stop"; \
		python -m SimpleHTTPServer 8000; \
	elif command -v npx >/dev/null 2>&1; then \
		echo "Starting development server with npx..."; \
		npx serve -l 8000; \
	else \
		echo "❌ No suitable HTTP server found"; \
		echo "Install Python or Node.js to use 'make serve'"; \
		exit 1; \
	fi

# Code quality (placeholder targets for future use)
lint:
	@if [ -f "package.json" ] && npm list eslint >/dev/null 2>&1; then \
		echo "Running ESLint..."; \
		npx eslint *.js; \
	else \
		echo "ℹ️  ESLint not configured. Skipping lint check."; \
		echo "   To add linting: npm install --save-dev eslint"; \
	fi

format:
	@if [ -f "package.json" ] && npm list prettier >/dev/null 2>&1; then \
		echo "Formatting code with Prettier..."; \
		npx prettier --write "*.js" "test/*.js"; \
	else \
		echo "ℹ️  Prettier not configured. Skipping code formatting."; \
		echo "   To add formatting: npm install --save-dev prettier"; \
	fi

# Dependency management
deps-check:
	@echo "Checking for outdated dependencies..."
	npm outdated

deps-update:
	@echo "Updating dependencies..."
	npm update
	@echo "✅ Dependencies updated"

deps-audit:
	@echo "Running security audit..."
	npm audit
	@echo "🔒 Security audit complete"

# Cleanup
clean:
	@echo "Cleaning up..."
	@if [ -d "node_modules" ]; then rm -rf node_modules && echo "🗑️  Removed node_modules/"; fi
	@if [ -d "coverage" ]; then rm -rf coverage && echo "🗑️  Removed coverage/"; fi
	@if [ -d ".vitest" ]; then rm -rf .vitest && echo "🗑️  Removed .vitest/"; fi
	@echo "✅ Cleanup complete"

# Quick setup for new developers
setup: clean install test
	@echo ""
	@echo "🎉 Setup complete! You can now:"
	@echo "   • Run 'make test' to run tests"
	@echo "   • Run 'make test-watch' for TDD"
	@echo "   • Run 'make serve' to start development server"
	@echo "   • Open index.html in browser to use the app"