.DEFAULT_GOAL := help

# --- Load Config from pyproject.toml ---
export
-include .env.make

# --- Profiles management ---
# Usage: make up p=rag,meili
p ?= full
PROFILES_PARAM	:= COMPOSE_PROFILES=$(p)

# --- Commands ---
DOCKER_COMPOSE	:= docker compose \
					--env-file .env.alfred \
					--env-file .env.secrets \
					--env-file .env.make
DOCKER_BUILD	:= DOCKER_BUILDKIT=1 docker build \
					--build-arg PYTHON_VERSION=$(PYTHON_VERSION) \
					--build-arg UV_VERSION=$(UV_VERSION)

# --- Phony ---
.PHONY: bootstrap up down restart logs ps shell build build-test install \
	update install-hooks test coverage lint format clean major minor patch help

# --- Setup ---
.env.alfred .env.librechat .env.secrets .env.make:
	@echo "Initializing environment..."
	@uv run python scripts/bootstrap.py \
		&& echo "✓ Environment ready" \
		|| (echo "✗ Environment setup failed" && exit 1)

bootstrap: .env.alfred .env.librechat .env.secrets .env.make

# --- Docker ---
up: .env.alfred .env.secrets
	@echo "Starting containers with profiles: [full]..."
	@$(PROFILES_PARAM) $(DOCKER_COMPOSE) up -d --remove-orphans \
		&& echo "✓ Containers started" \
		|| (echo "✗ Failed to start containers" && exit 1)

down:
	@echo "Stopping containers..."
	@$(PROFILES_PARAM) $(DOCKER_COMPOSE) down \
		&& echo "✓ Containers stopped" \
		|| (echo "✗ Failed to stop containers" && exit 1)

restart:
	@echo "Restarting containers..."
	@$(PROFILES_PARAM) $(DOCKER_COMPOSE) restart \
		&& echo "✓ Containers restarted" \
		|| (echo "✗ Failed to restart containers" && exit 1)

logs:
	@echo "Following logs (Ctrl+C to exit)..."
	@$(PROFILES_PARAM) $(DOCKER_COMPOSE) logs -f

ps:
	@echo "Container status:"
	@$(PROFILES_PARAM) $(DOCKER_COMPOSE) ps

shell:
	@echo "Opening shell in $(SERVICE_NAME)..."
	@$(DOCKER_COMPOSE) exec $(SERVICE_NAME) /bin/bash

# --- Build ---
build: .env.make
	@echo "Building image $(IMAGE_NAME):latest ..."
	@$(DOCKER_BUILD) -t $(IMAGE_NAME):latest . \
		&& echo "✓ Build complete" \
		|| (echo "✗ Build failed" && exit 1)

build-test: .env.make
	@echo "Building test image $(IMAGE_NAME):test..."
	@$(DOCKER_BUILD) --target test -t $(IMAGE_NAME):test . \
		&& echo "✓ Test image built" \
		|| (echo "✗ Build failed" && exit 1)

# --- Dependencies ---
install:
	@echo "Installing dependencies with uv..."
	@uv install \
		&& echo "✓ Dependencies installed" \
		|| (echo "✗ Installation failed" && exit 1)

install-hooks:
	@echo "Installing pre-commit hooks..."
	@uv run pre-commit install \
		&& echo "✓ Hooks installed" \
		|| (echo "✗ Hook installation failed" && exit 1)

update:
	@echo "Updating dependencies with uv..."
	@uv update \
		&& echo "✓ Dependencies updated" \
		|| (echo "✗ Update failed" && exit 1)

# --- Quality ---
test:
	@echo "Running tests..."
	@uv run pytest \
		&& echo "✓ Tests passed" \
		|| (echo "✗ Tests failed" && exit 1)

coverage:
	@echo "Running tests with coverage..."
	@uv run pytest --cov=. --cov-report=html --cov-report=term \
		&& echo "✓ Coverage report generated" \
		|| (echo "✗ Coverage failed" && exit 1)

lint:
	@echo "Linting code..."
	@uv run ruff check --fix . \
		&& echo "✓ Linting complete" \
		|| (echo "✗ Linting failed" && exit 1)

format:
	@echo "Formatting code..."
	@uv run ruff format . && uv run ruff check --fix . \
		&& echo "✓ Code formatted" \
		|| (echo "✗ Formatting failed" && exit 1)

clean:
	@echo "Cleaning build artifacts..."
	@rm -rf .ruff_cache __pycache__ .pytest_cache htmlcov .coverage
	@find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
	@echo "✓ Cleanup complete"

# --- Versioning ---
major minor patch: _check-main
	@echo "Bumping $@ version..."
	@uv run bump-my-version bump $@ \
		&& echo "✓ Version bumped" \
		|| (echo "✗ Version bump failed" && exit 1)

	@echo "Pushing tags..."
	@git push --tags \
		&& echo "✓ Tags pushed" \
		|| (echo "✗ Push failed" && exit 1)

# CI/CD helpers
_ci-dump-config:
	@echo "image_name=$(IMAGE_NAME)"
	@echo "python_version=$(PYTHON_VERSION)"
	@echo "uv_version=$(UV_VERSION)"
	@echo "service_name=$(SERVICE_NAME)"

_ci-run-tests:build-test
	@echo "Running tests in Docker..."
	docker run --rm \
		-e DEEPSEEK_API_KEY \
		-e TMDB_API_KEY \
		-e QBITTORRENT_URL \
		$(IMAGE_NAME):test pytest
	@echo "✓ Tests passed."

_check-main:
	@test "$$(git rev-parse --abbrev-ref HEAD)" = "main" \
		|| (echo "✗ ERROR: Not on main branch" && exit 1)

# --- Help ---
help:
	@echo "Cleverly Crafted Unawareness - Management Commands"
	@echo ""
	@echo "Usage: make [target] [p=profile1,profile2]"
	@echo ""
	@echo "Setup:"
	@echo "  bootstrap   Generate .env.alfred, .env.librechat, .env.secrets and .env.make"
	@echo ""
	@echo "Docker:"
	@echo "  up          Start containers (default profile: core)"
	@echo "              Example: make up p=rag,meili"
	@echo "  down        Stop all containers"
	@echo "  restart     Restart containers (supports p=...)"
	@echo "  logs        Follow logs (supports p=...)"
	@echo "  ps          Status of containers"
	@echo "  shell       Open bash in the core container"
	@echo "  build       Build the production Docker image"
	@echo ""
	@echo "Dev & Quality:"
	@echo "  setup       Bootstrap .env and security keys"
	@echo "  install     Install dependencies via uv"
	@echo "  test        Run pytest suite"
	@echo "  coverage    Run tests and generate HTML report"
	@echo "  lint/format Quality and style checks"
	@echo ""
	@echo "Release:"
	@echo "  major|minor|patch  Bump version and push tags (main branch only)"
