.PHONY: help build test lint clean docker-build docker-up docker-down migrate migrate-rollback migrate-status migrate-force seed dev install setup version sync-version check-version

# ============================================================================
# VERSION — single source of truth is the root VERSION file (see docs/VERSIONING.md)
# ============================================================================
VERSION    := $(shell cat VERSION 2>/dev/null | tr -d '[:space:]')
GIT_COMMIT := $(shell git rev-parse --short HEAD 2>/dev/null || echo unknown)
LDFLAGS    := -X main.Version=$(VERSION) -X main.Commit=$(GIT_COMMIT)
COMPOSE    ?= docker compose

help:
	@echo "╔════════════════════════════════════════════════════════════════╗"
	@echo "║         OpenRisk Development Commands                          ║"
	@echo "╚════════════════════════════════════════════════════════════════╝"
	@echo ""
	@echo "  🚀 QUICK START"
	@echo "     make setup                - Initial setup (install dependencies)"
	@echo "     make dev                  - Start full development environment"
	@echo "     make docker-up            - Start all Docker services"
	@echo ""
	@echo "  🔨 BUILD & COMPILE"
	@echo "     make build                - Build backend binary"
	@echo "     make frontend-build       - Build frontend for production"
	@echo ""
	@echo "  🧪 TESTING"
	@echo "     make test                 - Run all backend tests with coverage"
	@echo "     make test-unit            - Run unit tests only (fast)"
	@echo "     make test-integration     - Run integration tests (slow)"
	@echo "     make frontend-test        - Run frontend tests"
	@echo ""
	@echo "  📝 CODE QUALITY"
	@echo "     make lint                 - Run all linters"
	@echo "     make lint-backend         - Run backend linter (golangci-lint)"
	@echo "     make lint-frontend        - Run frontend linter (ESLint)"
	@echo "     make format               - Format code (gofmt + prettier)"
	@echo ""
	@echo "  🐳 DOCKER"
	@echo "     make docker-up            - Start all containers"
	@echo "     make docker-down          - Stop all containers"
	@echo "     make docker-build         - Build Docker image"
	@echo "     make docker-logs          - View live container logs"
	@echo "     make docker-status        - Show container status"
	@echo "     make docker-clean         - Remove containers and volumes"
	@echo ""
	@echo "  💾 DATABASE"
	@echo "     make migrate              - Run database migrations"
	@echo "     make migrate-rollback     - Rollback last migration"
	@echo "     make migrate-status       - Show current migration version + dirty flag"
	@echo "     make migrate-force VERSION=<n> - Recover a DIRTY database (set version, clear dirty)"
	@echo "     make seed                 - Seed database with sample data"
	@echo "     make db-shell             - Open database shell (psql)"
	@echo "     make db-test-shell        - Open test database shell"
	@echo ""
	@echo "  🧹 CLEANUP"
	@echo "     make clean                - Clean build artifacts"
	@echo "     make clean-all            - Clean everything (DESTRUCTIVE)"
	@echo ""

# ============================================================================
# SETUP & INSTALLATION
# ============================================================================

setup: install frontend-install
	@echo "✅ Setup complete! Run 'make dev' to start developing."

install:
	@echo "📦 Installing backend dependencies..."
	cd backend && go mod download
	@echo "✅ Backend dependencies installed"

frontend-install:
	@echo "📦 Installing frontend dependencies..."
	cd frontend && npm ci
	@echo "✅ Frontend dependencies installed"

# ============================================================================
# BUILD & COMPILE
# ============================================================================

build:
	@echo "🔨 Building backend binary (v$(VERSION), commit $(GIT_COMMIT))..."
	cd backend && CGO_ENABLED=0 go build -trimpath -ldflags "$(LDFLAGS)" -o openrisk ./cmd/server
	@echo "✅ Backend binary built: backend/openrisk"

# ----------------------------------------------------------------------------
# VERSION propagation & verification
# ----------------------------------------------------------------------------
version: ## Print the single-source version
	@echo "$(VERSION)"

sync-version: ## Write VERSION into the Helm chart and frontend package.json
	@echo "🔁 Propagating VERSION=$(VERSION) to Helm chart + frontend..."
	@sed -i.bak -E 's/^version: .*/version: $(VERSION)/' helm/openrisk/Chart.yaml && rm -f helm/openrisk/Chart.yaml.bak
	@sed -i.bak -E 's/^appVersion: .*/appVersion: "$(VERSION)"/' helm/openrisk/Chart.yaml && rm -f helm/openrisk/Chart.yaml.bak
	@sed -i.bak -E '0,/"version":/ s/("version": *")[^"]*"/\1$(VERSION)"/' frontend/package.json && rm -f frontend/package.json.bak
	@echo "✅ Synced. Review the diff, then commit."

check-version: ## Fail if Helm chart / frontend version drift from VERSION (used by release CI)
	@chart="$$(grep -E '^version:' helm/openrisk/Chart.yaml | head -1 | awk '{print $$2}')"; \
	 app="$$(grep -E '^appVersion:' helm/openrisk/Chart.yaml | head -1 | sed -E 's/appVersion: *"?([^"]*)"?/\1/')"; \
	 fe="$$(grep -E '"version"' frontend/package.json | head -1 | sed -E 's/.*: *"([^"]*)".*/\1/')"; \
	 fail=0; \
	 [ "$$chart" = "$(VERSION)" ] || { echo "::error::Helm chart version '$$chart' != VERSION '$(VERSION)'"; fail=1; }; \
	 [ "$$app" = "$(VERSION)" ]   || { echo "::error::Helm appVersion '$$app' != VERSION '$(VERSION)'"; fail=1; }; \
	 [ "$$fe" = "$(VERSION)" ]    || { echo "::error::frontend version '$$fe' != VERSION '$(VERSION)'"; fail=1; }; \
	 if [ "$$fail" -ne 0 ]; then exit 1; fi; \
	 echo "✅ Versions aligned on $(VERSION) (chart/appVersion/frontend)"

frontend-build:
	@echo "🔨 Building frontend for production..."
	cd frontend && npm run build
	@echo "✅ Frontend built: frontend/dist/"

# ============================================================================
# TESTING
# ============================================================================

test:
	@echo "🧪 Running all backend tests with coverage..."
	cd backend && go test -v -coverprofile=coverage.out ./...
	@echo ""
	@echo "📊 Coverage report:"
	cd backend && go tool cover -func=coverage.out | tail -1

test-unit:
	@echo "🧪 Running unit tests (fast)..."
	cd backend && go test -v -short ./...

test-integration:
	@echo "🧪 Running integration tests..."
	$(COMPOSE) up -d test_db
	@sleep 2
	cd backend && DATABASE_URL="postgres://test:test@localhost:5435/openrisk_test" go test -v -tags=integration ./...
	$(COMPOSE) stop test_db

frontend-test:
	@echo "🧪 Running frontend tests..."
	cd frontend && npm run test

# ============================================================================
# CODE QUALITY & FORMATTING
# ============================================================================

lint-backend:
	@echo "📝 Running backend linter..."
	cd backend && golangci-lint run ./...

lint-frontend:
	@echo "📝 Running frontend linter..."
	cd frontend && npm run lint

lint: lint-backend lint-frontend
	@echo "✅ All linting passed!"

format:
	@echo "🎨 Formatting code..."
	cd backend && gofmt -s -w .
	cd frontend && npm run format
	@echo "✅ Code formatted!"

# ============================================================================
# DOCKER MANAGEMENT
# ============================================================================

docker-up:
	@echo "🐳 Starting all containers..."
	$(COMPOSE) up -d --build
	@echo "✅ Containers started"
	@echo ""
	@echo "   Frontend:  http://localhost:5173"
	@echo "   Backend:   http://localhost:8080"
	@echo "   Postgres:  localhost:5434 (openrisk)"
	@echo "   Redis:     localhost:6380"

docker-down:
	@echo "🐳 Stopping all containers..."
	$(COMPOSE) down

docker-build:
	@echo "🐳 Building Docker image..."
	docker build -t openrisk:latest .
	@echo "✅ Docker image built"

docker-logs:
	@echo "📋 Showing live container logs..."
	$(COMPOSE) logs -f

docker-status:
	@echo "📊 Container status:"
	$(COMPOSE) ps

docker-clean:
	@echo "🗑️  Removing containers and volumes (DESTRUCTIVE)..."
	$(COMPOSE) down -v
	@echo "✅ Cleanup complete"

# ============================================================================
# DATABASE MANAGEMENT
# ============================================================================

migrate:
	@echo "📊 Running database migrations..."
	$(COMPOSE) up -d db
	@sleep 2
	cd backend && MIGRATIONS_DIR=../migrations go run ./cmd/server migrate up
	@echo "✅ Migrations complete"

migrate-rollback:
	@echo "⏮️  Rolling back last migration..."
	$(COMPOSE) up -d db
	@sleep 2
	cd backend && MIGRATIONS_DIR=../migrations go run ./cmd/server migrate down

migrate-status:
	@echo "🔎 Current migration version..."
	cd backend && MIGRATIONS_DIR=../migrations go run ./cmd/server migrate version

# Recovery for a DIRTY database (a migration that failed mid-apply). Set VERSION
# to the last known-good migration, then re-run `make migrate`.
# Example: make migrate-force VERSION=47
migrate-force:
	@if [ -z "$(VERSION)" ]; then echo "Usage: make migrate-force VERSION=<known_good_version>"; exit 1; fi
	@echo "🔧 Forcing migration version to $(VERSION) (clears dirty flag)..."
	cd backend && MIGRATIONS_DIR=../migrations go run ./cmd/server migrate force $(VERSION)
	@echo "✅ Rollback complete"

seed:
	@echo "🌱 Seeding database with sample data..."
	cd backend && go run ./cmd/server seed
	@echo "✅ Database seeded"

db-shell:
	@echo "🔌 Connecting to production database..."
	$(COMPOSE) exec db psql -U openrisk -d openrisk

db-test-shell:
	@echo "🔌 Connecting to test database..."
	$(COMPOSE) exec test_db psql -U test -d openrisk_test

# ============================================================================
# DEVELOPMENT
# ============================================================================

dev:
	@echo "🚀 Starting development environment..."
	@echo ""
	@echo "   Starting Docker services (db, redis)..."
	$(COMPOSE) up -d db redis test_db
	@sleep 2
	@echo ""
	@echo "   Frontend will be available at: http://localhost:5173"
	@echo "   Backend API at:                http://localhost:8080/api/v1"
	@echo ""
	@echo "   Press Ctrl+C to stop. Use 'make docker-down' to stop Docker services."
	@echo ""
	@sleep 1
	@echo "Starting backend (in background)..."
	cd backend && OPENRISK_DEPLOYMENT=$${OPENRISK_DEPLOYMENT:-self_hosted} go run ./cmd/server &
	@sleep 2
	@echo ""
	@echo "Starting frontend..."
	cd frontend && npm run dev

dev-docker:
	@echo "🐳 Starting full development environment in Docker..."
	$(COMPOSE) up

dev-logs:
	@echo "📋 Following all container logs..."
	$(COMPOSE) logs -f

# ============================================================================
# CLEANUP
# ============================================================================

clean:
	@echo "🧹 Cleaning build artifacts..."
	cd backend && go clean
	rm -f backend/openrisk
	rm -f backend/coverage.out
	rm -rf frontend/dist
	@echo "✅ Cleanup complete"

clean-all: clean docker-clean
	@echo "🗑️  Full cleanup complete (DESTRUCTIVE)"

.DEFAULT_GOAL := help
