#!/usr/bin/env bash

_common_setup() {
    export PROJECT_ROOT="$(dirname "${BASH_SOURCE[0]}")/../.."
    load "$PROJECT_ROOT/test/test_helper/bats-support/load"
    load "$PROJECT_ROOT/test/test_helper/bats-assert/load"
    load "$PROJECT_ROOT/test/test_helper/bats-file/load"
    load "$PROJECT_ROOT/test/test_helper/cache_setup"

    export PKL_PATH="$PROJECT_ROOT/pkl"

    # Create a temporary directory for each test
    TEST_TEMP_DIR="$(temp_make)"
    mkdir -p "$TEST_TEMP_DIR/src/proj"
    cd "$TEST_TEMP_DIR/src/proj"

    # Initialize a git repository
    export GIT_CONFIG_NOSYSTEM=1
    export HK_JOBS=2
    export MISE_INSTALLS_DIR="${MISE_INSTALLS_DIR:-$HOME/.local/share/mise/installs}"
    export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$MISE_INSTALLS_DIR/.cache}"
    export XDG_DATA_HOME="${XDG_DATA_HOME:-$MISE_INSTALLS_DIR/.local/share}"
    export RUSTUP_HOME="${RUSTUP_HOME:-$HOME/.rustup}"
    export HOME="$TEST_TEMP_DIR"

    # Install tool user configs into the temp HOME so tools behave correctly in tests.
    export XDG_CONFIG_HOME="$HOME/.config"
    mkdir -p "$XDG_CONFIG_HOME"
    cp -r "$PROJECT_ROOT/test/test_helper/xdg_config/." "$XDG_CONFIG_HOME/"

    git config --global init.defaultBranch main

    # Only set user config if not already set (to avoid overriding existing config)
    if ! git config --global user.email >/dev/null 2>&1; then
        git config --global user.email "test@example.com"
    fi
    if ! git config --global user.name >/dev/null 2>&1; then
        git config --global user.name "Test User"
    fi

    git init .

    # Add hk to PATH (assuming it's installed)
    # Use CARGO_TARGET_DIR if set (e.g., by mise), otherwise use local target
    if [ -n "$CARGO_TARGET_DIR" ]; then
        PATH="$CARGO_TARGET_DIR/debug:$PATH"
    else
        PATH="$(dirname $BATS_TEST_DIRNAME)/target/debug:$PATH"
    fi

    # Enable test cache by default for better performance
    # Individual tests can override this by calling _disable_test_cache
    _enable_test_cache
}

_common_teardown() {
    chmod -R u+w "$TEST_TEMP_DIR"
    temp_del "$TEST_TEMP_DIR"
}
