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

INSTALLER_PATH="/tmp/mirza_install.sh"
DEFAULT_INSTALLER_URL="https://raw.githubusercontent.com/mahdiMGF2/mirza_pro/main/install.sh"

usage() {
  cat <<'EOF'
Mirza Easy Installer (Bot + Panel)

Usage:
  bash easy-installer.sh [options]

Options:
  --domain <domain>            Domain name (example.com)
  --bot-token <token>          Telegram bot token
  --chat-id <id>               Telegram admin chat ID
  --bot-username <username>    Telegram bot username (without @)
  --db-user <username>         Optional DB username (blank = installer default)
  --db-pass <password>         Optional DB password (blank = installer default)
  --installer-url <url>        Optional custom install.sh URL
  -h, --help                   Show this help

Example:
  bash easy-installer.sh \
    --domain example.com \
    --bot-token 123456789:ABCDEF1234567890ABCDEF1234567890ABC \
    --chat-id 123456789 \
    --bot-username myvpn_bot
EOF
}

require_root() {
  if [[ "${EUID}" -ne 0 ]]; then
    echo "[ERROR] Please run as root."
    exit 1
  fi
}

require_cmd() {
  local cmd="$1"
  if ! command -v "$cmd" >/dev/null 2>&1; then
    echo "[ERROR] Missing required command: $cmd"
    exit 1
  fi
}

validate_domain() {
  local value="$1"
  [[ "$value" =~ ^[a-zA-Z0-9.-]+$ ]]
}

validate_token() {
  local value="$1"
  [[ "$value" =~ ^[0-9]{8,10}:[a-zA-Z0-9_-]{35}$ ]]
}

validate_chat_id() {
  local value="$1"
  [[ "$value" =~ ^-?[0-9]+$ ]]
}

DOMAIN=""
BOT_TOKEN=""
CHAT_ID=""
BOT_USERNAME=""
DB_USER=""
DB_PASS=""
INSTALLER_URL="$DEFAULT_INSTALLER_URL"

while [[ $# -gt 0 ]]; do
  case "$1" in
    --domain)
      DOMAIN="${2:-}"
      shift 2
      ;;
    --bot-token)
      BOT_TOKEN="${2:-}"
      shift 2
      ;;
    --chat-id)
      CHAT_ID="${2:-}"
      shift 2
      ;;
    --bot-username)
      BOT_USERNAME="${2:-}"
      shift 2
      ;;
    --db-user)
      DB_USER="${2:-}"
      shift 2
      ;;
    --db-pass)
      DB_PASS="${2:-}"
      shift 2
      ;;
    --installer-url)
      INSTALLER_URL="${2:-}"
      shift 2
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      echo "[ERROR] Unknown option: $1"
      usage
      exit 1
      ;;
  esac
done

require_root
require_cmd curl
require_cmd bash

if [[ -z "$DOMAIN" ]]; then
  read -r -p "Domain: " DOMAIN
fi
if ! validate_domain "$DOMAIN"; then
  echo "[ERROR] Invalid domain format."
  exit 1
fi

if [[ -z "$BOT_TOKEN" ]]; then
  read -r -p "Bot token: " BOT_TOKEN
fi
if ! validate_token "$BOT_TOKEN"; then
  echo "[ERROR] Invalid bot token format."
  exit 1
fi

if [[ -z "$CHAT_ID" ]]; then
  read -r -p "Admin chat id: " CHAT_ID
fi
if ! validate_chat_id "$CHAT_ID"; then
  echo "[ERROR] Invalid chat ID format."
  exit 1
fi

if [[ -z "$BOT_USERNAME" ]]; then
  read -r -p "Bot username (without @): " BOT_USERNAME
fi
if [[ -z "$BOT_USERNAME" ]]; then
  echo "[ERROR] Bot username can not be empty."
  exit 1
fi

if [[ -z "$DB_USER" ]]; then
  read -r -p "DB username (optional, Enter for auto): " DB_USER
fi
if [[ -z "$DB_PASS" ]]; then
  read -r -p "DB password (optional, Enter for auto): " DB_PASS
fi

echo "[INFO] Downloading installer..."
curl -fsSL "$INSTALLER_URL" -o "$INSTALLER_PATH"
chmod +x "$INSTALLER_PATH"

echo "[INFO] Starting one-shot installation (bot + panel)..."
printf '1\n%s\n%s\n%s\n%s\n%s\n%s\n' \
  "$DOMAIN" \
  "$BOT_TOKEN" \
  "$CHAT_ID" \
  "$BOT_USERNAME" \
  "$DB_USER" \
  "$DB_PASS" | bash "$INSTALLER_PATH"

echo "[DONE] Installation flow finished."
