# FireFlow Project Makefile

.PHONY: all build prepublish-check publish publish-packages clean \
	db-migrate-build db-migrate-run db-migrate ssl-certs

# Default target - only builds the project
all: build

# Build all packages
build:
	pnpm run build

prepublish-check: ## Run prepublish checks (linting, testing)
	pnpm run prepublish-check

publish: prepublish-check build publish-packages ## Full publish workflow: check -> build -> publish


publish-packages: ## Publish all packages individually
	@echo "Publishing all packages..."
	pnpm --filter @persistent-ai/fireflow-types publish --access restricted
	pnpm --filter @persistent-ai/fireflow-nodes publish --access restricted
	pnpm --filter @persistent-ai/fireflow-trpc publish --access restricted
	pnpm --filter @persistent-ai/fireflow-backend publish --access restricted
	pnpm --filter @persistent-ai/fireflow-frontend publish --access restricted
	pnpm --filter @persistent-ai/persistentai-api publish --access restricted
	# Root package should be published last
	pnpm publish --access restricted --no-git-checks
	@echo "All packages have been published!"


clean-builds: ## Clean build outputs
	@echo "Cleaning build outputs..."
	rm -rf ./dist/
	rm -rf ./build/
	rm -rf ./coverage/
	rm -rf ./*/**/dist/
	rm -rf ./*/**/build/
	rm -rf ./*/**/coverage/
	rm -rf ./*/**/*.tsbuildinfo
	@echo "Build outputs cleaned!"

clean-cache: # Clean cache
	@echo "Cleaning cache..."
	rm -rf ./.turbo
	rm -rf ./.cache
	@echo "Cache cleaned!"

clean: clean-builds clean-cache ## Clean everything
	@echo "Project cleaned successfully!"

# Database migration commands
# ----------------------------------------------------------------

# Name of the migration Docker image
MIGRATION_IMAGE := Persistent-AI/fireflow-migration

# Path to the migration Dockerfile
MIGRATION_DOCKERFILE := apps/fireflow-backend/migrate.Dockerfile

db-migrate-build: ## Build the migration Docker image
	@echo "Building database migration Docker image..."
	docker build -t $(MIGRATION_IMAGE) -f $(MIGRATION_DOCKERFILE) .
	@echo "Migration image built successfully!"

# Usage: make db-migrate-run DATABASE_URL="postgres://username:password@hostname:5432/dbname"
db-migrate-run: ## Run database migrations with the specified DATABASE_URL
ifndef DATABASE_URL
	@echo "Error: DATABASE_URL is required"
	@echo "Usage: make db-migrate-run DATABASE_URL=\"postgres://username:password@hostname:5432/dbname\""
	@exit 1
endif
	@echo "Running database migrations..."
	docker run --rm -e DATABASE_URL="$(DATABASE_URL)" $(MIGRATION_IMAGE)
	@echo "Database migrations completed!"

# Build migration image and run migrations in one command
# Usage: make db-migrate DATABASE_URL="postgres://username:password@hostname:5432/dbname"
db-migrate: db-migrate-build db-migrate-run ## Build migration image and run migrations in one command

ifeq ($(CI),true)
o:=.ops
include $o/Makefile
endif

# SSL certificate generation (for local HTTPS development)
# ----------------------------------------------------------------
# Requires mkcert: brew install mkcert

# Install local CA and generate certs for localhost
ssl-certs:
	@command -v mkcert >/dev/null 2>&1 || { echo "Error: mkcert not found. Install with: brew install mkcert"; exit 1; }
	mkcert -install
	mkdir -p certs
	mkcert -key-file certs/key.pem -cert-file certs/cert.pem localhost fireflow.local 127.0.0.1 ::1
	@# Copy the mkcert root CA into the repo so Node/Bun can trust it via
	@# NODE_EXTRA_CA_CERTS. Bun/Node TLS does NOT consult the system keychain that
	@# `mkcert -install` populates, so server-side fetch() (e.g. the MCP probe
	@# connecting to https://fireflow.local) would otherwise fail cert validation.
	@# The dev scripts set NODE_EXTRA_CA_CERTS=../../certs/rootCA.pem.
	cp "$$(mkcert -CAROOT)/rootCA.pem" certs/rootCA.pem
	@echo ""
	@echo "SSL certificates generated in certs/ (incl. rootCA.pem for NODE_EXTRA_CA_CERTS)"
	@echo "Enable HTTPS by setting in .env:"
	@echo "  SSL_ENABLED=true"
	@echo "  SSL_CERT_PATH=./certs/cert.pem"
	@echo "  SSL_KEY_PATH=./certs/key.pem"
	@echo ""
