#!/bin/bash

export E2E_ADMIN_USER=mb
export E2E_ADMIN_PASSWORD="monse$(date +%H)"

run_all() {
  npm test
}

run_spec() {
  SPEC="$1"

  if [ ! -f "$SPEC" ]; then
    echo "No existe el test: $SPEC"
    exit 1
  fi

  npx playwright test "$SPEC"
}

run_grep() {
  PATTERN="$1"

  if [ -z "$PATTERN" ]; then
    echo "Patrón vacío."
    exit 1
  fi

  npx playwright test --grep "$PATTERN"
}

find_specs() {
  QUERY="$1"

  find tests -type f -name "*.spec.ts" | grep -i "$QUERY"
}

echo "========================================"
echo "Tests E2E Playwright"
echo "Usuario: $E2E_ADMIN_USER"
echo "========================================"
echo "1) Todos los tests"
echo "2) Tests por archivo .spec.ts"
echo "3) Tests por nombre/patrón (--grep)"
echo "4) Sugerir test desde plantilla Twig"
echo "5) Solo si hay Twig modificados"
echo "========================================"

read -p "Opción [1-5]: " OPTION

case "$OPTION" in
  1)
    run_all
    ;;

  2)
    echo ""
    echo "Tests disponibles:"
    find tests -type f -name "*.spec.ts"
    echo ""

    read -p "Ruta del .spec.ts: " SPEC
    run_spec "$SPEC"
    ;;

  3)
    read -p "Texto del test, describe o título: " PATTERN
    run_grep "$PATTERN"
    ;;

  4)
    read -p "Ruta de la plantilla .twig: " TWIG

    if [ ! -f "$TWIG" ]; then
      echo "No existe la plantilla: $TWIG"
      exit 1
    fi

    DIRNAME=$(basename "$(dirname "$TWIG")")
    BASENAME=$(basename "$TWIG" .html.twig)

    echo ""
    echo "Plantilla:"
    echo "$TWIG"
    echo ""
    echo "Buscando specs relacionados con:"
    echo "$DIRNAME"
    echo "$BASENAME"
    echo ""

    MATCHES=$(find_specs "$DIRNAME")

    if [ -z "$MATCHES" ]; then
      MATCHES=$(find_specs "$BASENAME")
    fi

    if [ -z "$MATCHES" ]; then
      echo "No encontré .spec.ts relacionado automáticamente."
      echo ""
      echo "Specs disponibles:"
      find tests -type f -name "*.spec.ts"
      echo ""
      read -p "Ingresá la ruta del .spec.ts a ejecutar: " SPEC
      run_spec "$SPEC"
    else
      echo "Specs encontrados:"
      echo "$MATCHES"
      echo ""

      COUNT=$(echo "$MATCHES" | wc -l | tr -d ' ')

      if [ "$COUNT" = "1" ]; then
        SPEC="$MATCHES"
        echo "Ejecutando: $SPEC"
        run_spec "$SPEC"
      else
        echo "Hay varios posibles."
        read -p "Copiá la ruta exacta del .spec.ts a ejecutar: " SPEC
        run_spec "$SPEC"
      fi
    fi
    ;;

  5)
    CHANGED_TWIG=$(git diff --name-only HEAD | grep '\.twig$')

    if [ -z "$CHANGED_TWIG" ]; then
      echo "No hay plantillas Twig modificadas."
      exit 0
    fi

    echo ""
    echo "Plantillas Twig modificadas:"
    echo "$CHANGED_TWIG"
    echo ""

    echo "Specs disponibles:"
    find tests -type f -name "*.spec.ts"
    echo ""

    read -p "Ingresá .spec.ts a ejecutar o texto para --grep: " TARGET

    if [ -f "$TARGET" ]; then
      run_spec "$TARGET"
    else
      run_grep "$TARGET"
    fi
    ;;

  *)
    echo "Opción inválida."
    exit 1
    ;;
esac

