#!/bin/bash
#############################################################################
#                                                                           #
# This file is part of the "ubuntu" module of the otoolbox project.         #
#                                                                           #
# This script is open-source and intended for automation purposes.          #
# It is distributed in the hope that it will be useful, but WITHOUT         #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        #
# or FITNESS FOR A PARTICULAR PURPOSE.                                      #
#                                                                           #
# Use of this script is entirely at your own risk.                          #
#                                                                           #
# Copyright (c) The otoolbox contributors.                                  #
#############################################################################
SCRIPT_DIR="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
source "$SCRIPT_DIR/bulk-common"
cd "$WORKSPACE_ROOT" || { echo "Error: failed to change directory to $WORKSPACE_ROOT" >&2; exit 1; }



set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ODOO_BIN="${WORKSPACE_ROOT}/odoo/odoo/odoo-bin"

REPO_PATH=""
INIT_MODULES=""
TEST_MODULES=""
DB_SUFFIX=""
DRY_RUN=false

usage() {
    cat <<'EOF'
Interactive Odoo test runner by repository

Usage:
  run-odoo-tests-by-repo.sh [options]

Options:
  --repo PATH           Repository path (absolute or relative to workspace)
  --init MODULES        Comma-separated list for --init (moduleList)
  --test-tags MODULES   Comma-separated list for --test-tag (moduleListTest)
  --db-suffix VALUE     Database suffix for odooiottest<VALUE>
  --dry-run             Print command only, do not execute
  -h, --help            Show this help message

Notes:
  - Activates: ${workspace}/.venv/bin/activate
  - If --repo is not provided, script lists git repositories and asks selection.
EOF
}

while [[ $# -gt 0 ]]; do
    case "$1" in
        --repo)
            REPO_PATH="$2"
            shift 2
            ;;
        --init)
            INIT_MODULES="$2"
            shift 2
            ;;
        --test-tags)
            TEST_MODULES="$2"
            shift 2
            ;;
        --db-suffix)
            DB_SUFFIX="$2"
            shift 2
            ;;
        --dry-run)
            DRY_RUN=true
            shift
            ;;
        -h|--help)
            usage
            exit 0
            ;;
        *)
            echo "Unknown option: $1" >&2
            usage
            exit 1
            ;;
    esac
done

if [[ ! -x "${ODOO_BIN}" ]]; then
    echo "odoo-bin not found or not executable: ${ODOO_BIN}" >&2
    exit 1
fi

resolve_repo_path() {
    local input_path="$1"
    if [[ -z "${input_path}" ]]; then
        echo ""
        return
    fi
    if [[ -d "${input_path}/.git" ]]; then
        cd "${input_path}" && pwd
        return
    fi
    if [[ -d "${WORKSPACE_ROOT}/${input_path}/.git" ]]; then
        cd "${WORKSPACE_ROOT}/${input_path}" && pwd
        return
    fi
    echo ""
}

choose_repo_interactive() {
    mapfile -t repos < <(
        find "${WORKSPACE_ROOT}" \
            -path "${WORKSPACE_ROOT}/.venv" -prune -o \
            -type d -name .git -print \
        | sed 's|/.git$||' \
        | sort
    )

    if [[ ${#repos[@]} -eq 0 ]]; then
        echo "No git repositories found under ${WORKSPACE_ROOT}" >&2
        exit 1
    fi

    echo "Available repositories:"
    local i=1
    for repo in "${repos[@]}"; do
        printf '  %2d) %s\n' "${i}" "${repo#${WORKSPACE_ROOT}/}"
        ((i++))
    done

    local choice
    read -r -p "Select repository number: " choice
    if [[ ! "${choice}" =~ ^[0-9]+$ ]] || (( choice < 1 || choice > ${#repos[@]} )); then
        echo "Invalid repository selection." >&2
        exit 1
    fi

    echo "${repos[$((choice - 1))]}"
}

collect_repo_modules() {
    local repo="$1"
    mapfile -t addon_paths < <(
        find "${repo}" -mindepth 1 -maxdepth 3 -type f -name __manifest__.py \
            | sed 's|/__manifest__.py$||' \
            | sort
    )

    if [[ ${#addon_paths[@]} -eq 0 ]]; then
        echo ""
        return
    fi

    local modules=()
    local addon
    for addon in "${addon_paths[@]}"; do
        modules+=("$(basename "${addon}")")
    done

    local joined
    IFS=, read -r -a _dummy <<< ""
    joined="$(IFS=,; echo "${modules[*]}")"
    echo "${joined}"
}

get_addons_path() {
    local addons_path
    addons_path="$(WORKSPACE_ROOT="${WORKSPACE_ROOT}" ${WORKSPACE_ROOT}/.venv/bin/python - <<'PY'
import os
import json
from pathlib import Path

root = Path(os.environ["WORKSPACE_ROOT"])
workspace_file = root / "odoo-dev.code-workspace"
data = json.loads(workspace_file.read_text())
addons = data["settings"]["odoo"]["addons"]
print(addons.replace("${workspaceFolder}", str(root)))
PY
)"
    echo "${addons_path}"
}

SELECTED_REPO="$(resolve_repo_path "${REPO_PATH}")"
if [[ -z "${SELECTED_REPO}" ]]; then
    SELECTED_REPO="$(choose_repo_interactive)"
fi

echo "Selected repository: ${SELECTED_REPO#${WORKSPACE_ROOT}/}"

DEFAULT_MODULES="$(collect_repo_modules "${SELECTED_REPO}")"
if [[ -z "${DEFAULT_MODULES}" ]]; then
    echo "No module (__manifest__.py) found in selected repository." >&2
    exit 1
fi

if [[ -z "${INIT_MODULES}" ]]; then
    echo "Detected modules in repository:"
    echo "  ${DEFAULT_MODULES}"
    read -r -p "Module List for --init : " INIT_MODULES
    INIT_MODULES="${INIT_MODULES:-${DEFAULT_MODULES}}"
fi

if [[ -z "${TEST_MODULES}" ]]; then
    read -r -p "Module List to use as test tag --test-tag : " TEST_MODULES
    TEST_MODULES="${TEST_MODULES:-${INIT_MODULES}}"
fi

if [[ -z "${DB_SUFFIX}" ]]; then
    DB_SUFFIX="$(date +%s)"
fi

ADDONS_PATH="$(get_addons_path)"
DB_NAME="odooiottest${DB_SUFFIX}"

CMD=(
    "${WORKSPACE_ROOT}/.venv/bin/python" "${ODOO_BIN}"
    "--db_host" "localhost"
    "--db_password" "odoo"
    "--db_user" "odoo"
    "--database" "${DB_NAME}"
    "--addons-path" "${ADDONS_PATH}"
    "--init" "${INIT_MODULES}"
    "--test-tag" "${TEST_MODULES}"
    "--stop-after-init"
    "--with-demo"
)

echo "Running Odoo tests with:"
printf '  %q' "${CMD[@]}"
echo

if [[ "${DRY_RUN}" == true ]]; then
    echo "Dry-run enabled. Command not executed."
    exit 0
fi

"${CMD[@]}"
