# Makefile for Mawidi Docker Management
.PHONY: help build build-prod build-dev up down restart logs clean dev dev-watch prod shell test

# Default target
help:
	@echo "Mawidi Docker Management Commands:"
	@echo ""
	@echo "🚀 Development with Auto-Updates:"
	@echo "  make dev         - Start development with hot-reload"
	@echo "  make dev-watch   - Start with file watching for auto-rebuild"
	@echo "  make dev-build   - Rebuild development containers"
	@echo "  make dev-logs    - View development logs"
	@echo "  make dev-shell   - Open shell in dev container"
	@echo ""
	@echo "📦 Production:"
	@echo "  make prod        - Start production environment"
	@echo "  make build-prod  - Build production image only"
	@echo "  make prod-logs   - View production logs"
	@echo ""
	@echo "🛠️ General:"
	@echo "  make build       - Build all Docker images"
	@echo "  make up          - Start production containers"
	@echo "  make down        - Stop all containers"
	@echo "  make restart     - Restart all containers"
	@echo "  make clean       - Remove all containers and images"
	@echo "  make status      - Show container status"
	@echo "  make test        - Run tests in container"

# Build commands
build:
	docker-compose build --no-cache

build-prod:
	docker-compose build --no-cache mawidi-app

build-dev:
	docker-compose --profile dev build --no-cache mawidi-dev

# Container management
up:
	docker-compose up -d mawidi-app

down:
	docker-compose down

restart:
	docker-compose restart

logs:
	docker-compose logs -f --tail=100

clean:
	docker-compose down -v
	docker system prune -af

# Development with auto-reload (volumes mounted for hot-reload)
dev:
	@echo "🚀 Starting development with hot-reload..."
	docker-compose --profile dev up mawidi-dev

# Development with file watching for auto-rebuild
dev-watch:
	@echo "👁️ Starting development with auto-rebuild on file changes..."
	@./docker-watch.sh dev

dev-build:
	@echo "🔨 Rebuilding development containers..."
	docker-compose --profile dev build --no-cache mawidi-dev

dev-logs:
	docker-compose --profile dev logs -f mawidi-dev

dev-shell:
	docker exec -it mawidi-nextjs-dev sh

# Production commands
prod:
	docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d

prod-logs:
	docker-compose logs -f mawidi-app

# Utility commands
shell:
	docker exec -it mawidi-nextjs-prod sh

status:
	@echo "📊 Container Status:"
	@docker-compose ps
	@echo ""
	@echo "💾 Resource Usage:"
	@docker stats --no-stream --format "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}"

test:
	docker-compose run --rm mawidi-app npm test

# Production deployment
deploy:
	@echo "Building production image..."
	docker-compose -f docker-compose.yml -f docker-compose.prod.yml build
	@echo "Starting production services..."
	docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d
	@echo "Deployment complete!"

# Health check
health:
	@echo "Checking service health..."
	@curl -f http://localhost:9000/api/health || echo "Production service not healthy"
	@curl -f http://localhost:3000/api/health || echo "Development service not healthy"

# Database backup (if using database)
backup:
	@echo "Creating backup..."
	@mkdir -p backups
	@docker exec mawidi-redis redis-cli BGSAVE
	@echo "Backup complete!"

# Update dependencies
update-deps:
	docker-compose run --rm mawidi-dev npm update
	docker-compose run --rm mawidi-dev npm audit fix