# Baron SSO용 Docker Compose 헬퍼

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

# Compose 파일 경로
COMPOSE_INFRA := compose.infra.yaml
COMPOSE_ORY := compose.ory.yaml
COMPOSE_APP := docker-compose.yaml
AUTH_CONFIG_ENV := .generated/auth-config.env

COMPOSE_CLI_ENV_ARGS :=
ifneq (,$(wildcard ./.env))
COMPOSE_CLI_ENV_ARGS += --env-file .env
endif
COMPOSE_CLI_ENV_ARGS += --env-file $(AUTH_CONFIG_ENV)

# --- 인증 설정 빌드/검증 ---
build-auth-config:
	@echo "Building auth config..."
	@mkdir -p .generated
	@bash scripts/auth_config.sh build

validate-auth-config: build-auth-config
	@echo "Validating auth config..."
	@bash scripts/auth_config.sh validate

verify-auth-config: validate-auth-config
	@echo "Verifying auth config wiring..."
	@bash scripts/auth_config.sh verify

# --- 기본 실행 ---
# 주의: --remove-orphan 사용 금지 (다른 스택이 orphan으로 판단되어 종료될 수 있음)
up-all: validate-auth-config
	@echo "Starting ALL stacks (infra + ory + app)..."
	docker compose $(COMPOSE_CLI_ENV_ARGS) -f $(COMPOSE_INFRA) -f $(COMPOSE_ORY) -f $(COMPOSE_APP) up -d

# --- 개별 스택 실행 ---
up-infra:
	@echo "Starting Infra stack (postgres/clickhouse/redis)..."
	docker compose -f $(COMPOSE_INFRA) up -d

up-ory: validate-auth-config
	@echo "Starting Ory stack (kratos/hydra/keto/oathkeeper)..."
	docker compose $(COMPOSE_CLI_ENV_ARGS) -f $(COMPOSE_ORY) up -d

up-app: validate-auth-config
	@echo "Starting App stack (backend/userfront/adminfront/devfront)..."
	docker compose $(COMPOSE_CLI_ENV_ARGS) -f $(COMPOSE_APP) up -d

up-backend: validate-auth-config
	@echo "Starting Backend only..."
	docker compose $(COMPOSE_CLI_ENV_ARGS) -f $(COMPOSE_APP) up -d backend

up-dev: up-infra up-ory
	@echo "Dev stack is up (infra + ory)."

up-front-dev: up-infra up-ory up-backend
	@echo "Dev stack is up (infra + ory + backend)."

# --- 종료 (Down) ---
down-all:
	@echo "Stopping ALL stacks (infra + ory + app)..."
	docker compose -f $(COMPOSE_INFRA) -f $(COMPOSE_ORY) -f $(COMPOSE_APP) down

down-app:
	@echo "Stopping App stack..."
	docker compose -f $(COMPOSE_APP) down

down-backend:
	@echo "Stopping Backend only..."
	docker compose -f $(COMPOSE_APP) stop backend

down-infra:
	@echo "Stopping Infra stack..."
	docker compose -f $(COMPOSE_INFRA) down

down-ory:
	@echo "Stopping Ory stack..."
	docker compose -f $(COMPOSE_ORY) down

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

ps:
	docker compose -f $(COMPOSE_INFRA) -f $(COMPOSE_ORY) -f $(COMPOSE_APP) ps

logs-infra:
	docker compose -f $(COMPOSE_INFRA) logs -f

logs-ory:
	docker compose -f $(COMPOSE_ORY) logs -f

logs-app:
	docker compose -f $(COMPOSE_APP) logs -f

# --- 로컬 통합 코드 체크 ---
.PHONY: code-check code-check-i18n code-check-go-lint code-check-userfront-lint code-check-front-lint code-check-backend-tests code-check-userfront-tests code-check-adminfront-tests code-check-devfront-tests

code-check: code-check-i18n code-check-go-lint code-check-userfront-lint code-check-front-lint code-check-backend-tests code-check-userfront-tests code-check-adminfront-tests code-check-devfront-tests
	@echo "code-check complete."

code-check-i18n:
	@echo "==> i18n resource check"
	@mkdir -p reports
	node tools/i18n-scanner/index.js
	node tools/i18n-scanner/report.js
	@cat reports/i18n-report.txt

code-check-go-lint:
	@echo "==> go lint/format check"
	@if command -v golangci-lint >/dev/null 2>&1; then \
		cd backend && golangci-lint run --enable-only=gofmt,gofumpt; \
	else \
		echo "WARN: golangci-lint not found, fallback to gofmt check only."; \
		unformatted="$$(cd backend && gofmt -l .)"; \
		if [ -n "$$unformatted" ]; then \
			echo "gofmt required:"; \
			echo "$$unformatted"; \
			exit 1; \
		fi; \
	fi

code-check-userfront-lint:
	@echo "==> userfront format/analyze"
	cd userfront && flutter pub get
	cd userfront && dart format --output=show --set-exit-if-changed lib test
	cd userfront && flutter analyze --no-fatal-warnings --no-fatal-infos

code-check-front-lint:
	@echo "==> adminfront biome lint/format check"
	cd adminfront && npm ci
	cd adminfront && npx biome check src tests playwright.config.ts --formatter-enabled=false --organize-imports-enabled=false
	cd adminfront && npx biome check src tests playwright.config.ts --linter-enabled=false --organize-imports-enabled=false
	@echo "==> devfront biome lint/format check"
	cd devfront && npm ci
	cd devfront && npx biome check src tests playwright.config.ts --formatter-enabled=false --organize-imports-enabled=false
	cd devfront && npx biome check src tests playwright.config.ts --linter-enabled=false --organize-imports-enabled=false

code-check-backend-tests:
	@echo "==> backend tests"
	cd backend && go test -v ./...

code-check-userfront-tests:
	@echo "==> userfront tests"
	cd userfront && flutter test

code-check-adminfront-tests:
	@echo "==> adminfront tests"
	@mkdir -p reports/adminfront
	@rm -rf reports/adminfront/playwright-report reports/adminfront/test-results
	@status=0; \
	(cd adminfront && npx playwright install) || status=$$?; \
	if [ $$status -eq 0 ]; then \
		(cd adminfront && npm test) || status=$$?; \
	fi; \
	[ -d adminfront/playwright-report ] && cp -R adminfront/playwright-report reports/adminfront/ || true; \
	[ -d adminfront/test-results ] && cp -R adminfront/test-results reports/adminfront/ || true; \
	exit $$status

code-check-devfront-tests:
	@echo "==> devfront tests"
	@mkdir -p reports/devfront
	@rm -rf reports/devfront/playwright-report reports/devfront/test-results
	@status=0; \
	(cd devfront && npx playwright install) || status=$$?; \
	if [ $$status -eq 0 ]; then \
		(cd devfront && npm test) || status=$$?; \
	fi; \
	[ -d devfront/playwright-report ] && cp -R devfront/playwright-report reports/devfront/ || true; \
	[ -d devfront/test-results ] && cp -R devfront/test-results reports/devfront/ || true; \
	exit $$status
