#!/usr/bin/env bash

set -euo pipefail

source "$(cd "$(dirname "$0")" && pwd)/common.sh"

if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
  cat <<'EOF'
Usage: ./scripts/dev-desktop [cargo tauri dev args...]

Starts the MCPMate Tauri desktop app in dev mode.
The board dev server is started by Tauri's beforeDevCommand using the configured host/port.
The script enables inspector and debug logging, and keeps all logs in the current terminal.
Additional arguments are forwarded to cargo tauri dev.
EOF
  exit 0
fi

require_command cargo
require_command bun

TAURI_DIR="$(project_dir desktop/src-tauri)"

print_section "backend dependency check"
BACKEND_DIR="$(project_dir backend)"
printf 'Ensuring backend dependencies are up-to-date...\n'
run_in "$BACKEND_DIR" cargo check --quiet

print_section "desktop tauri dev"
printf 'MCPMATE_TAURI_ENABLE_INSPECTOR=%s\n' "${MCPMATE_TAURI_ENABLE_INSPECTOR:-1}"
printf 'MCPMATE_TAURI_LOG=%s\n' "${MCPMATE_TAURI_LOG:-debug}"
printf 'RUST_LOG=%s\n' "${RUST_LOG:-mcpmate=debug,mcpmate_tauri=debug,tauri=info}"

run_in "$TAURI_DIR" env \
  MCPMATE_TAURI_ENABLE_INSPECTOR="${MCPMATE_TAURI_ENABLE_INSPECTOR:-1}" \
  MCPMATE_TAURI_LOG="${MCPMATE_TAURI_LOG:-debug}" \
  RUST_LOG="${RUST_LOG:-mcpmate=debug,mcpmate_tauri=debug,tauri=info}" \
  cargo tauri dev --verbose "$@"
