.PHONY: web api run-api-opencode run-api-pi kill install build-agent-runtime setup-env sync-skills sync-autox-skill sync-pod-credentials clean doc help fetch-opencode-artifact docker-build-frontend docker-build-frontend-base docker-build-backend docker-build-backend-base

web:
	$(MAKE) -C frontend dev

api:
	$(MAKE) -C backend dev

run-api-opencode:
	$(MAKE) -C backend dev AGENT_TYPE=opencode

run-api-pi:
	$(MAKE) -C backend dev-pi

kill:
	pkill -f "uvicorn" || true
	pkill -f "next" || true

install:
	@echo "Installing investibook dependencies..."
	$(MAKE) -C backend install
	$(MAKE) -C frontend install
	@if [ -d agentkit/plugins/ya-opencode-telemetry ]; then \
		echo "Installing agentkit plugins..."; \
		cd agentkit/plugins/ya-opencode-telemetry && \
		if command -v bun >/dev/null 2>&1; then bun install; \
		elif command -v npm >/dev/null 2>&1; then npm install --ignore-scripts --no-package-lock; \
		else echo "bun or npm is required to install agentkit plugins" >&2; exit 1; fi; \
	fi
	@if [ -d agentkit/runtime/pi-core ]; then \
		echo "Installing Pi core runtime..."; \
		cd agentkit/runtime/pi-core && \
		if command -v bun >/dev/null 2>&1; then bun install --ignore-scripts; \
		elif command -v npm >/dev/null 2>&1; then npm install --ignore-scripts --no-package-lock; \
		else echo "bun or npm is required to install Pi core runtime" >&2; exit 1; fi; \
	fi
	@echo ""
	@echo "All dependencies installed!"
	@echo ""
	@echo "Note: If backend fails, you may need to sync skills first:"
	@echo "   make sync-skills"

build-agent-runtime:
	cd agentkit/runtime/pi-core && bun install --frozen-lockfile --ignore-scripts && bun run build
	cd agentkit/plugins/ya-opencode-telemetry && bun install --frozen-lockfile --ignore-scripts && bun run build

setup-env:
	$(MAKE) -C backend setup-env
	$(MAKE) -C frontend setup-env

sync-skills:
	@echo "Syncing skills from nutshell-skills repository..."
	./scripts/sync-nutshell-skills.sh
	@echo "Skills synced successfully"

sync-autox-skill:
	@echo "Syncing AutoX SQL tuning skill..."
	./scripts/sync-autox-skill.sh
	@echo "AutoX skill synced successfully"

sync-pod-credentials:
	@echo "Syncing credentials to Kubernetes pod (if running)..."
	@if kubectl get pods -n investibook -l app=backend --field-selector=status.phase=Running 2>/dev/null | grep -q backend; then \
		sh scripts/sync-pod-credentials.sh; \
	else \
		echo "   No running pod found, skipping kubectl sync"; \
		echo "   Run 'sh scripts/sync-pod-credentials.sh' manually after deploying"; \
	fi

doc:
	$(MAKE) -C tools/mdbook serve

clean:
	$(MAKE) -C frontend clean
	$(MAKE) -C backend clean
	rm -rf .book

# ============= Docker Build Targets (used by CI) =============

fetch-opencode-artifact:
	python3 scripts/fetch-opencode-artifact.py

docker-build-frontend-base: IMG ?= investibook-frontend-base:latest
docker-build-frontend-base:
	docker build -t ${IMG} -f frontend/Dockerfile.base frontend/

docker-build-frontend: IMG ?= investibook-frontend:latest
docker-build-frontend: BASE_IMAGE ?= investibook-frontend-base:latest
docker-build-frontend:
	docker build --build-arg BASE_IMAGE=${BASE_IMAGE} -t ${IMG} -f frontend/Dockerfile frontend/

docker-build-backend-base: IMG ?= investibook-backend-base:latest
docker-build-backend-base:
	docker build -t ${IMG} -f backend/Dockerfile.base backend/

docker-build-backend: IMG ?= investibook-backend:latest
docker-build-backend: BASE_IMAGE ?= investibook-backend-base:latest
docker-build-backend: fetch-opencode-artifact
	docker build --build-arg BASE_IMAGE=${BASE_IMAGE} -t ${IMG} -f backend/Dockerfile .

docker-build-evolver: IMG ?= investibook-evolver:latest
docker-build-evolver: BASE_IMAGE ?= investibook-backend-base:latest
docker-build-evolver:
	docker build --build-arg BASE_IMAGE=${BASE_IMAGE} -t ${IMG} -f deployment/evolver/Dockerfile .

# ============= Help =============

help:
	@echo "================================================================================"
	@echo "Investibook - AI Research Workspace Platform"
	@echo "================================================================================"
	@echo ""
	@echo "Available targets:"
	@echo ""
	@echo "  make sync-skills            - Sync skills from nutshell-skills repository"
	@echo "                                - Run this FIRST before make install"
	@echo "                                - Pulls latest skills from upstream"
	@echo ""
	@echo "  make sync-autox-skill       - Sync vendored AutoX SQL tuning skill"
	@echo "                                - Pulls AutoX into agentkit/skills/tidb-query-tuning-autox"
	@echo ""
	@echo "  make install                - Install ALL dependencies"
	@echo "                                - Frontend (pnpm, in frontend/)"
	@echo "                                - Backend (uv)"
	@echo "                                - Agentkit plugins"
	@echo "                                - Pi core runtime"
	@echo "  make build-agent-runtime    - Build standalone Pi and bundled OpenCode plugin"
	@echo ""
	@echo "  make web                    - Start frontend (Next.js)"
	@echo "  make run-api-opencode       - Start backend with OpenCode agent"
	@echo "  make run-api-pi             - Start backend with Pi agent"
	@echo ""
	@echo "  make sync-pod-credentials   - Sync clinic credentials to running K8s pod"
	@echo ""
	@echo "  make docker-build-frontend-base - Build frontend base image (Node+pnpm+deps)"
	@echo "  make docker-build-frontend      - Build frontend app (requires base image)"
	@echo ""
	@echo "  make kill                   - Kill all running processes"
	@echo "  make clean                  - Remove build artifacts"
	@echo ""
	@echo "  make doc                    - Build docs with mdbook (output: .book/)"
	@echo ""
	@echo "================================================================================"
	@echo "Quick start (first time setup):"
	@echo "  1. make sync-skills            # Sync skills from upstream"
	@echo "  2. make sync-autox-skill       # Sync AutoX SQL skill when updating diagnosis skills"
	@echo "  3. make install                # Install everything (~5-10 min)"
	@echo "  4. make web                    # Start frontend (terminal 1)"
	@echo "  5. make run-api-opencode       # Start backend (terminal 2)"
	@echo "     # or: make run-api-pi"
	@echo "================================================================================"
