@echo off
chcp 65001 >nul
title Stocknews - Tailscale uzak kopru
cd /d "%~dp0"
where node >nul 2>nul
if errorlevel 1 (
  echo Node.js 18 veya ustunu kurun: https://nodejs.org
  pause
  exit /b 1
)
where tailscale >nul 2>nul
if errorlevel 1 (
  echo Tailscale kurun ve giris yapin: https://tailscale.com/download
  pause
  exit /b 1
)
tailscale status >nul 2>nul
if errorlevel 1 (
  echo Tailscale bagli degil. Uygulamadan baglanin.
  pause
  exit /b 1
)
if not defined STOCKNEWS_BRIDGE_PORT set STOCKNEWS_BRIDGE_PORT=8766
curl --fail --silent --max-time 2 http://127.0.0.1:%STOCKNEWS_BRIDGE_PORT%/version >nul 2>nul
if not errorlevel 1 (
  echo Bu portta zaten kopru var. Once yerel kopru penceresini kapatin.
  pause
  exit /b 1
)
echo Tailscale Serve - yalniz ozel tailnet agi. Funnel acilmaz.
echo Stocknews HTTPS portu 8443. Metin Duzenleyici'nin 443 yonlendirmesi korunur.
tailscale serve --bg --https=8443 http://127.0.0.1:%STOCKNEWS_BRIDGE_PORT%
if errorlevel 1 (
  echo MagicDNS ve HTTPS sertifikalarini etkinlestirin: https://login.tailscale.com/admin/dns
  pause
  exit /b 1
)
echo Uzak adres:
powershell -NoProfile -Command "$stocknewsTs = tailscale status --json | ConvertFrom-Json; 'https://' + $stocknewsTs.Self.DNSName.TrimEnd('.') + ':8443'"
echo Bu adresi Stocknews - AI Settings - Tailscale Serve adresi alanina girin.
echo Telefonda da Tailscale acik ve ayni agda olmali. Stocknews hesabiniza giris yapin.
echo Kapatmak icin: tailscale serve --https=8443 off
echo Supabase aktarmayi da ayni pencerede acmak icin: stocknews-bridge-remote.bat --relay
node "%~dp0stocknews-bridge.js" --remote %*
echo Kopru kapandi. Serve ayarini kapatmak icin: tailscale serve --https=8443 off
pause
