# Makefile: 適用於 docker-compose 專案，自動取用當前資料夾為專案名

COMPOSE := docker-compose
COMPOSE_FILE := docker-compose.yml
PROJECT_NAME := $(notdir $(CURDIR))  # 依照當前資料夾自動命名

# 可選自訂 .env 變數（預設 docker-compose 自動載入）
# ENV_FILE := .env

.PHONY: help up down build restart logs ps exec clean prune

help:
	@echo "🔧 Docker Compose Makefile 指令："
	@echo "  make up         - 啟動所有服務 (背景執行)"
	@echo "  make up-fg      - 前景執行（方便除錯）"
	@echo "  make down       - 停止並移除服務容器"
	@echo "  make build      - 重新建構服務映像檔"
	@echo "  make restart    - 重啟所有服務"
	@echo "  make logs       - 查看服務日誌"
	@echo "  make ps         - 顯示服務狀態"
	@echo "  make exec       - 進入主要容器 shell"
	@echo "  make clean      - 清理所有服務資料（含 volume）"
	@echo "  make prune      - 清除未使用的資源"

up:
	$(COMPOSE) -p $(PROJECT_NAME) -f $(COMPOSE_FILE) up -d

up-fg:
	$(COMPOSE) -p $(PROJECT_NAME) -f $(COMPOSE_FILE) up

down:
	$(COMPOSE) -p $(PROJECT_NAME) -f $(COMPOSE_FILE) down

build:
	$(COMPOSE) -p $(PROJECT_NAME) -f $(COMPOSE_FILE) build

restart:
	$(COMPOSE) -p $(PROJECT_NAME) -f $(COMPOSE_FILE) down
	$(COMPOSE) -p $(PROJECT_NAME) -f $(COMPOSE_FILE) up -d

logs:
	$(COMPOSE) -p $(PROJECT_NAME) -f $(COMPOSE_FILE) logs -f --tail=100

ps:
	$(COMPOSE) -p $(PROJECT_NAME) -f $(COMPOSE_FILE) ps

exec:
	$(COMPOSE) -p $(PROJECT_NAME) -f $(COMPOSE_FILE) exec app sh

clean:
	$(COMPOSE) -p $(PROJECT_NAME) -f $(COMPOSE_FILE) down -v --remove-orphans

prune:
	docker system prune -af --volumes

