.PHONY: help dev prod stop logs logs-dev logs-prod rebuild rebuild-dev clean install test migrate

# Default target
help:
	@echo "Feuerwehr Dashboard - Available Commands"
	@echo ""
	@echo "Development:"
	@echo "  make dev          - Start local development database"
	@echo "  make logs-dev     - Show development logs"
	@echo "  make rebuild-dev  - Rebuild development services"
	@echo ""
	@echo "Production:"
	@echo "  make prod         - Deploy production environment (includes migrations)"
	@echo "  make migrate      - Run database migrations against production DB"
	@echo "  make logs-prod    - Show production logs"
	@echo "  make rebuild      - Rebuild production services"
	@echo ""
	@echo "General:"
	@echo "  make stop         - Stop all services"
	@echo "  make clean        - Remove all containers and volumes"
	@echo "  make install      - Install dependencies for backend, frontend and sync"
	@echo "  make test         - Run tests"
	@echo ""

# Development
dev:
	@echo "Starting local development database..."
	docker compose -f docker-compose.dev.yml up -d
	@echo ""
	@echo "Database is ready at localhost:5432"
	@echo "Database: feuerwehr_dev"
	@echo "User: dev_user"
	@echo "Password: dev_password"

logs-dev:
	docker compose -f docker-compose.dev.yml logs -f

rebuild-dev:
	docker compose -f docker-compose.dev.yml up -d --build --force-recreate

# Production
prod:
	@if [ ! -f .env ]; then \
		echo "Error: .env file not found!"; \
		echo "Please copy .env.example to .env and configure it:"; \
		echo "  cp .env.example .env"; \
		exit 1; \
	fi
	@echo "Starting production deployment..."
	docker compose -f docker-compose.yml up -d --build
	@echo ""
	@echo "Running database migrations..."
	@$(MAKE) migrate
	@echo ""
	@echo "Production services are running!"

migrate:
	@if [ ! -f .env ]; then \
		echo "Error: .env file not found! Run 'cp .env.example .env' first."; \
		exit 1; \
	fi
	@echo "Waiting for database to be ready..."
	@until docker compose -f docker-compose.yml exec -T postgres \
		pg_isready -U "$${POSTGRES_USER:-prod_user}" -d "$${POSTGRES_DB:-feuerwehr_prod}" \
		> /dev/null 2>&1; do \
		printf '.'; sleep 2; \
	done
	@echo ""
	@echo "Running migrations..."
	docker compose -f docker-compose.yml exec -T backend npm run migrate
	@echo "Migrations complete!"

logs-prod:
	docker compose -f docker-compose.yml logs -f

logs:
	@$(MAKE) logs-prod

rebuild:
	docker compose -f docker-compose.yml up -d --build --force-recreate

# General commands
stop:
	@echo "Stopping all services..."
	docker compose -f docker-compose.yml down 2>/dev/null || true
	docker compose -f docker-compose.dev.yml down 2>/dev/null || true
	@echo "All services stopped"

clean:
	@echo "Warning: This will remove all containers, volumes, and images!"
	@read -p "Are you sure? [y/N] " -n 1 -r; \
	echo; \
	if [ "$$REPLY" = "y" ] || [ "$$REPLY" = "Y" ]; then \
		docker compose -f docker-compose.yml down -v 2>/dev/null || true; \
		docker compose -f docker-compose.dev.yml down -v 2>/dev/null || true; \
		docker images | grep feuerwehr | awk '{print $$3}' | xargs docker rmi -f 2>/dev/null || true; \
		echo "Cleanup complete!"; \
	else \
		echo "Cleanup cancelled"; \
	fi

# Install dependencies
install:
	@echo "Installing backend dependencies..."
	cd backend && npm install
	@echo ""
	@echo "Installing frontend dependencies..."
	cd frontend && npm install
	@echo ""
	@echo "Installing sync dependencies..."
	cd sync && npm install
	@echo ""
	@echo "Dependencies installed!"

# Run tests
test:
	@echo "Running backend tests..."
	cd backend && npm test
	@echo ""
	@echo "Running frontend tests..."
	cd frontend && npm test
