@echo off
REM Quick Fix Script - Apply Migrations and Deploy
REM This script fixes the database errors and deploys the updated system

echo ================================================================
echo   Brand Master - Quick Fix Deployment
echo ================================================================
echo.
echo This will:
echo   1. Apply contact messages migration (fix full_name error)
echo   2. Apply flexible login migration (fix username error)
echo   3. Rebuild and deploy backend/frontend
echo.
pause

REM Step 1: Apply Contact Messages Migration
echo ================================================================
echo Step 1: Applying contact messages migration...
echo ================================================================
call apply-migration.bat 007_enhance_contact_messages.sql
if errorlevel 1 (
    echo [ERROR] Failed to apply contact messages migration
    pause
    exit /b 1
)
echo.

REM Step 2: Apply Flexible Login Migration
echo ================================================================
echo Step 2: Applying flexible login migration...
echo ================================================================
call apply-migration.bat 008_add_username_to_users.sql
if errorlevel 1 (
    echo [ERROR] Failed to apply flexible login migration
    pause
    exit /b 1
)
echo.

REM Step 3: Build and Deploy
echo ================================================================
echo Step 3: Building and deploying...
echo ================================================================

set BACKEND_IMAGE=harbor.dvirlabs.com/my-apps/brand-master-backend:latest
set FRONTEND_IMAGE=harbor.dvirlabs.com/my-apps/brand-master-frontend:latest

echo Building backend...
cd backend
docker build -t %BACKEND_IMAGE% .
if errorlevel 1 (
    echo [ERROR] Backend build failed
    pause
    exit /b 1
)
cd ..

echo Building frontend...
cd frontend
docker build -t %FRONTEND_IMAGE% .
if errorlevel 1 (
    echo [ERROR] Frontend build failed
    pause
    exit /b 1
)
cd ..

echo Pushing images...
docker push %BACKEND_IMAGE%
docker push %FRONTEND_IMAGE%

echo Deploying to Kubernetes...
cd brand-master-chart
helm upgrade brand-master . --namespace my-apps --wait --timeout 5m
cd ..

echo.
echo ================================================================
echo                   Deployment Complete!
echo ================================================================
echo.
echo FIXED ISSUES:
echo   ✅ Contact form "full_name" error - FIXED
echo   ✅ Password reset "username" error - FIXED
echo   ✅ Email sending functionality - ADDED
echo.
echo NEXT STEPS:
echo.
echo 1. CONFIGURE EMAIL (Required for password reset to work):
echo    - Read EMAIL_SETUP.md for detailed instructions
echo    - Quick setup: Update brand-master-chart/values.yaml with SMTP settings
echo    - Or check backend logs for PIN (printed to console if email not configured)
echo.
echo 2. TEST PASSWORD RESET:
echo    - Go to login page
echo    - Click "Forgot Password"
echo    - Enter your email
echo    - Check email for PIN (or backend logs if email not configured)
echo    - Enter PIN and new password
echo.
echo 3. VIEW BACKEND LOGS:
echo    kubectl logs -n my-apps deployment/brand-master-backend -f
echo.
echo 4. CHECK EMAIL CONFIGURATION:
echo    kubectl exec -n my-apps deployment/brand-master-backend -- env ^| findstr SMTP
echo.
echo FOR EMAIL SETUP HELP: See EMAIL_SETUP.md
echo.
pause
