.PHONY: install build test test-unit test-integration test-e2e test-cov test-docker test-docker-all clean lint format db-migrate db-seed help

# Variables
DOCKER_COMPOSE_TEST = docker compose -f docker-compose.test.yml

# Help
help:
	@echo "Available commands:"
	@echo "  make install           - Install dependencies"
	@echo "  make build             - Build the project"
	@echo "  make test              - Run all tests"
	@echo "  make test-unit         - Run unit tests only"
	@echo "  make test-integration  - Run integration tests"
	@echo "  make test-e2e          - Run end-to-end tests"
	@echo "  make test-cov          - Run tests with coverage"
	@echo "  make test-docker       - Run tests in Docker"
	@echo "  make test-docker-all   - Run all tests in Docker"
	@echo "  make lint              - Run linter"
	@echo "  make format            - Format code"
	@echo "  make db-start          - Start test database"
	@echo "  make db-stop           - Stop test database"
	@echo "  make db-migrate        - Run database migrations"
	@echo "  make db-seed           - Seed database"
	@echo "  make clean             - Clean build artifacts"

# Install dependencies
install:
	npm ci
	npx prisma generate

# Build
build:
	npm run build

# Lint
lint:
	npm run lint

# Format
format:
	npm run format

# Clean
clean:
	rm -rf dist coverage node_modules/.cache
	$(DOCKER_COMPOSE_TEST) down -v --remove-orphans 2>/dev/null || true

###############################
# Testing
###############################

# Run all tests (unit tests)
test:
	npm test

# Run unit tests only (domain and application layer)
test-unit:
	npm test -- --testPathPattern="(spec|test)\\.ts$$" --testPathIgnorePatterns="e2e"

# Run integration tests (requires database)
test-integration: db-start db-migrate
	DATABASE_URL="postgresql://postgres:postgres@localhost:5433/rwadurian_reporting_test?schema=public" \
	REDIS_HOST=localhost \
	REDIS_PORT=6380 \
	npm test -- --testPathPattern="integration" --runInBand
	@$(MAKE) db-stop

# Run e2e tests (requires full stack)
test-e2e: db-start db-migrate
	DATABASE_URL="postgresql://postgres:postgres@localhost:5433/rwadurian_reporting_test?schema=public" \
	REDIS_HOST=localhost \
	REDIS_PORT=6380 \
	npm run test:e2e -- --runInBand
	@$(MAKE) db-stop

# Run tests with coverage
test-cov:
	npm run test:cov

###############################
# Docker Testing
###############################

# Start test infrastructure (postgres + redis)
db-start:
	$(DOCKER_COMPOSE_TEST) up -d postgres-test redis-test
	@echo "Waiting for services to be healthy..."
	@sleep 5

# Stop test infrastructure
db-stop:
	$(DOCKER_COMPOSE_TEST) down -v

# Run database migrations
db-migrate:
	DATABASE_URL="postgresql://postgres:postgres@localhost:5433/rwadurian_reporting_test?schema=public" \
	npx prisma migrate deploy 2>/dev/null || \
	DATABASE_URL="postgresql://postgres:postgres@localhost:5433/rwadurian_reporting_test?schema=public" \
	npx prisma db push --skip-generate

# Seed database
db-seed:
	DATABASE_URL="postgresql://postgres:postgres@localhost:5433/rwadurian_reporting_test?schema=public" \
	npx ts-node prisma/seed.ts

# Build test Docker image
docker-build-test:
	docker build -f Dockerfile.test -t reporting-service-test .

# Run unit tests in Docker
test-docker: docker-build-test
	$(DOCKER_COMPOSE_TEST) run --rm reporting-service-test npm run test

# Run all tests in Docker (unit + integration + e2e)
test-docker-all:
	$(DOCKER_COMPOSE_TEST) up --build --abort-on-container-exit --exit-code-from reporting-service-test
	$(DOCKER_COMPOSE_TEST) down -v

# Run tests with coverage in Docker
test-docker-cov:
	$(DOCKER_COMPOSE_TEST) run --rm reporting-service-test npm run test:cov
	$(DOCKER_COMPOSE_TEST) down -v

###############################
# CI/CD Helpers
###############################

# CI test command
ci-test: install test-cov lint

# Full test suite
full-test: clean install lint test-unit test-docker-all
	@echo "All tests passed!"
