# SIA Project Makefile
# ==================

# Default variables
PYTHON_DIRS = sia tools test
RUST_DIRS = lib/xml_schema_validator
WEB_DIRS = web

# Default target
.PHONY: all
all: help

# ==================
# Clean targets
# ==================

.PHONY: clean
clean: clean-python clean-rust clean-docker clean-iterations clean-web clean-build clean-temp ## Clean all project artifacts

.PHONY: clean-python
clean-python: ## Clean Python artifacts
	@echo "Cleaning Python artifacts..."
	find $(PYTHON_DIRS) -type d -name "__pycache__" -exec rm -rf {} +
	find $(PYTHON_DIRS) -type d -name "*.egg-info" -exec rm -rf {} +
	find $(PYTHON_DIRS) -type f -name "*.pyc" -delete
	find $(PYTHON_DIRS) -type f -name "*.pyo" -delete
	find $(PYTHON_DIRS) -type f -name "*.pyd" -delete
	find $(PYTHON_DIRS) -type f -name ".coverage" -delete
	find $(PYTHON_DIRS) -type d -name "htmlcov" -exec rm -rf {} +
	find $(PYTHON_DIRS) -type d -name ".pytest_cache" -exec rm -rf {} +
	find $(PYTHON_DIRS) -type d -name ".coverage*" -exec rm -rf {} +
	rm -rf .pytest_cache
	rm -rf .coverage
	rm -rf htmlcov
	rm -rf .tox
	rm -rf dist
	rm -rf build

.PHONY: clean-rust
clean-rust: ## Clean Rust artifacts
	@echo "Cleaning Rust artifacts..."
	for dir in $(RUST_DIRS); do \
		if [ -f "$$dir/Cargo.toml" ]; then \
			(cd "$$dir" && cargo clean); \
		fi; \
	done

.PHONY: clean-docker
clean-docker: ## Clean Docker artifacts
	@echo "Cleaning Docker artifacts..."
	-docker system prune -f

.PHONY: clean-iterations
clean-iterations: ## Clean iteration files (WARNING: data loss)
	@echo "WARNING: This will delete all iteration files in data/iterations"
	@echo "Press Ctrl+C to cancel or Enter to continue"
	@read
	rm -rf data/iterations/*.xml

.PHONY: clean-web
clean-web: ## Clean web build artifacts
	@echo "Cleaning web build artifacts..."
	rm -rf web/dist
	rm -rf web/node_modules
	rm -rf static

.PHONY: clean-build
clean-build: ## Clean build artifacts
	@echo "Cleaning build artifacts..."
	rm -rf build/
	rm -rf dist/
	rm -rf *.egg-info/

.PHONY: clean-temp
clean-temp: ## Clean temporary files
	@echo "Cleaning temporary files..."
	find . -type f -name "*.log" -delete
	find . -type f -name "*.tmp" -delete
	find . -type f -name ".DS_Store" -delete
	find . -type f -name "*.swp" -delete
	find . -type f -name "*~" -delete

# ==================
# Format targets
# ==================

.PHONY: format
format: format-python format-rust ## Format all code

.PHONY: format-python
format-python: ## Format Python code
	@echo "Formatting Python code..."
	black $(PYTHON_DIRS)

.PHONY: format-rust
format-rust: ## Format Rust code
	@echo "Formatting Rust code..."
	for dir in $(RUST_DIRS); do \
		if [ -f "$$dir/Cargo.toml" ]; then \
			(cd "$$dir" && cargo fmt); \
		fi; \
	done

# ==================
# Lint targets
# ==================

.PHONY: lint
lint: lint-python lint-rust ## Lint all code

.PHONY: lint-python
lint-python: ## Lint Python code
	@echo "Linting Python code..."
	flake8 $(PYTHON_DIRS)

.PHONY: lint-rust
lint-rust: ## Lint Rust code
	@echo "Linting Rust code..."
	for dir in $(RUST_DIRS); do \
		if [ -f "$$dir/Cargo.toml" ]; then \
			(cd "$$dir" && cargo clippy -- -D warnings); \
		fi; \
	done

# ==================
# Test targets
# ==================

.PHONY: test
test: test-python test-rust ## Run all tests

.PHONY: test-python
test-python: ## Run Python tests
	@echo "Running Python tests..."
	python -m pytest -xvs

.PHONY: test-rust
test-rust: ## Run Rust tests
	@echo "Running Rust tests..."
	for dir in $(RUST_DIRS); do \
		if [ -f "$$dir/Cargo.toml" ]; then \
			(cd "$$dir" && cargo test); \
		fi; \
	done

# ==================
# Build targets
# ==================

.PHONY: build
build: build-rust build-web ## Build all components

.PHONY: build-rust
build-rust: ## Build Rust components
	@echo "Building Rust components..."
	for dir in $(RUST_DIRS); do \
		if [ -f "$$dir/Cargo.toml" ]; then \
			(cd "$$dir" && cargo build --release); \
		fi; \
	done

.PHONY: build-web
build-web: ## Build web interface
	@echo "Building web interface..."
	cd web && npm install && npm run build

.PHONY: docker-build
docker-build: ## Build Docker image
	@echo "Building Docker image..."
	docker build -t sia .

# ==================
# Utility targets
# ==================

.PHONY: help
help: ## Show this help message
	@echo "Usage: make [target]"
	@echo ""
	@echo "Targets:"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2}'

.PHONY: collect
collect: ## Run collect.sh script
	@echo "Running collect.sh script..."
	./scripts/collect.sh -s core -s lib -s webui -o collect.txt