# Makefile for ai.unturf.com project
# All commands used for testing, validation, and development

.PHONY: help format check test validate-exports validate-all clean install dev build deploy languages-list languages-build-% languages-test-% languages-clean

# Environment variables for testing language implementations
MODEL_ENDPOINT_1 ?= https://hermes.ai.unturf.com/v1
MODEL_ENDPOINT_2 ?= https://qwen.ai.unturf.com/v1
TTS_ENDPOINT_1 ?= https://speech.ai.unturf.com/v1
DOCKER_ENV_VARS := -e MODEL_ENDPOINT_1=$(MODEL_ENDPOINT_1) -e MODEL_ENDPOINT_2=$(MODEL_ENDPOINT_2) -e TTS_ENDPOINT_1=$(TTS_ENDPOINT_1)

# Default target
help:
	@echo "Available commands:"
	@echo "  make format           - Format all files with biome"
	@echo "  make check            - Check syntax and linting with biome"
	@echo "  make format-check     - Format and check in sequence"
	@echo "  make test             - Run integration tests"
	@echo "  make test-simple      - Run simple unit-style tests"
	@echo "  make test-modal       - Run custom modal tests"
	@echo "  make validate-exports - Validate import/export consistency"
	@echo "  make verify-translations - Verify translation completeness"
	@echo "  make validate-translations - Validate all translation files"
	@echo "  make validate-all     - Run all validation checks"
	@echo "  make clean            - Clean temporary files"
	@echo "  make install          - Install dependencies"
	@echo "  make dev              - Start development server"
	@echo "  make build            - Build for production"
	@echo "  make deploy           - Deploy to production"
	@echo "  make git-status       - Show git status"
	@echo "  make git-add          - Stage all changes"
	@echo "  make git-commit       - Commit with biome formatting"
	@echo "  make git-push         - Push to remote"
	@echo ""
	@echo "Language Examples:"
	@echo "  make languages-list           - List all language directories"
	@echo "  make languages-build-<lang>   - Build Docker image for language"
	@echo "  make languages-test-<lang>    - Test language implementation with endpoints"
	@echo "  make languages-clean          - Stop and remove language test containers"

# Code formatting and linting
format:
	npx biome format --write .

check:
	npx biome check .

format-check: format check

# Testing and validation
test:
	@echo "Running integration tests..."
	@if command -v node >/dev/null 2>&1; then \
		if node -e "import('jsdom')" 2>/dev/null; then \
			node integration_tests.js; \
		else \
			echo "⚠️  JSDOM not available - running simple tests instead"; \
			node simple_tests.js; \
		fi; \
	else \
		echo "❌ Node.js not available"; \
	fi

test-simple:
	node simple_tests.js

validate-exports:
	@echo "Running export validation..."
	@if command -v node >/dev/null 2>&1; then \
		node validate_exports.js; \
	else \
		echo "❌ Node.js not available"; \
	fi

test-modal:
	@echo "Running custom modal tests..."
	@if command -v node >/dev/null 2>&1; then \
		if node -e "import('jsdom')" 2>/dev/null; then \
			node modal_tests.js; \
		else \
			echo "⚠️  JSDOM not available - skipping modal tests (install jsdom for full modal testing)"; \
		fi; \
	else \
		echo "❌ Node.js not available"; \
	fi

validate-all: format-check validate-exports verify-translations test test-simple test-modal

# Development workflow
install:
	npm install

dev:
	python3 -m http.server 8000

build: format-check validate-all
	@echo "Build complete - all checks passed"

# Git workflow
git-status:
	git status

git-add:
	git add -A

git-commit: format-check
	git add -A
	@echo "Ready to commit - run 'git commit -m \"your message\"'"

git-push:
	git push

# Deployment
deploy: build git-add
	@echo "Ready to deploy - review changes and commit"

# Cleanup
clean:
	rm -f .biome-cache
	rm -f src/.*.swp
	rm -f .DS_Store
	find . -name "*.log" -delete
	find . -name "node_modules" -type d -exec rm -rf {} +

# Language validation
validate-languages:
	@echo "Validating language files..."
	@for file in src/languages/*.js; do \
		if [ "$$(basename $$file)" != "add-missing-keys.js" ] && [ "$$(basename $$file)" != "verify-translations.js" ]; then \
			echo "Checking $$file..."; \
			node -e "import('./$$file').then(m => console.log('✅', '$$file', 'exports:', Object.keys(m))).catch(e => console.error('❌', '$$file', 'error:', e.message))"; \
		fi \
	done

verify-translations:
	@echo "Verifying translation completeness..."
	@node src/languages/verify-translations.js

validate-translations: verify-translations validate-languages

# File structure validation
validate-structure:
	@echo "Validating project structure..."
	@test -f src/ui.js || echo "❌ Missing src/ui.js"
	@test -f src/translation.js || echo "❌ Missing src/translation.js"
	@test -f src/tts.js || echo "❌ Missing src/tts.js"
	@test -f src/chat.js || echo "❌ Missing src/chat.js"
	@test -f src/config.js || echo "❌ Missing src/config.js"
	@test -d src/languages || echo "❌ Missing src/languages directory"
	@echo "✅ Core files exist"

# Module size check
check-sizes:
	@echo "Module sizes:"
	@wc -l src/*.js | sort -n
	@echo ""
	@echo "Language files:"
	@wc -l src/languages/*.js | sort -n

# Production checks
prod-check: format-check validate-exports validate-structure
	@echo "Production readiness check complete"

# Development setup
setup: install
	@echo "Development environment setup complete"
	@echo "Run 'make dev' to start development server"

# Quick development cycle
quick: format-check git-add
	@echo "Quick validation complete - ready to commit"

# Full CI/CD cycle
ci: clean format-check validate-all validate-structure validate-translations check-sizes
	@echo "CI pipeline complete"

# Language Examples Commands
languages-list:
	@echo "Available language implementations:"
	@ls -d languages/*/ 2>/dev/null | sed 's|languages/||g' | sed 's|/||g' || echo "No language directories found"

languages-build-%:
	@echo "Building Docker image for $*..."
	@if [ -d "languages/$*" ]; then \
		docker build -t ai-unturf-$* languages/$*/; \
	else \
		echo "❌ Language directory languages/$* not found"; \
		exit 1; \
	fi

languages-test-%:
	@echo "Testing $* implementation with official endpoints..."
	@echo "Environment: MODEL_ENDPOINT_1=$(MODEL_ENDPOINT_1)"
	@echo "Environment: MODEL_ENDPOINT_2=$(MODEL_ENDPOINT_2)"
	@echo "Environment: TTS_ENDPOINT_1=$(TTS_ENDPOINT_1)"
	@if [ -d "languages/$*" ]; then \
		echo "Starting container..."; \
		docker run -d --name test-$* $(DOCKER_ENV_VARS) ai-unturf-$* && \
		sleep 3 && \
		echo "Container logs:" && \
		docker logs test-$* && \
		echo "" && \
		echo "✅ Container started - check logs above for model discovery" && \
		echo "To stop: docker stop test-$* && docker rm test-$*"; \
	else \
		echo "❌ Language directory languages/$* not found"; \
		exit 1; \
	fi

languages-clean:
	@echo "Stopping and removing language test containers..."
	@docker ps -a | grep test- | awk '{print $$1}' | xargs -r docker stop 2>/dev/null || true
	@docker ps -a | grep test- | awk '{print $$1}' | xargs -r docker rm 2>/dev/null || true
	@echo "✅ Containers cleaned (images preserved for reuse)"
	@echo "To remove images: docker images | grep ai-unturf- | awk '{print \$$3}' | xargs docker rmi"