@echo off
REM Quick Publish Script for Irene Cipher (Windows)
REM This script automates the publishing process

echo ================================================================
echo         Irene Cipher - PyPI Publishing Script
echo ================================================================
echo.

REM Check if maturin is installed
where maturin >nul 2>&1
if %errorlevel% neq 0 (
    echo [X] maturin not found. Installing...
    pip install maturin
)

REM Check if PyPI token is set
if "%MATURIN_PYPI_TOKEN%"=="" (
    echo [!] MATURIN_PYPI_TOKEN environment variable not set
    echo.
    echo Please set your PyPI token:
    echo   set MATURIN_PYPI_TOKEN=pypi-YOUR_TOKEN
    echo.
    echo Get your token from: https://pypi.org/manage/account/token/
    exit /b 1
)

echo [+] maturin found
echo [+] PyPI token configured
echo.

REM Run tests
echo Running tests...
echo ----------------------------------------------------------------

cargo test --quiet
if %errorlevel% neq 0 (
    echo [X] Rust tests failed
    exit /b 1
)
echo [+] Rust tests passed

python test_irene_cipher.py >nul 2>&1
if %errorlevel% neq 0 (
    echo [X] Python tests failed
    exit /b 1
)
echo [+] Python tests passed

echo.

REM Format check
echo Checking code formatting...
echo ----------------------------------------------------------------

cargo fmt --check --quiet
if %errorlevel% neq 0 (
    echo [!] Code needs formatting. Run: cargo fmt
    set /p continue="Continue anyway? (y/N): "
    if /i not "%continue%"=="y" exit /b 1
) else (
    echo [+] Code formatting OK
)

echo.

REM Version info
echo Package Information:
echo ----------------------------------------------------------------
for /f "tokens=2 delims==" %%a in ('findstr /C:"version" Cargo.toml ^| findstr /v "#"') do set VERSION=%%a
set VERSION=%VERSION: =%
set VERSION=%VERSION:"=%
echo Name: irene-cipher
echo Version: %VERSION%
echo.

REM Ask for confirmation
set /p confirm="Ready to publish version %VERSION% to PyPI? (y/N): "
if /i not "%confirm%"=="y" (
    echo Aborted.
    exit /b 1
)

echo.

REM Build and publish
echo Building and publishing...
echo ----------------------------------------------------------------

maturin publish
if %errorlevel% equ 0 (
    echo.
    echo ================================================================
    echo                    SUCCESS! [OK]
    echo ================================================================
    echo.
    echo Package published to PyPI!
    echo.
    echo View at: https://pypi.org/project/irene-cipher/%VERSION%/
    echo.
    echo Install with: pip install irene-cipher
    echo.
) else (
    echo.
    echo [X] Publishing failed
    echo.
    echo Common issues:
    echo   - Version already exists (bump version in Cargo.toml and pyproject.toml^)
    echo   - Invalid token (check MATURIN_PYPI_TOKEN^)
    echo   - Network issues (check internet connection^)
    exit /b 1
)
