# Makefile for Ory Stack

# 환경 변수 로드
ifneq (,$(wildcard ./.env))
    include .env
    export
endif

# --- 기본 실행 (All Apps) ---
# DB 상태 체크 후 모든 App 서비스 실행
up: check-db
	@echo "Starting ALL Ory services (Profile: app)..."
	docker compose --profile app up -d

# --- 개별 서비스 실행 ---
# Kratos만 실행
up-kratos: check-db
	@echo "Starting Ory Kratos..."
	docker compose --profile kratos up -d

# Hydra만 실행
up-hydra: check-db
	@echo "Starting Ory Hydra..."
	docker compose --profile hydra up -d

# Keto만 실행
up-keto: check-db
	@echo "Starting Ory Keto..."
	docker compose --profile keto up -d

# --- 인프라 (DB) 실행 ---
# PostgreSQL 실행
up-infra:
	@echo "Starting Infrastructure (PostgreSQL)..."
	docker compose --profile infra up -d

# --- 종료 (Down) ---
# 모든 서비스 및 인프라 종료
down:
	@echo "Stopping ALL services (Infra + App)..."
	docker compose --profile infra --profile app down

# App 서비스만 종료 (DB는 유지)
down-app:
	@echo "Stopping App services..."
	docker compose --profile app down

# 인프라만 종료 (주의: App 서비스 에러 가능성 있음)
down-infra:
	@echo "Stopping Infrastructure..."
	docker compose --profile infra down

# --- 유틸리티 ---
# DB 상태 확인 로직
check-db:
	@echo "Checking database status..."
	@if [ "$$(docker inspect -f '{{.State.Health.Status}}' ory-postgres 2>/dev/null)" != "healthy" ]; then \
			echo "Error: Database is not running or not healthy."; \
			echo "Please run 'make up-infra' first."; \
			exit 1; \
	else \
			echo "Database is healthy."; \
	fi

# 로그 확인
logs:
	docker compose -f compose.ory.yaml logs -f
