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

BASE_URL="https://accounting.vazin.online"
TARGET_PATH="/app"

for arg in "$@"; do
  case "$arg" in
    --personal) TARGET_PATH="/app/personal" ;;
    --business) TARGET_PATH="/app?section=overview" ;;
    --ledger)   TARGET_PATH="/app?section=ledger" ;;
    --sales)    TARGET_PATH="/app?section=sales" ;;
    --treasury) TARGET_PATH="/app?section=treasury" ;;
    --reports)  TARGET_PATH="/app?section=reports" ;;
    --settings) TARGET_PATH="/app?section=settings" ;;
    --help|-h)
      echo "Vazin Cloud Accounting Desktop Client v1.0.0"
      echo "Usage: vazin-accounting [--business|--personal|--ledger|--sales|--treasury|--reports|--settings]"
      exit 0
      ;;
  esac
done

URL="${BASE_URL}${TARGET_PATH}"
PROFILE_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/vazin-accounting/browser-profile"
mkdir -p "$PROFILE_DIR"

for bin in google-chrome-stable google-chrome chromium-browser chromium brave-browser microsoft-edge; do
  if command -v "$bin" >/dev/null 2>&1; then
    exec "$bin" --app="$URL" --user-data-dir="$PROFILE_DIR" --class="VazinAccounting" --window-size=1380,880 >/dev/null 2>&1 &
    exit 0
  fi
done

if command -v xdg-open >/dev/null 2>&1; then
  exec xdg-open "$URL"
fi

echo "Open in browser: $URL"
