.PHONY: help build up down restart logs ps clean test

# Default target
help:
	@echo "License Server Management Commands:"
	@echo "  make build    - Build all Docker images"
	@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 ps       - List running containers"
	@echo "  make clean    - Remove containers and volumes"
	@echo "  make test     - Run tests"
	@echo "  make init-db  - Initialize database schema"

# Build all Docker images
build:
	docker-compose build

# Start all services
up:
	docker-compose up -d
	@echo "Waiting for services to be healthy..."
	@sleep 10
	@echo "Services are running!"
	@echo "Auth Service: http://localhost:5001"
	@echo "License API: http://localhost:5002"
	@echo "Analytics: http://localhost:5003"
	@echo "Admin API: http://localhost:5004"
	@echo "RabbitMQ Management: http://localhost:15672"

# Stop all services
down:
	docker-compose down

# Restart all services
restart: down up

# View logs
logs:
	docker-compose logs -f

# List containers
ps:
	docker-compose ps

# Clean up everything
clean:
	docker-compose down -v
	docker system prune -f

# Run tests
test:
	@echo "Running API tests..."
	@python tests/test_api.py

# Initialize database
init-db:
	@echo "Initializing database schema..."
	docker-compose exec postgres psql -U license_admin -d licenses -f /docker-entrypoint-initdb.d/init.sql

# Service-specific commands
logs-auth:
	docker-compose logs -f auth_service

logs-license:
	docker-compose logs -f license_api

logs-analytics:
	docker-compose logs -f analytics_service

logs-admin:
	docker-compose logs -f admin_api

# Development commands
dev:
	docker-compose -f docker-compose.yml -f docker-compose.dev.yml up

shell-auth:
	docker-compose exec auth_service /bin/bash

shell-license:
	docker-compose exec license_api /bin/bash

shell-db:
	docker-compose exec postgres psql -U license_admin -d licenses