# 巡樓 Haixun — 快速啟動與程序管理
# 需求：Node.js 20+、npm、pm2（npm i -g pm2）

SHELL := /bin/bash
ROOT := $(abspath $(dir $(lastword $(MAKEFILE_LIST))))
PORT ?= 3000

PM2 := $(shell command -v pm2 2>/dev/null)

.PHONY: help init db-init install build up start dev stop restart status logs down \
	playwright-setup playwright-deps save

help:
	@echo "巡樓 Haixun — 常用指令"
	@echo ""
	@echo "  初始化"
	@echo "    make init            首次設定（安裝依賴 + 建立 .env + 初始化 DB）"
	@echo "    make db-init         僅同步 Prisma schema 到資料庫"
	@echo "    make install         僅 npm install"
	@echo ""
	@echo "  啟動（PM2）"
	@echo "    make start           build 後以 PM2 啟動 web + worker（生產）"
	@echo "    make up              不 build，直接 PM2 啟動 web + worker"
	@echo "    make dev             PM2 啟動開發伺服器（next dev，不含 worker）"
	@echo "    make stop            停止所有 Haixun 程序"
	@echo "    make restart         重啟所有 Haixun 程序"
	@echo "    make down            停止並移除 PM2 程序"
	@echo "    make status          查看 PM2 狀態"
	@echo "    make logs            查看 PM2 日誌（web + worker）"
	@echo "    make save            儲存 PM2 程序列表（開機自動啟動用）"
	@echo ""
	@echo "  其他"
	@echo "    make build           next build"
	@echo "    make playwright-setup  安裝 Chromium 與 Playwright 依賴"
	@echo "    make playwright-deps   安裝 Playwright 系統函式庫（apt）"
	@echo ""
	@echo "  環境變數：PORT=$(PORT)（預設 3000）"

init:
	@bash "$(ROOT)/scripts/init.sh"

db-init:
	@test -f "$(ROOT)/.env" || (cp "$(ROOT)/.env.example" "$(ROOT)/.env" && echo "已建立 .env")
	@cd "$(ROOT)" && npm run db:generate && npm run db:push
	@echo "資料庫 schema 已同步"

install:
	@cd "$(ROOT)" && npm install

build:
	@cd "$(ROOT)" && npm run build

check-pm2:
ifndef PM2
	@echo "錯誤：找不到 pm2，請執行：npm install -g pm2" >&2
	@exit 1
endif

up: check-pm2
	@chmod +x "$(ROOT)/scripts/"pm2-*.sh "$(ROOT)/scripts/init.sh"
	@cd "$(ROOT)" && PORT=$(PORT) pm2 start ecosystem.config.cjs --only haixun-web,haixun-worker
	@echo "已啟動：haixun-web (http://localhost:$(PORT))、haixun-worker"
	@echo "查看狀態：make status"

start: build up

dev: check-pm2
	@chmod +x "$(ROOT)/scripts/"pm2-*.sh
	@cd "$(ROOT)" && PORT=$(PORT) pm2 start ecosystem.config.cjs --only haixun-web-dev
	@echo "開發伺服器：http://localhost:$(PORT)"
	@echo "查看日誌：pm2 logs haixun-web-dev"

stop: check-pm2
	@cd "$(ROOT)" && pm2 stop haixun-web haixun-worker haixun-web-dev 2>/dev/null || true
	@echo "已停止"

restart: check-pm2
	@cd "$(ROOT)" && pm2 restart haixun-web haixun-worker 2>/dev/null || $(MAKE) up
	@echo "已重啟"

down: check-pm2
	@cd "$(ROOT)" && pm2 delete haixun-web haixun-worker haixun-web-dev 2>/dev/null || true
	@echo "已移除 PM2 程序"

status: check-pm2
	@pm2 status haixun-web haixun-worker haixun-web-dev 2>/dev/null || pm2 status

logs: check-pm2
	@pm2 logs haixun-web haixun-worker --lines 100

save: check-pm2
	@pm2 save
	@echo "PM2 程序列表已儲存（搭配 pm2 startup 可開機自啟）"

playwright-deps:
	@echo "安裝 Playwright 系統函式庫..."
	@sudo apt-get install -y libatk1.0-0 libatk-bridge2.0-0t64 libcups2t64 \
		libdrm2 libdbus-1-3 libxkbcommon-x11-0 libxcomposite1 libxdamage1 \
		libxrandr2 libgbm1 libpango-1.0-0 libcairo2 libasound2t64
	@echo "Playwright 系統函式庫安裝完成"

playwright-setup:
	@cd "$(ROOT)" && npm run playwright:setup