#!/bin/bash
# IronDome — instalação em Mac novo
set -e
cd "$(dirname "$0")"

echo "=== IRONDOME — INSTALACAO ==="

# 1. Verifica Homebrew
if ! command -v brew > /dev/null; then
  echo "Homebrew nao encontrado. Instalando..."
  /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi

# 2. Python 3.12
if ! command -v python3.12 > /dev/null; then
  echo "Instalando Python 3.12..."
  brew install python@3.12
fi

# 3. Dependencia Python
echo "Instalando websocket-client..."
python3.12 -m pip install --user --break-system-packages --quiet websocket-client

# 4. Brave Browser
if [ ! -d "/Applications/Brave Browser.app" ]; then
  echo "Brave Browser nao encontrado. Instalando via brew cask..."
  brew install --cask brave-browser
fi

# 5. Cert self-signed pro backend HTTPS
if [ ! -f cert.pem ] || [ ! -f key.pem ]; then
  echo "Gerando certificado TLS local..."
  openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -sha256 -days 365 -nodes \
    -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost,IP:127.0.0.1" 2>/dev/null
fi

# 6. Permissoes
chmod +x start.sh stop.sh install.sh

echo ""
echo "=== INSTALACAO CONCLUIDA ==="
echo ""
echo "PROXIMOS PASSOS:"
echo "  1. Rode: ./start.sh"
echo "  2. No Brave que abrir, logue na casa de apostas"
echo "  3. Abra o Aviator e clique JOGAR"
echo "  4. Dashboard: https://localhost:8766/"
echo ""
echo "Pra desligar: ./stop.sh"
