@echo off
REM HLS Transcoding Setup Script for Windows
REM This script initializes the HLS transcoding infrastructure

setlocal enabledelayedexpansion

echo.
echo ===========================================
echo HLS Transcoding Setup Script for Windows
echo ===========================================
echo.

REM Check if .env file exists
if not exist ".env" (
    echo ⚠ .env file not found
    echo Creating .env from .env.example...
    copy .env.example .env
    echo ✓ .env file created
    echo.
    echo Please edit .env and configure your settings before proceeding
    echo.
)

echo Step 1: Checking prerequisites
echo.

REM Check if Docker is installed
docker --version >nul 2>&1
if %errorlevel% neq 0 (
    echo ✗ Docker is not installed
    echo Please install Docker Desktop from https://www.docker.com/products/docker-desktop
    exit /b 1
)
echo ✓ Docker is installed (%docker_version%)

REM Check if Docker Compose is installed
docker-compose --version >nul 2>&1
if %errorlevel% neq 0 (
    echo ✗ Docker Compose is not installed or not accessible
    echo Make sure Docker Desktop includes Docker Compose (it does by default)
    exit /b 1
)
echo ✓ Docker Compose is installed

echo.
echo Step 2: Creating required directories
echo.

REM Create uploads directory structure
set UPLOADS_DIR=C:\Users\%USERNAME%\college-uploads
if not exist "%UPLOADS_DIR%\videos" mkdir "%UPLOADS_DIR%\videos"
if not exist "%UPLOADS_DIR%\hls" mkdir "%UPLOADS_DIR%\hls"

echo ✓ Created %UPLOADS_DIR%\videos
echo ✓ Created %UPLOADS_DIR%\hls

REM Create PostgreSQL data directory
set PG_DIR=C:\ProgramData\college-postgres
if not exist "%PG_DIR%" mkdir "%PG_DIR%"

echo ✓ Created PostgreSQL data directory

echo.
echo Step 3: Building Docker images
echo.

REM Build the transcoder image
echo Building transcoder image...
docker-compose build transcoder
if %errorlevel% neq 0 (
    echo ✗ Failed to build transcoder image
    exit /b 1
)
echo ✓ Transcoder image built

echo.
echo Step 4: Starting services
echo.

REM Start all services
docker-compose up -d
if %errorlevel% neq 0 (
    echo ✗ Failed to start services
    exit /b 1
)
echo ✓ Services started

echo.
echo Step 5: Waiting for services to be ready
echo.

REM Wait for PostgreSQL (max 30 seconds)
echo Waiting for PostgreSQL...
set "retry=0"
:wait_postgres
if %retry% geq 30 goto postgres_timeout
docker-compose exec -T postgres pg_isready -U cms_user >nul 2>&1
if %errorlevel% neq 0 (
    set /a retry+=1
    timeout /t 1 /nobreak >nul
    goto wait_postgres
)
echo ✓ PostgreSQL is ready
goto skip_postgres_timeout

:postgres_timeout
echo ⚠ PostgreSQL did not become ready in time
goto skip_postgres_timeout

:skip_postgres_timeout

REM Wait for CMS (max 60 seconds)
echo Waiting for CMS application...
set "retry=0"
:wait_cms
if %retry% geq 60 goto cms_timeout
curl -s http://localhost:3000 >nul 2>&1
if %errorlevel% neq 0 (
    set /a retry+=1
    timeout /t 1 /nobreak >nul
    goto wait_cms
)
echo ✓ CMS is ready
goto skip_cms_timeout

:cms_timeout
echo ⚠ CMS did not become ready in time

:skip_cms_timeout

echo.
echo Step 6: Running database migration
echo.

docker-compose exec -T cms npx prisma migrate deploy
if %errorlevel% neq 0 (
    echo ⚠ Database migration had issues, but setup is proceeding
) else (
    echo ✓ Database migration completed
)

echo.
echo ===========================================
echo Setup Complete!
echo ===========================================
echo.
echo Services are now running:
echo   * CMS: http://localhost:3000
echo   * PostgreSQL: localhost:5432
echo   * Transcoder: (background service)
echo.
echo Next steps:
echo   1. Visit http://localhost:3000 and sign in
echo   2. Upload MP4 files as an admin user
echo   3. Watch the transcoder logs: docker-compose logs -f transcoder
echo   4. Check HLS files: dir "%UPLOADS_DIR%\hls\{video_id}"
echo.
echo For more information, see TRANSCODING.md
echo.
echo Note: To configure custom paths, edit .env before running docker-compose commands
echo.
