.PHONY: help setup setup-py setup-web db-reset seed demo dev dev-api dev-web dev-agent dev-agent-loop test test-py e2e clean

SHELL := /bin/bash
ROOT   := $(CURDIR)
PY     := "$(ROOT)/.venv/bin/python"
PIP    := "$(ROOT)/.venv/bin/pip"
CP     := $(ROOT)/services/control-plane
AGENT  := $(ROOT)/services/host-agent
CONSOLE := $(ROOT)/../前端UI八页面完成
export PYTHONPATH := $(CP):$(AGENT)

help:
	@echo "FlashOps —— 常用命令"
	@echo ""
	@echo "  make setup     建 venv、装依赖、建库、灌种子数据"
	@echo "  make demo      无硬件跑通一整条固件 A/B 回归（含故障注入与恢复）"
	@echo "  make dev       起控制平面与同源控制台 :8000"
	@echo "  make dev-agent 向已启动的控制平面注册并发送一次真实 Agent 心跳"
	@echo "  make test      后端单测 + 端到端 smoke"
	@echo "  make db-reset  删库重建并重灌种子"
	@echo "  make clean     清理本地运行产物"

setup: setup-py db-reset
	@echo ""
	@echo "✅ 就绪。下一步：make demo（看它跑）或 make dev（起服务）"

setup-py:
	@test -d .venv || python3 -m venv .venv
	@$(PIP) install --quiet --upgrade pip
	@$(PIP) install --quiet -r services/control-plane/requirements.txt
	@echo "✅ 后端依赖就绪"

db-reset:
	@rm -f "$(ROOT)/var/flashops.db"
	@rm -rf "$(ROOT)/var/objects"
	@mkdir -p "$(ROOT)/var/objects"
	@$(PY) -m flashops_control.cli init-db
	@$(PY) -m flashops_control.cli seed
	@echo "✅ 数据库已重建并灌入种子数据"

seed:
	@$(PY) -m flashops_control.cli seed

demo:
	@$(PY) -m flashops_control.cli demo

dev:
	@echo "控制台 http://localhost:8000/console/Dashboard.dc.html  ·  API http://localhost:8000/docs"
	@$(PY) -m uvicorn flashops_control.main:app --host 0.0.0.0 --port 8000 --reload

dev-api:
	@$(PY) -m uvicorn flashops_control.main:app --host 0.0.0.0 --port 8000 --reload

dev-web:
	@cd "$(CONSOLE)" && "$(PY)" -m http.server 3000

dev-agent:
	@$(PY) -m flashops_agent --server http://127.0.0.1:8000 --once

dev-agent-loop:
	@$(PY) -m flashops_agent --server http://127.0.0.1:8000

test: test-py

test-py:
	@"$(ROOT)/.venv/bin/pytest" services/control-plane/tests services/host-agent/tests -q

e2e:
	@"$(ROOT)/.venv/bin/pytest" services/control-plane/tests/test_e2e_vertical_slice.py -q -s

clean:
	@rm -rf "$(ROOT)/var" "$(ROOT)/.pytest_cache"
	@find "$(ROOT)" -name __pycache__ -type d -prune -exec rm -rf {} +
	@echo "✅ 已清理"
