#!/usr/bin/env bash
# gradle_screenshot_test — Corre screenshot tests Roborazzi (JVM, no necesita emulador)

gradle_screenshot_test() {
    local project_dir="${1:?project_dir requerido}"
    local module="${2:-app}"
    local record_flag="${3:-}"

    local SCRIPT_DIR
    SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
    source "$SCRIPT_DIR/gradle_run.sh"

    local task
    if [[ "$record_flag" == "--record" ]]; then
        task=":${module}:recordRoborazziDebug"
    else
        task=":${module}:verifyRoborazziDebug"
    fi

    local goldens_dir="${project_dir}/${module}/src/test/snapshots"
    local diff_dir="${project_dir}/${module}/build/outputs/roborazzi"

    gradle_run "$project_dir" "$task"
    local exit_code=$?

    if [[ "$record_flag" == "--record" ]]; then
        echo "RECORDED: ${goldens_dir}"
    elif [[ $exit_code -ne 0 ]]; then
        echo "DIFF: ${diff_dir}"
    fi

    return $exit_code
}

# Source-able y ejecutable directo
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
    gradle_screenshot_test "$@"
fi
