.PHONY: lint ci-lint lint-biome lint-ruff lint-ty test test-db-up test-db-down help

# Default target
all: lint

# Package directory
PKG_DIR := ./mountaineer_auth/
VIEWS_DIR := ./mountaineer_auth/views/
TEST_COMPOSE_FILE := docker-compose.test.yml

# Define a function to run ruff on a specific directory
# Usage: $(call run_ruff,<directory>)
define run_ruff
	@echo "\n=== Running ruff on $(1) ==="; \
	echo "Running ruff format in $(1)"; \
	(cd $(1) && uv run ruff format .) || { echo "FAILED: ruff format in $(1)"; exit 1; }; \
	echo "Running ruff check --fix in $(1)"; \
	(cd $(1) && uv run ruff check --fix .) || { echo "FAILED: ruff check in $(1)"; exit 1; }; \
	echo "=== ruff completed successfully for $(1) ===";
endef

# Define a function to run ruff in CI mode (check only, no fixes)
# Usage: $(call run_ruff_ci,<directory>)
define run_ruff_ci
	@echo "\n=== Running ruff (validation only) on $(1) ==="; \
	echo "Running ruff format --check in $(1)"; \
	(cd $(1) && uv run ruff format --check .) || { echo "FAILED: ruff format in $(1)"; exit 1; }; \
	echo "Running ruff check (no fix) in $(1)"; \
	(cd $(1) && uv run ruff check .) || { echo "FAILED: ruff check in $(1)"; exit 1; }; \
	echo "=== ruff validation completed successfully for $(1) ===";
endef

# Define a function to run ty on a specific directory
# Usage: $(call run_ty,<directory>)
define run_ty
	@echo "\n=== Running ty on $(1) ==="; \
	(uv run ty check --project . $(1)) || { echo "FAILED: ty in $(1)"; exit 1; }; \
	echo "=== ty completed successfully for $(1) ===";
endef

# Define a function to run biome on the frontend source directories
define run_biome
	@echo "\n=== Running biome on $(VIEWS_DIR) ==="; \
	(cd $(VIEWS_DIR) && npm exec -- biome check --write package.json postcss.config.mjs $$(find auth emails -type f \( -name '*.ts' -o -name '*.tsx' -o -name '*.js' -o -name '*.jsx' \) ! -path '*/_server/*')) || { echo "FAILED: biome in $(VIEWS_DIR)"; exit 1; }; \
	echo "=== biome completed successfully for $(VIEWS_DIR) ===";
endef

# Define a function to run biome in CI mode (check only, no fixes)
define run_biome_ci
	@echo "\n=== Running biome (validation only) on $(VIEWS_DIR) ==="; \
	(cd $(VIEWS_DIR) && npm exec -- biome check package.json postcss.config.mjs $$(find auth emails -type f \( -name '*.ts' -o -name '*.tsx' -o -name '*.js' -o -name '*.jsx' \) ! -path '*/_server/*')) || { echo "FAILED: biome in $(VIEWS_DIR)"; exit 1; }; \
	echo "=== biome validation completed successfully for $(VIEWS_DIR) ===";
endef

# Main lint target (with fixes)
lint:
	@echo "=== Linting mountaineer-auth ==="
	$(call run_biome)
	$(call run_ruff,$(PKG_DIR))
	$(call run_ty,$(PKG_DIR))
	@echo "\n=== All linters completed successfully ==="

# CI lint target (validation only, no fixes)
ci-lint:
	@echo "=== CI Linting mountaineer-auth (validation only) ==="
	$(call run_biome_ci)
	$(call run_ruff_ci,$(PKG_DIR))
	$(call run_ty,$(PKG_DIR))
	@echo "\n=== All linters completed successfully ==="

# Tool-specific targets
lint-biome:
	@echo "=== Running biome ==="
	$(call run_biome)

lint-ruff:
	@echo "=== Running ruff ==="
	$(call run_ruff,$(PKG_DIR))

lint-ty:
	@echo "=== Running ty ==="
	$(call run_ty,$(PKG_DIR))

# Test target
test:
	@set -e; \
	cleanup() { docker compose -f $(TEST_COMPOSE_FILE) down -v --remove-orphans >/dev/null 2>&1 || true; }; \
	trap cleanup EXIT; \
	echo "=== Starting test database ==="; \
	docker compose -f $(TEST_COMPOSE_FILE) up -d --wait; \
	echo "=== Running tests ==="; \
	uv run pytest -vvv $(PKG_DIR)
	@echo "=== Tests completed successfully ==="

test-db-up:
	@echo "=== Starting test database ==="
	docker compose -f $(TEST_COMPOSE_FILE) up -d --wait
	@echo "=== Test database ready ==="

test-db-down:
	@echo "=== Stopping test database ==="
	docker compose -f $(TEST_COMPOSE_FILE) down -v --remove-orphans
	@echo "=== Test database stopped ==="

# Show help
help:
	@echo "Available targets:"
	@echo " "
	@echo "  lint            - Run all linters (with fixes)"
	@echo "  ci-lint         - Run all linters (validation only, no fixes)"
	@echo "  lint-biome      - Run biome on the frontend auth/email views"
	@echo "  lint-ruff       - Run ruff only (with fixes)"
	@echo "  lint-ty         - Run ty type checker only"
	@echo " "
	@echo "  test            - Start the test database, run tests, then tear it down"
	@echo "  test-db-up      - Start the Postgres test database"
	@echo "  test-db-down    - Stop and remove the Postgres test database"
