.DEFAULT_GOAL := help

# --- Config ---
export IMAGE_NAME			:= alfred_media_organizer
export LIBRECHAT_VERSION 	:= v0.8.1
export PYTHON_VERSION		:= 3.14.2
export PYTHON_VERSION_SHORT	:= 3.14
export RAG_VERSION 			:= v0.7.0
export RUNNER				:= poetry
export SERVICE_NAME			:= alfred

# --- Commands ---
CLI				:= python3 cli.py
DOCKER_COMPOSE	:= docker compose
DOCKER_BUILD	:= docker build \
					--build-arg PYTHON_VERSION=$(PYTHON_VERSION) \
					--build-arg PYTHON_VERSION_SHORT=$(PYTHON_VERSION_SHORT) \
					--build-arg RUNNER=$(RUNNER)

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

# --- Setup ---
setup:
	@echo "Initializing environment..."
	@$(CLI) setup \
		&&  echo "✓ Environment ready" \
		|| (echo "✗ Setup failed" && exit 1)

status:
	@$(CLI) status

check:
	@$(CLI) check

# --- Docker ---
up: check
	@echo "Starting containers..."
	@$(DOCKER_COMPOSE) up -d --remove-orphans \
		&& echo "✓ Containers started" \
		|| (echo "✗ Failed to start containers" && exit 1)

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

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

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

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

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

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

build-test: check
	@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 $(RUNNER)..."
	@$(RUNNER) install \
		&& echo "✓ Dependencies installed" \
		|| (echo "✗ Installation failed" && exit 1)

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

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

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

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

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

format:
	@echo "Formatting code..."
	@$(RUNNER) run ruff format . && $(RUNNER) 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"

prune:
	@echo "Pruning Docker system..."
	@docker system prune -af \
		&& echo "✓ Docker pruned" \
		|| (echo "✗ Prune failed" && exit 1)

# --- Versioning ---
major minor patch: _check-main
	@echo "Bumping $@ version..."
	@$(RUNNER) 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-dump-config:
	@echo "image_name=$(IMAGE_NAME)"
	@echo "python_version=$(PYTHON_VERSION)"
	@echo "python_version_short=$(PYTHON_VERSION_SHORT)"
	@echo "runner=$(RUNNER)"
	@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 "Usage: make [target]"
	@echo ""
	@echo "Setup:"
	@echo "  setup       Initialize .env"
	@echo "  status      Show project status"
	@echo ""
	@echo "Docker:"
	@echo "  up          Start containers"
	@echo "  down        Stop containers"
	@echo "  restart     Restart containers"
	@echo "  logs        Follow logs"
	@echo "  ps          Container status"
	@echo "  shell       Shell into container"
	@echo "  build       Build image"
	@echo ""
	@echo "Dev:"
	@echo "  install     Install dependencies"
	@echo "  update      Update dependencies"
	@echo "  test        Run tests"
	@echo "  coverage    Run tests with coverage"
	@echo "  lint        Lint code"
	@echo "  format      Format code"
	@echo "  clean       Clean artifacts"
	@echo ""
	@echo "Release:"
	@echo "  patch       Bump patch version"
	@echo "  minor       Bump minor version"
	@echo "  major       Bump major version"