@echo off
REM Complete System Update Deployment Script (Windows)
REM This script deploys Contact Messages + Flexible Login features

echo ================================================================
echo   Brand Master - Complete System Update Deployment
echo ================================================================
echo.
echo This will deploy:
echo   1. Contact Messages Management System
echo   2. Flexible Login (Email/Username/Phone)
echo.

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

REM Step 1: Apply Database Migrations
echo ================================================================
echo Step 1: Applying database migrations...
echo ================================================================
echo.

echo [1/2] Applying contact messages migration...
if exist "apply-migration.bat" (
    call apply-migration.bat 007_enhance_contact_messages.sql
    if errorlevel 1 (
        echo [ERROR] Contact messages migration failed!
        echo Please fix the error and try again.
        pause
        exit /b 1
    )
    echo [SUCCESS] Contact messages migration applied
) else (
    echo [ERROR] Migration script not found!
    pause
    exit /b 1
)
echo.

echo [2/2] Applying flexible login migration...
call apply-migration.bat 008_add_username_to_users.sql
if errorlevel 1 (
    echo [ERROR] Flexible login migration failed!
    echo Please fix the error and try again.
    pause
    exit /b 1
)
echo [SUCCESS] Flexible login migration applied
echo.

REM Step 2: Build Backend Image
echo ================================================================
echo Step 2: Building backend Docker image...
echo ================================================================
cd backend
docker build -t %BACKEND_IMAGE% .
if errorlevel 1 (
    echo [ERROR] Backend build failed!
    pause
    exit /b 1
)
echo [SUCCESS] Backend image built successfully
cd ..
echo.

REM Step 3: Build Frontend Image
echo ================================================================
echo Step 3: Building frontend Docker image...
echo ================================================================
cd frontend
docker build -t %FRONTEND_IMAGE% .
if errorlevel 1 (
    echo [ERROR] Frontend build failed!
    pause
    exit /b 1
)
echo [SUCCESS] Frontend image built successfully
cd ..
echo.

REM Step 4: Push Images to Harbor
echo ================================================================
echo Step 4: Pushing images to Harbor registry...
echo ================================================================
echo Pushing backend...
docker push %BACKEND_IMAGE%
if errorlevel 1 (
    echo [ERROR] Backend push failed!
    pause
    exit /b 1
)
echo [SUCCESS] Backend image pushed

echo Pushing frontend...
docker push %FRONTEND_IMAGE%
if errorlevel 1 (
    echo [ERROR] Frontend push failed!
    pause
    exit /b 1
)
echo [SUCCESS] Frontend image pushed
echo.

REM Step 5: Deploy to Kubernetes
echo ================================================================
echo Step 5: Deploying to Kubernetes...
echo ================================================================
cd brand-master-chart
helm upgrade brand-master . --namespace %NAMESPACE% --set backend.image.tag=latest --set frontend.image.tag=latest --wait --timeout 5m
if errorlevel 1 (
    echo [ERROR] Helm deployment failed!
    pause
    exit /b 1
)
echo [SUCCESS] Helm deployment successful
cd ..
echo.

REM Step 6: Verify Deployment
echo ================================================================
echo Step 6: Verifying deployment...
echo ================================================================
echo Checking pods status...
kubectl get pods -n %NAMESPACE% | findstr brand-master

echo.
echo Checking backend logs...
kubectl logs -n %NAMESPACE% deployment/brand-master-backend --tail=30

echo.
echo ================================================================
echo             Deployment Completed Successfully!
echo ================================================================
echo.
echo NEW FEATURES DEPLOYED:
echo.
echo 1. CONTACT MESSAGES MANAGEMENT
echo    - Admin can view/manage contact form submissions
echo    - Unread counter badge in admin dashboard
echo    - Status tracking (New/Read/Replied)
echo    - Admin notes functionality
echo.
echo 2. FLEXIBLE LOGIN
echo    - Users can now login with:
echo      * Email: admin@brandmaster.com
echo      * Username: (if set during registration)
echo      * Phone: (if set during registration)
echo.
echo TESTING INSTRUCTIONS:
echo ================================================================
echo.
echo A. Test Contact Form:
echo    1. Visit: https://brand-master.dvirlabs.com/contact
echo    2. Fill form with name, email, phone, subject, message
echo    3. Submit and verify success
echo.
echo B. Test Admin Dashboard:
echo    1. Login: https://brand-master.dvirlabs.com/login
echo    2. Credentials: admin@brandmaster.com / Admin123!
echo    3. Click "Contact Messages" tab
echo    4. View unread counter, open message, update status
echo.
echo C. Test Flexible Login:
echo    1. Try logging in with email: admin@brandmaster.com
echo    2. Create new user with username and phone
echo    3. Try logging in with username
echo    4. Try logging in with phone number
echo.
echo USEFUL COMMANDS:
echo ================================================================
echo - View backend logs:
echo   kubectl logs -n %NAMESPACE% deployment/brand-master-backend -f
echo.
echo - View frontend logs:
echo   kubectl logs -n %NAMESPACE% deployment/brand-master-frontend -f
echo.
echo - Check database:
echo   kubectl exec -it -n %NAMESPACE% deployment/brand-master-postgres -- psql -U brand_master -d brand_master_db
echo.
echo - Check contact messages:
echo   SELECT * FROM contact_message ORDER BY created_at DESC LIMIT 10;
echo.
echo - Check users table:
echo   SELECT id, email, username, phone, full_name FROM "user";
echo.
pause
