@echo off
REM Contact Messages System Deployment Script (Windows)
REM This script automates the deployment of the Contact Messages management system

echo ================================================================
echo   Brand Master - Contact Messages System Deployment (Windows)
echo ================================================================
echo.

REM Configuration
set NAMESPACE=my-apps
set MIGRATION_FILE=007_enhance_contact_messages.sql
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 Migration
echo Step 1: Applying database migration...
if exist "apply-migration.bat" (
    call apply-migration.bat %MIGRATION_FILE%
    if errorlevel 1 (
        echo [ERROR] Migration failed!
        exit /b 1
    )
    echo [SUCCESS] Migration applied successfully
) else (
    echo [ERROR] Migration script not found!
    echo Please run manually: apply-migration.bat %MIGRATION_FILE%
    exit /b 1
)
echo.

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

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

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

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

REM Step 5: Deploy to Kubernetes
echo Step 5: Deploying to Kubernetes...
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!
    exit /b 1
)
echo [SUCCESS] Helm deployment successful
cd ..
echo.

REM Step 6: Verify Deployment
echo Step 6: Verifying deployment...
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=20

echo.
echo ================================================================
echo             Deployment Completed Successfully!
echo ================================================================
echo.
echo Next Steps:
echo 1. Test public contact form: https://brand-master.dvirlabs.com/contact
echo 2. Test admin dashboard: https://brand-master.dvirlabs.com/admin
echo 3. Verify contact messages management functionality
echo.
echo Useful Commands:
echo   - View backend logs: kubectl logs -n %NAMESPACE% deployment/brand-master-backend -f
echo   - View frontend logs: kubectl logs -n %NAMESPACE% deployment/brand-master-frontend -f
echo   - Check database: kubectl exec -it -n %NAMESPACE% deployment/brand-master-postgres -- psql -U brand_master -d brand_master_db
echo.
