.DEFAULT_GOAL := help

COMPOSE ?= docker compose
COMPOSE_DIR := infrastructure/dev
COMPOSE_FILE := $(COMPOSE_DIR)/docker-compose.yml
ENV_FILE := $(COMPOSE_DIR)/.env
COMPOSE_CMD := $(COMPOSE) -f $(COMPOSE_FILE) --env-file $(ENV_FILE) --project-directory $(COMPOSE_DIR)

ifneq ("$(wildcard $(ENV_FILE))","")
  include $(ENV_FILE)
endif

POSTGRES_USER ?= knowfoolery
POSTGRES_PASSWORD ?= knowfoolery
POSTGRES_PORT ?= 5432
POSTGRES_DB ?= knowfoolery
REDIS_PORT ?= 6379
ZITADEL_PORT ?= 8080
PROMETHEUS_PORT ?= 9091
GRAFANA_PORT ?= 3001
JAEGER_UI_PORT ?= 16686
ZITADEL_ADMIN_USERNAME ?= admin
ZITADEL_ADMIN_PASSWORD ?= AdminPassword123!
GRAFANA_ADMIN_USER ?= admin

.PHONY: help dev dev-full dev-auth dev-gateway stop clean \
        backend-lint backend-test backend-build \
        frontend-dev frontend-lint frontend-test frontend-build \
        db-up db-down db-logs db-shell redis-shell \
        lint test build

# Default target
help:
	@echo "KnowFoolery Development Commands"
	@echo ""
	@echo "Development Environment:"
	@echo "  make dev          - Start core infrastructure (PostgreSQL, Redis)"
	@echo "  make dev-full     - Start core infrastructure + observability"
	@echo "  make dev-auth     - Start core infrastructure + Zitadel auth"
	@echo "  make dev-gateway  - Start core infrastructure + gateway ingress (NGINX + gateway service)"
	@echo "  make stop         - Stop all containers"
	@echo "  make clean        - Stop containers and remove volumes"
	@echo ""
	@echo "Backend:"
	@echo "  make backend-lint  - Run Go linters"
	@echo "  make backend-test  - Run Go tests"
	@echo "  make backend-build - Build all services"
	@echo ""
	@echo "Frontend:"
	@echo "  make frontend-dev   - Start frontend dev server"
	@echo "  make frontend-lint  - Run ESLint and Prettier"
	@echo "  make frontend-test  - Run frontend tests"
	@echo "  make frontend-build - Build frontend for production"
	@echo ""
	@echo "Database:"
	@echo "  make db-up        - Start PostgreSQL and Redis"
	@echo "  make db-down      - Stop PostgreSQL and Redis"
	@echo "  make db-logs      - Show PostgreSQL logs"
	@echo "  make db-shell     - Open PostgreSQL shell"
	@echo "  make redis-shell  - Open Redis CLI"
	@echo ""
	@echo "All:"
	@echo "  make lint         - Run all linters"
	@echo "  make test         - Run all tests"
	@echo "  make build        - Build everything"

# =============================================================================
# Development Environment
# =============================================================================

dev:
	@echo "Starting core infrastructure..."
	@$(COMPOSE_CMD) up -d postgres redis
	@echo "Waiting for services to be healthy..."
	@sleep 5
	@echo ""
	@echo "Infrastructure ready:"
	@echo "  PostgreSQL: localhost:$(POSTGRES_PORT) (user: $(POSTGRES_USER), password: $(POSTGRES_PASSWORD))"
	@echo "  Redis:      localhost:$(REDIS_PORT)"

dev-full: dev
	@echo "Starting observability stack..."
	@$(COMPOSE_CMD) --profile observability up -d
	@echo ""
	@echo "Observability ready:"
	@echo "  Prometheus: http://localhost:$(PROMETHEUS_PORT)"
	@echo "  Grafana:    http://localhost:$(GRAFANA_PORT) (admin: $(GRAFANA_ADMIN_USER))"
	@echo "  Jaeger:     http://localhost:$(JAEGER_UI_PORT)"

dev-auth: dev
	@echo "Starting authentication stack..."
	@$(COMPOSE_CMD) --profile auth up -d
	@echo ""
	@echo "Zitadel ready at http://localhost:$(ZITADEL_PORT)"
	@echo "Admin credentials: $(ZITADEL_ADMIN_USERNAME) / $(ZITADEL_ADMIN_PASSWORD)"

dev-gateway: dev
	@echo "Starting gateway ingress stack..."
	@$(COMPOSE_CMD) --profile gateway up -d gateway-service nginx
	@echo ""
	@echo "Gateway ingress ready:"
	@echo "  Public entrypoint (NGINX): http://localhost:$(GATEWAY_PORT)"

stop:
	@echo "Stopping all containers..."
	@$(COMPOSE_CMD) down

clean:
	@echo "Stopping containers and removing volumes..."
	@$(COMPOSE_CMD) down -v

# =============================================================================
# Backend
# =============================================================================

backend-lint:
	@echo "Running Go linters..."
	@cd backend && golangci-lint run ./...

backend-test:
	@echo "Running Go tests..."
	@cd backend && go test -v -race -cover ./...

backend-build:
	@echo "Building all services..."
	@cd backend && go build ./...

# =============================================================================
# Frontend
# =============================================================================

frontend-dev:
	@echo "Starting frontend development server..."
	@cd frontend && yarn dev

frontend-lint:
	@echo "Running frontend linters..."
	@cd frontend && yarn lint && yarn format:check

frontend-test:
	@echo "Running frontend tests..."
	@cd frontend && yarn test

frontend-build:
	@echo "Building frontend..."
	@cd frontend && yarn build

# =============================================================================
# Database Utilities
# =============================================================================

db-up:
	@$(COMPOSE_CMD) up -d postgres redis

db-down:
	@$(COMPOSE_CMD) stop postgres redis

db-logs:
	@$(COMPOSE_CMD) logs -f postgres

db-shell:
	@docker exec -it knowfoolery-postgres psql -U $(POSTGRES_USER) -d $(POSTGRES_DB)

redis-shell:
	@docker exec -it knowfoolery-redis redis-cli

# =============================================================================
# Combined Commands
# =============================================================================

lint: backend-lint frontend-lint
	@echo "All linters passed!"

test: backend-test frontend-test
	@echo "All tests passed!"

build: backend-build frontend-build
	@echo "Build complete!"
