# ============================================================
# lokistack - Makefile
# Convenient shortcuts for common operations
# ============================================================

.PHONY: help build start stop status logs clean prod-setup secrets

# Default target
help:
	@echo "lokistack - Available Commands:"
	@echo ""
	@echo "  make build       - Build Docker image"
	@echo "  make start       - Start with docker-compose (dev)"
	@echo "  make stop        - Stop all services"
	@echo "  make status      - Show container status and health"
	@echo "  make logs        - Follow container logs"
	@echo "  make prod        - Start production deployment"
	@echo "  make prod-stop   - Stop production deployment"
	@echo "  make secrets     - Generate secret files"
	@echo "  make clean       - Remove containers, volumes, and images"
	@echo "  make shell       - Open shell in running container"
	@echo "  make lint        - Validate Docker and Compose files"
	@echo ""

# Build Docker image
build:
	@echo "Building lokistack image..."
	@docker compose build --no-cache

# Development
start:
	@echo "Starting lokistack (dev mode)..."
	@echo ""
	@if [ -z "$${JWT_SECRET:-}" ]; then \
		echo "WARNING: JWT_SECRET not set!"; \
		echo "Run: export JWT_SECRET=\$$(openssl rand -hex 32)"; \
		echo "     export ENCRYPTION_KEY=\$$(openssl rand -hex 32)"; \
		echo ""; \
		read -p "Continue without secrets? (y/N) " ans; \
		[ "$$ans" != "y" ] && [ "$$ans" != "Y" ] && exit 1; \
	fi
	@docker compose up -d
	@echo ""
	@echo "lokistack started!"
	@echo "  URL: http://localhost:$${SERVER_PORT:-8080}"
	@echo "  Check status: make status"

stop:
	@echo "Stopping lokistack..."
	@docker compose down --timeout 15

status:
	@echo "=== Container Status ==="
	@docker compose ps
	@echo ""
	@echo "=== Health Checks ==="
	@docker inspect --format='{{.Name}}: {{.State.Health.Status}}' lokistack 2>/dev/null || echo "lokistack: container not found"
	@echo ""
	@echo "=== Resource Usage ==="
	@docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.NetIO}}\t{{.BlockIO}}" lokistack 2>/dev/null || true

logs:
	@docker compose logs -f --tail=50

# Production
prod-setup: secrets
	@echo "Setting up production environment..."
	@mkdir -p secrets
	@echo "Production secrets created in ./secrets/"

prod:
	@echo "Starting lokistack (production mode)..."
	@if [ ! -f secrets/jwt_secret.txt ] || [ ! -f secrets/encryption_key.txt ]; then \
		echo "ERROR: Secrets not found. Run 'make secrets' first."; \
		exit 1; \
	fi
	@docker compose -f docker-compose.prod.yml up -d
	@echo "Production deployment started!"
	@echo "  Load Balancer: http://localhost:$${SERVER_PORT:-8080}"
	@echo "  Check status: docker compose -f docker-compose.prod.yml ps"

prod-stop:
	@echo "Stopping production deployment..."
	@docker compose -f docker-compose.prod.yml down --timeout 30

# Secrets generation
secrets:
	@echo "Generating secret files..."
	@mkdir -p secrets
	@chmod 700 secrets
	@openssl rand -hex 32 > secrets/jwt_secret.txt
	@openssl rand -hex 32 > secrets/encryption_key.txt
	@chmod 600 secrets/*.txt
	@echo "Secrets created:"
	@echo "  - secrets/jwt_secret.txt"
	@echo "  - secrets/encryption_key.txt"
	@echo ""
	@echo "IMPORTANT: Keep these files secure and never commit them!"

# Maintenance
shell:
	@docker compose exec lokistack /bin/sh

clean:
	@echo "Cleaning up..."
	@docker compose down --volumes --rmi local --timeout 15 2>/dev/null || true
	@docker compose -f docker-compose.prod.yml down --volumes --rmi local --timeout 30 2>/dev/null || true
	@echo "Cleanup complete."

# Linting
dist:
	APP_VERSION=$$(git describe --tags --abbrev=0 2>/dev/null || echo dev) bash build.sh --dist

lint:
	@echo "Validating Dockerfile..."
	@docker run --rm -i hadolint/hadolint < Dockerfile 2>/dev/null || echo "hadolint not available, install from: https://github.com/hadolint/hadolint"
	@echo ""
	@echo "Validating docker-compose.yml..."
	@docker compose config > /dev/null && echo "docker-compose.yml: OK"
	@echo ""
	@echo "Validating docker-compose.prod.yml..."
	@docker compose -f docker-compose.prod.yml config > /dev/null && echo "docker-compose.prod.yml: OK"
	@echo ""
	@echo "Checking shell scripts..."
	@bash -n run.sh && echo "run.sh: syntax OK"
	@bash -n build.sh && echo "build.sh: syntax OK"
	@bash -n docker-entrypoint.sh && echo "docker-entrypoint.sh: syntax OK"
