.PHONY: all build run clean install dev test test-proxy test-proxy-postgres-up test-proxy-postgres-down test-proxy-postgres-contract test-proxy-postgres

# Default target
all: install build

# Install dependencies
install:
	@echo "📦 Installing Go dependencies..."
	cd proxy && go mod download
	@echo "📦 Installing Node dependencies..."
	cd svelte && npm install

# Build both services
build: build-proxy build-svelte

build-proxy:
	@echo "🔨 Building proxy server..."
	cd proxy && go build -o ../bin/proxy cmd/proxy/main.go

build-svelte:
	@echo "🔨 Building svelte interface..."
	cd svelte && npm run build

# Run in development mode
dev:
	@echo "🚀 Starting development servers..."
	./run.sh

# Run proxy only
run-proxy:
	cd proxy && go run cmd/proxy/main.go

# Run Go tests for the proxy
test-proxy:
	cd proxy && go test ./...

# Run only the env-gated Postgres contract suite
test-proxy-postgres-contract:
	cd proxy && go test ./internal/service -run TestPostgresStorageContract -count=1

# Start disposable Postgres for contract tests
test-proxy-postgres-up:
	docker compose -f ../docker-compose.test.yml up -d --wait --remove-orphans postgres-test

# Stop disposable Postgres for contract tests
test-proxy-postgres-down:
	docker compose -f ../docker-compose.test.yml down -v --remove-orphans

# Start disposable Postgres, run contract suite, then stop it
test-proxy-postgres:
	@set -e; \
	trap 'docker compose -f ../docker-compose.test.yml down -v --remove-orphans >/dev/null 2>&1 || true' EXIT; \
	docker compose -f ../docker-compose.test.yml up -d --wait --remove-orphans postgres-test; \
	TEST_POSTGRES_DSN=$${TEST_POSTGRES_DSN:-postgresql://$${TEST_POSTGRES_USER:-test}:$${TEST_POSTGRES_PASSWORD:-test}@localhost:$${TEST_POSTGRES_PORT:-5434}/$${TEST_POSTGRES_DB:-claude_code_proxy_test}?sslmode=disable} \
	$(MAKE) test-proxy-postgres-contract

# Run web only
run-svelte:
	cd svelte && npm run dev

# Clean build artifacts
clean:
	@echo "🧹 Cleaning build artifacts..."
	rm -rf bin/
	rm -rf svelte/build/
	rm -rf svelte/.svelte-kit/
	rm -f requests.db
	rm -rf requests/

# Database operations
db-reset:
	@echo "🗑️  Resetting database..."
	rm -f requests.db
	rm -rf requests/

# Help
help:
	@echo "Claude Code Proxy - Available targets:"
	@echo "  make install    - Install all dependencies"
	@echo "  make build      - Build both services"
	@echo "  make dev        - Run in development mode"
	@echo "  make run-proxy  - Run proxy server only"
	@echo "  make test-proxy - Run all Go proxy tests"
	@echo "  make test-proxy-postgres-up - Start disposable Postgres for contract tests"
	@echo "  make test-proxy-postgres-down - Stop disposable Postgres for contract tests"
	@echo "  make test-proxy-postgres-contract - Run Postgres storage contract test (requires TEST_POSTGRES_DSN)"
	@echo "  make test-proxy-postgres - Start disposable Postgres, run contract test, stop it"
	@echo "  make run-svelte - Run svelte interface only"
	@echo "  make clean      - Clean build artifacts"
	@echo "  make db-reset   - Reset database"
	@echo "  make help       - Show this help message"
