#!/usr/bin/env bash
set -euo pipefail

cd "$(dirname "$0")"

HOST="${HOST:-127.0.0.1}"
PORT="${PORT:-8100}"
DEFAULT_ROOT="/home/tom/Downloads/2026/5"

if [[ -z "${INVOICE_ROOT:-}" ]]; then
  if [[ -d "$DEFAULT_ROOT" ]]; then
    export INVOICE_ROOT="$DEFAULT_ROOT"
  else
    export INVOICE_ROOT="$PWD"
  fi
fi

if [[ -z "${REPORT_DIR:-}" ]]; then
  if [[ -d "$PWD/.state" ]]; then
    export REPORT_DIR="$PWD/.state"
  elif [[ -d "$INVOICE_ROOT/.state" ]]; then
    export REPORT_DIR="$INVOICE_ROOT/.state"
  else
    export REPORT_DIR="$PWD/.state"
  fi
fi

export DELETE_MODE="${DELETE_MODE:-trash}"

if ! command -v php >/dev/null 2>&1; then
  echo "php not found; install php-cli first" >&2
  exit 1
fi

echo "Invoice review UI"
echo "  URL:         http://$HOST:$PORT"
echo "  INVOICE_ROOT: $INVOICE_ROOT"
echo "  REPORT_DIR:   $REPORT_DIR"
echo "  DELETE_MODE:  $DELETE_MODE"
echo
echo "DELETE_MODE=trash moves files to \$INVOICE_ROOT/.deleted"
echo "DELETE_MODE=unlink deletes permanently"

exec php -S "$HOST:$PORT" -t "$PWD" index.php
