# Include shared TypeScript library dependencies
LIB_DIR := .
include deps.mk

# File associations for timestamp checking
# Format: display_name source_file output_file config_file
MAIN_CHECK := $(MAIN_JS_FILE) $(MAIN_TS) $(MAIN_JS) $(TSCONFIG_MAIN)
SETTINGS_CHECK := $(SETTINGS_JS_FILE) $(SETTINGS_TS) $(SETTINGS_JS) $(TSCONFIG_SETTINGS)

TARGETS := help install build clean clean-all dist-clean all check check-exists check-timestamps
.PHONY: $(TARGETS)
.SILENT: $(TARGETS) ts/node_modules
.DEFAULT_GOAL := all


# Default target
help:
	echo "Available targets:"
	echo "  make install         - Install npm dependencies"
	echo "  make build           - Compile TypeScript files (only if sources changed)"
	echo "  make check           - Check if outputs exist and are up to date"
	echo "  make check-exists    - Check if compiled JavaScript files exist"
	echo "  make check-timestamps - Check if outputs are newer than sources"
	echo "  make clean           - Remove compiled JavaScript files"
	echo "  make clean-all       - Remove compiled JavaScript files AND node_modules"
	echo "  make dist-clean      - Remove node_modules but keep compiled JavaScript files"
	echo "  make all             - Install dependencies and build everything"
	echo ""
	echo "The build target uses dependency tracking to avoid unnecessary recompilation."
	echo "Files are only rebuilt when source files or configuration changes."

# Install npm dependencies if not already installed
$(NODE_MODULES):
	echo "Installing dependencies..."
	cd ts/config \
		&& npm install \
		&& mv node_modules ..
	echo "Dependencies installed successfully."

install: $(NODE_MODULES)

# Compile main.js only if sources are newer (compilation only, no linting)
$(MAIN_JS): $(MAIN_JS_DEPS)
	echo "Compiling main.ts..."
	cd ts/config && npm run build:main
	echo "main.ts compiled to main.js"

# Compile settings.js only if sources are newer (compilation only, no linting)
$(SETTINGS_JS): $(SETTINGS_JS_DEPS)
	echo "Compiling settings.ts..."
	cd ts/config && npm run build:settings
	echo "settings.ts compiled to settings.js"

# Build target compiles what needs rebuilding, then runs ESLint and cleanup once
build: $(MAIN_JS) $(SETTINGS_JS)
	echo "Running ESLint fix on all compiled files..."
	cd ts/config && npm run eslint:fix
	echo "Cleaning up node_modules..."
	rm -rf ts/config/node_modules
	echo "Build complete - all outputs are up to date."

# Clean compiled JavaScript files and all associated TypeScript build artifacts
clean:
	echo "Cleaning compiled outputs..."
	rm -f $(MAIN_JS) $(MAIN_JS:.js=.d.ts) $(MAIN_JS:.js=.d.ts.map) $(MAIN_JS:.js=.js.map)
	rm -f $(SETTINGS_JS) $(SETTINGS_JS:.js=.d.ts) $(SETTINGS_JS:.js=.d.ts.map) $(SETTINGS_JS:.js=.js.map)
	echo "Cleaned compiled outputs"

# Clean everything including node_modules
clean-all: clean
	echo "Removing node_modules directory..."
	rm -rf $(NODE_MODULES) ts/config/package-lock.json
	echo "Full clean complete."

# Remove node_modules but keep compiled JavaScript files (for distribution)
dist-clean:
	echo "Removing node_modules directory..."
	rm -rf $(NODE_MODULES) ts/config/package-lock.json
	echo "Distribution clean complete (JavaScript files preserved)."

# Install and build everything
all: build
	echo "Setup complete!"

# Check if compiled files exist
check-exists:
	@status=0; \
	if [ -f $(MAIN_JS) ] && [ -f $(SETTINGS_JS) ]; then \
		echo "✓ Both output files exist"; \
	elif [ -f $(MAIN_JS) ]; then \
		echo "✗ Partially compiled: $(MAIN_JS) exists but $(SETTINGS_JS) is missing"; \
		status=1; \
	elif [ -f $(SETTINGS_JS) ]; then \
		echo "✗ Partially compiled: $(SETTINGS_JS) exists but $(MAIN_JS) is missing"; \
		status=1; \
	else \
		echo "✗ Not compiled: both $(MAIN_JS) and $(SETTINGS_JS) are missing"; \
		status=1; \
	fi; \
	exit $$status

# Check if compiled files are up to date (newer than sources)
check-timestamps:
	@check_timestamp() { \
		name=$$1; src_file=$$2; output_file=$$3; config_file=$$4; \
		if [ ! -f "$$output_file" ]; then \
			echo "✗ $$name is missing"; \
			return 1; \
		elif [ "$$src_file" -nt "$$output_file" ] || \
		     [ $(DRUPAL_DEFS) -nt "$$output_file" ] || \
		     [ $(TSCONFIG_BASE) -nt "$$output_file" ] || \
		     [ "$$config_file" -nt "$$output_file" ]; then \
			echo "✗ $$name is outdated (sources or config modified)"; \
			return 1; \
		else \
			echo "✓ $$name is up to date"; \
			return 0; \
		fi; \
	}; \
	status=0; \
	check_timestamp $(MAIN_CHECK) || status=1; \
	check_timestamp $(SETTINGS_CHECK) || status=1; \
	exit $$status

# Combined check: existence and timestamps
check: check-exists check-timestamps
	@echo ""
	@echo "✓ All checks passed - outputs exist and are up to date"
