# rdev-api development commands
# Run 'make help' to see available targets

.PHONY: help setup run test build sdk sdk-check clean db-up db-down db-reset db-shell db-logs

# Load .env.local if it exists
ifneq (,$(wildcard .env.local))
	include .env.local
endif

# Default values (can be overridden by .env.local)
PORT ?= 8080
DB_HOST ?= localhost
DB_PORT ?= 5433
DB_USER ?= appuser
DB_PASSWORD ?= localdev
DB_NAME ?= rdev
DB_SSL_MODE ?= disable
RDEV_ADMIN_KEY ?= rdev_sk_devadmin_0123456789abcdef0123456789abcdef

# Export all variables to child processes
export PORT DB_HOST DB_PORT DB_USER DB_PASSWORD DB_NAME DB_SSL_MODE RDEV_ADMIN_KEY

help: ## Show this help
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-15s\033[0m %s\n", $$1, $$2}'

setup: ## First-time setup: copy env template
	@if [ ! -f .env.local ]; then \
		cp .env.local.example .env.local; \
		echo "Created .env.local from template"; \
	else \
		echo ".env.local already exists"; \
	fi

run: ## Run the API server locally
	go run ./cmd/rdev-api

test: ## Run all tests
	go test -v ./...

build: ## Build the binary
	CGO_ENABLED=0 go build -o rdev-api ./cmd/rdev-api

sdk: ## Regenerate sdk/openapi.json from the embedded OpenAPI spec
	go run ./cmd/rdev-api --export-openapi > sdk/openapi.json
	@echo "sdk/openapi.json updated"

sdk-check: ## Verify sdk/openapi.json matches the embedded spec (CI drift detection)
	@go run ./cmd/rdev-api --export-openapi > /tmp/openapi-check.json
	@diff sdk/openapi.json /tmp/openapi-check.json > /dev/null || \
		(echo "ERROR: sdk/openapi.json is out of sync. Run 'make sdk'." && exit 1)
	@echo "sdk/openapi.json is up to date"

clean: ## Remove build artifacts
	rm -f rdev-api

# Database commands
db-up: ## Start local PostgreSQL container
	docker compose up -d postgres
	@echo "Waiting for postgres to be ready..."
	@until docker compose exec -T postgres pg_isready -U $(DB_USER) -d $(DB_NAME) > /dev/null 2>&1; do \
		sleep 1; \
	done
	@echo "PostgreSQL is ready on port $(DB_PORT)"

db-down: ## Stop local PostgreSQL container
	docker compose down

db-reset: ## Reset database (destroy and recreate)
	docker compose down -v
	$(MAKE) db-up

db-shell: ## Open psql shell to local database
	docker compose exec postgres psql -U $(DB_USER) -d $(DB_NAME)

db-logs: ## View PostgreSQL logs
	docker compose logs -f postgres

# Combined commands
dev: db-up run ## Start database and run API

# Testing helpers
curl-health: ## Test health endpoint
	@curl -s http://localhost:$(PORT)/health | jq .

curl-projects: ## Test projects endpoint (requires auth)
	@curl -s -H "Authorization: Bearer $(RDEV_ADMIN_KEY)" http://localhost:$(PORT)/projects | jq .

curl-keys: ## Test keys endpoint (requires auth)
	@curl -s -H "Authorization: Bearer $(RDEV_ADMIN_KEY)" http://localhost:$(PORT)/keys | jq .

create-test-key: ## Create a test API key
	@curl -s -X POST -H "Authorization: Bearer $(RDEV_ADMIN_KEY)" \
		-H "Content-Type: application/json" \
		-d '{"name": "test-key", "scopes": ["projects:read"], "expires_in": "30d"}' \
		http://localhost:$(PORT)/keys | jq .
