@echo off
REM ===================================================================
REM  FIKO AI - YAYIN (internete acilan HTTPS kanal)
REM
REM  Tek surec, tek origin, hepsi TLS:
REM     https://fikoyt.com:4200
REM        /       -> Angular arayuzu (statik + SPA fallback)
REM        /api/*  -> REST API
REM        /ws     -> WebSocket (wss://)
REM
REM  Proxy / nginx YOKTUR. Arayuz ve API ayni origin'de oldugu icin
REM  karma icerik (https sayfa -> http istek) ve CORS sorunu olusmaz.
REM
REM  Baslat : start-yayin.bat
REM  Durdur : start-yayin.bat stop
REM
REM  GELISTIRME icin: start-all.bat  (http://localhost:4200 + :8080)
REM  Ikisi AYNI ANDA calismaz (ikisi de 4200'i kullanir).
REM ===================================================================

cd /d "%~dp0"

if /i "%~1"=="stop" goto :durdur

echo.
echo ============================================================
echo   FIKO AI YAYINI hazirlaniyor...
echo ============================================================
echo.
echo   YAYIN ADRESI : https://fikoyt.com:4200      ^<- BURAYA GIT
echo.
echo   On kosullar:
echo     - DNS kaydi olmali : fikoyt.com sunucu IP'nize cozumlenmeli
echo     - Firewall  : 4200 portu disari acik
echo.
echo   IP ile GIRME - sertifika *.fikoyt.com icindir, IP'de
echo   tarayici "sertifika gecersiz" uyarisi verir.
echo   Sertifika gecerlilik: 27.12.2026
echo.

REM --- On koşul: sertifikalar ---
if not exist "cert\server.crt" (
  echo  [HATA] cert\server.crt bulunamadi.
  echo         SSL sertifikalari fiko-ai\cert\ altinda olmalidir.
  exit /b 1
)
if not exist "cert\server.key" (
  echo  [HATA] cert\server.key bulunamadi.
  exit /b 1
)
echo  [1/3] Sertifikalar bulundu.

REM --- On kosul: uretilmis arayuz ---
if not exist "frontend\dist\frontend\browser\index.html" (
  echo.
  echo  [2/3] Arayuz uretiliyor - npm run build, 1-3 dk...
  cd /d "%~dp0frontend"
  call npm run build
  cd /d "%~dp0"
  if not exist "frontend\dist\frontend\browser\index.html" (
    echo  [HATA] Arayuz derlenemedi.
    exit /b 1
  )
) else (
  echo  [2/3] Arayuz zaten derli.
)
echo        -> frontend\dist\frontend\browser

REM --- Port kontrolu YAPILMAZ (bilerek) ---
REM Onceki surumde `netstat`/`Get-NetTCPConnection` ile kontrol ediliyordu ve
REM bu KONTROL betigi oldugu yerde durduruyordu ("Erişim engellendi"), yani
REM sunucu hic baslamiyordu. Kontrol hatasi, yayin hatasindan kotuydu.
REM Zaten gerek yok: Fiber port meşgulse "address already in use" der ve
REM o mesaj kullaniciya daha guvenilir gelir. Burada yalnizca bilgi veriyoruz.

echo.
echo  [3/3] Sunucu baslatiliyor - HTTPS :4200...
echo        (bu pencere sunucu olur, kapatmak icin Ctrl+C)
echo.
cd /d "%~dp0backend"
go run ./cmd/server
cd /d "%~dp0"
goto :bitti

:bitti
echo.
echo   Sunucu kapandi.
echo.
echo   Yayin icin tekrar: start-yayin.bat
echo   Durdurmak icin: start-yayin.bat stop
echo.
pause
exit /b 0

:durdur
echo.
echo  Yayin durduruluyor...
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0tools\stop-fiko.ps1"
echo.
echo  Tamam.
echo.
pause
exit /b 0
