.PHONY: help up down logs migrate test test-unit test-integration test-system test-e2e lint clean build

# Default target
help:
	@echo "Headquarter Development Commands"
	@echo "================================"
	@echo "make up           - Start all services"
	@echo "make down         - Stop all services"
	@echo "make logs         - View service logs"
	@echo "make migrate      - Run database migrations"
	@echo "make test         - Run all test suites"
	@echo "make test-unit    - Run unit tests only"
	@echo "make test-integration - Run integration tests only"
	@echo "make test-system  - Run system tests only"
	@echo "make test-e2e     - Run E2E tests (Playwright)"
	@echo "make lint         - Run linting"
	@echo "make build        - Build all Docker images"
	@echo "make clean        - Remove containers and volumes"
	@echo "make shell        - Open shell in API container"

# Start services
up:
	docker compose up -d
	@echo "Services starting..."
	@echo "API: http://localhost:8000"
	@echo "Web: http://localhost:3000"
	@echo "Postgres: localhost:5432"
	@echo "Redis: localhost:6379"

# Stop services
down:
	docker compose down

# View logs
logs:
	docker compose logs -f

# View specific service logs
logs-api:
	docker compose logs -f api

logs-web:
	docker compose logs -f web

logs-db:
	docker compose logs -f postgres

# Run database migrations
migrate:
	docker compose exec api alembic upgrade head

# Create new migration
migration:
	docker compose exec api alembic revision --autogenerate -m "$(message)"

# Run all tests
test:
	docker compose exec api pytest -v

# Run unit tests only (fast, no external dependencies)
test-unit:
	docker compose exec api pytest -v -m unit tests/unit/

# Run integration tests only (requires database)
test-integration:
	docker compose exec api pytest -v -m integration tests/integration/

# Run system tests only (full stack)
test-system:
	docker compose exec api pytest -v -m system tests/system/

# Run E2E tests (requires full application stack)
test-e2e:
	cd e2e && npx playwright test

# Run linting
lint:
	docker compose exec api ruff check .
	docker compose exec api mypy .
	cd apps/web && npm run lint

# Type checking
typecheck:
	docker compose exec api mypy .
	cd apps/web && npm run typecheck

# Build all images
build:
	docker compose build

# Build specific service
build-api:
	docker compose build api

build-web:
	docker compose build web

# Clean up
clean:
	docker compose down -v --remove-orphans
	docker system prune -f

# Open shell in API container
shell:
	docker compose exec api /bin/sh

# Database shell
db-shell:
	docker compose exec postgres psql -U $(POSTGRES_USER) -d $(POSTGRES_DB)

# Health check
health:
	@echo "Checking service health..."
	@docker compose ps
