.PHONY: help setup db-up db-down migrate bootstrap dev shell clean

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

setup: db-up migrate bootstrap ## Complete setup: start database, run migrations, and bootstrap data
	@echo "✓ Setup complete! You do not need to run this again; Run 'make dev' to start the development server."

db-up: ## Start PostgreSQL database using Docker Compose
	@echo "Starting PostgreSQL database..."
	@docker compose -f docker-compose.dev.yml up -d
	@echo "Waiting for database to be ready..."
	@sleep 3
	@echo "✓ Database is running"

db-down: ## Stop PostgreSQL database
	@echo "Stopping PostgreSQL database..."
	@docker compose down

migrate: ## Run database migrations
	@echo "Running migrations..."
	@uv run python manage.py migrate
	@echo "✓ Migrations complete"

bootstrap: ## Bootstrap initial data (anonymous user, packages, models)
	@echo "Bootstrapping initial data..."
	@echo "This will take a bit ⏱️. Get yourself some coffe..."
	@uv run python manage.py bootstrap
	@echo "✓ Bootstrap complete"
	@echo ""
	@echo "Default admin credentials:"
	@echo "  Username: admin"
	@echo "  Email: admin@envipath.com"
	@echo "  Password: SuperSafe"

dev: db-up
	@uv run python manage.py runserver

shell: ## Open Django shell
	@uv run python manage.py shell

clean: ## Remove database and volumes (WARNING: destroys all data)
	@echo "WARNING: This will delete all database data!"
	@read -p "Are you sure? [y/N] " -n 1 -r; \
	echo; \
	if [[ $$REPLY =~ ^[Yy]$$ ]]; then \
		docker compose down -v; \
		echo "✓ Cleaned"; \
	else \
		echo "Cancelled"; \
	fi
