#!/bin/bash

# script/lint: Run linting tools and apply formatting
#
# Runs Ruff format and Ruff check with auto-fix enabled. Automatically cleans up
# any accidental package installations after running.
#
# Usage:
#   ./script/lint
#
# Examples:
#   ./script/lint

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$SCRIPT_DIR/.."

# shellcheck source=script/.lib/output.sh
source "$SCRIPT_DIR/.lib/output.sh"

if [[ -z ${VIRTUAL_ENV:-} ]]; then
    log_header "Activating virtual environment"
    # shellcheck source=/dev/null
    if [[ -f "$PWD/.local/ha-venv/bin/activate" ]]; then
        source "$PWD/.local/ha-venv/bin/activate"
    elif [[ -f "$HOME/.local/ha-venv/bin/activate" ]]; then
        source "$HOME/.local/ha-venv/bin/activate"
    else
        log_error "Virtual environment not found in $PWD/.local/ha-venv or $HOME/.local/ha-venv"
        exit 1
    fi
fi

log_header "Running Ruff format"
ruff format .

log_header "Running Ruff check"
ruff check . --fix

# Clean up any accidental package installation from uv run
"$SCRIPT_DIR/clean" --minimal

log_success "Linting completed"
