.PHONY: help up down restart logs build clean ps shell-backend shell-frontend shell-db migrate seed studio

# Default target
help:
	@echo "GPSF-MIS Docker Management"
	@echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
	@echo "Available commands:"
	@echo "  make up          - Start all services"
	@echo "  make down        - Stop all services"
	@echo "  make restart     - Restart all services"
	@echo "  make logs        - View logs from all services"
	@echo "  make build       - Rebuild and start services"
	@echo "  make clean       - Stop services and remove volumes"
	@echo "  make ps          - Show running containers"
	@echo ""
	@echo "Database commands:"
	@echo "  make migrate     - Run database migrations"
	@echo "  make seed        - Seed the database"
	@echo "  make studio      - Open Prisma Studio"
	@echo ""
	@echo "Shell access:"
	@echo "  make shell-backend   - Access backend container shell"
	@echo "  make shell-frontend  - Access frontend container shell"
	@echo "  make shell-db        - Access PostgreSQL shell"
	@echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# Start all services
up:
	@echo "🚀 Starting all services..."
	@docker-compose up -d
	@echo "✅ Services started!"
	@echo "   Frontend:      http://localhost:3001"
	@echo "   Backend:       http://localhost:3002"
	@echo "   Prisma Studio: http://localhost:5555"
	@echo "   Health:        http://localhost:3002/api/v1/health"

# Stop all services
down:
	@echo "🛑 Stopping all services..."
	@docker-compose down
	@echo "✅ Services stopped!"

# Restart all services
restart:
	@echo "🔄 Restarting all services..."
	@docker-compose restart
	@echo "✅ Services restarted!"

# View logs
logs:
	@docker-compose logs -f

# Rebuild and start
build:
	@echo "🔨 Rebuilding services..."
	@docker-compose up -d --build
	@echo "✅ Services rebuilt and started!"

# Clean everything (including volumes)
clean:
	@echo "🧹 Cleaning up (this will delete all data)..."
	@read -p "Are you sure? [y/N] " -n 1 -r; \
	echo; \
	if [[ $$REPLY =~ ^[Yy]$$ ]]; then \
		docker-compose down -v; \
		echo "✅ Cleanup complete!"; \
	else \
		echo "❌ Cleanup cancelled"; \
	fi

# Show container status
ps:
	@docker-compose ps

# Run migrations
migrate:
	@echo "🔄 Running database migrations..."
	@docker exec -it gpsf-mis-backend npx prisma migrate deploy
	@echo "✅ Migrations complete!"

# Seed database
seed:
	@echo "🌱 Seeding database..."
	@docker exec -it gpsf-mis-backend npm run prisma:seed
	@echo "✅ Seeding complete!"

# Open Prisma Studio
studio:
	@echo "📊 Prisma Studio is running at: http://localhost:5555"
	@echo "💡 It starts automatically with docker-compose up"
	@open http://localhost:5555 2>/dev/null || echo "Open http://localhost:5555 in your browser"

# Shell access
shell-backend:
	@docker exec -it gpsf-mis-backend sh

shell-frontend:
	@docker exec -it gpsf-mis-frontend sh

shell-db:
	@docker exec -it gpsf-mis-postgres psql -U khonvakhim -d GPSF_MIS_DB
