# Makefile for Overseas Appointment System
# Usage: make <target>

.PHONY: help dev prod build start stop restart logs clean backup restore health

# Default target
help:
	@echo "Overseas Appointment System - Docker Commands"
	@echo ""
	@echo "Development:"
	@echo "  make dev          - Start development services (MySQL, Redis)"
	@echo "  make dev-stop     - Stop development services"
	@echo ""
	@echo "Production:"
	@echo "  make prod         - Start all production services"
	@echo "  make prod-build   - Build and start production services"
	@echo "  make stop         - Stop all services"
	@echo "  make restart      - Restart all services"
	@echo ""
	@echo "Maintenance:"
	@echo "  make logs         - View all service logs"
	@echo "  make logs-api     - View API logs"
	@echo "  make logs-mysql   - View MySQL logs"
	@echo "  make logs-redis   - View Redis logs"
	@echo "  make logs-nginx   - View Nginx logs"
	@echo ""
	@echo "Database:"
	@echo "  make backup       - Create database backup"
	@echo "  make migrate      - Run database migrations"
	@echo "  make seed         - Run database seeders"
	@echo ""
	@echo "Utilities:"
	@echo "  make health       - Check service health"
	@echo "  make clean        - Remove all containers and volumes"
	@echo "  make status       - Show service status"

# Development
dev:
	docker-compose -f docker-compose.dev.yml up -d
	@echo "Development services started!"
	@echo "MySQL: localhost:3306"
	@echo "Redis: localhost:6379"

dev-stop:
	docker-compose -f docker-compose.dev.yml down

# Production
prod:
	docker-compose up -d

prod-build:
	docker-compose up -d --build

stop:
	docker-compose down

restart:
	docker-compose restart

# Logs
logs:
	docker-compose logs -f

logs-api:
	docker-compose logs -f api

logs-mysql:
	docker-compose logs -f mysql

logs-redis:
	docker-compose logs -f redis

logs-nginx:
	docker-compose logs -f nginx

# Database operations
backup:
	@echo "Creating database backup..."
	docker-compose exec api sh -c 'mysqldump -h mysql -u $${DB_USER} -p$${DB_PASSWORD} $${DB_NAME} | gzip > /app/backups/backup_$$(date +%Y%m%d_%H%M%S).sql.gz'
	@echo "Backup completed!"

migrate:
	docker-compose exec api node src/migrations/001-create-tables.js

seed:
	docker-compose exec api node src/seeders/001-seed-categories.js
	docker-compose exec api node src/seeders/002-seed-admin.js

# Utilities
health:
	@curl -s http://localhost:3000/health | python3 -m json.tool 2>/dev/null || curl -s http://localhost:3000/health

status:
	docker-compose ps

clean:
	@echo "WARNING: This will remove all containers and volumes!"
	@read -p "Are you sure? (y/N): " confirm && [ "$$confirm" = "y" ] || exit 1
	docker-compose down -v --remove-orphans
	docker image prune -f

# Shell access
shell-api:
	docker-compose exec api sh

shell-mysql:
	docker-compose exec mysql mysql -u root -p

shell-redis:
	docker-compose exec redis redis-cli

# Scaling
scale-api:
	@read -p "Number of API instances: " count && docker-compose up -d --scale api=$$count
