#!/bin/bash
# Contact Messages System Deployment Script
# This script automates the deployment of the Contact Messages management system

set -e  # Exit on any error

echo "╔══════════════════════════════════════════════════════════╗"
echo "║  Brand Master - Contact Messages System Deployment      ║"
echo "╚══════════════════════════════════════════════════════════╝"
echo ""

# Configuration
NAMESPACE="my-apps"
MIGRATION_FILE="007_enhance_contact_messages.sql"
BACKEND_IMAGE="harbor.dvirlabs.com/my-apps/brand-master-backend:latest"
FRONTEND_IMAGE="harbor.dvirlabs.com/my-apps/brand-master-frontend:latest"

# Color codes
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# Step 1: Apply Database Migration
echo -e "${YELLOW}Step 1: Applying database migration...${NC}"
if [ -f "./apply-migration.sh" ]; then
    ./apply-migration.sh "$MIGRATION_FILE"
    echo -e "${GREEN}✓ Migration applied successfully${NC}"
else
    echo -e "${RED}✗ Migration script not found!${NC}"
    echo "Please run manually: ./apply-migration.sh $MIGRATION_FILE"
    exit 1
fi
echo ""

# Step 2: Build Backend Image
echo -e "${YELLOW}Step 2: Building backend Docker image...${NC}"
cd backend
docker build -t "$BACKEND_IMAGE" .
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ Backend image built successfully${NC}"
else
    echo -e "${RED}✗ Backend build failed!${NC}"
    exit 1
fi
cd ..
echo ""

# Step 3: Build Frontend Image
echo -e "${YELLOW}Step 3: Building frontend Docker image...${NC}"
cd frontend
docker build -t "$FRONTEND_IMAGE" .
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ Frontend image built successfully${NC}"
else
    echo -e "${RED}✗ Frontend build failed!${NC}"
    exit 1
fi
cd ..
echo ""

# Step 4: Push Images to Harbor
echo -e "${YELLOW}Step 4: Pushing images to Harbor registry...${NC}"
docker push "$BACKEND_IMAGE"
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ Backend image pushed successfully${NC}"
else
    echo -e "${RED}✗ Backend push failed!${NC}"
    exit 1
fi

docker push "$FRONTEND_IMAGE"
if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ Frontend image pushed successfully${NC}"
else
    echo -e "${RED}✗ Frontend push failed!${NC}"
    exit 1
fi
echo ""

# Step 5: Deploy to Kubernetes
echo -e "${YELLOW}Step 5: Deploying to Kubernetes...${NC}"
cd brand-master-chart
helm upgrade brand-master . \
    --namespace "$NAMESPACE" \
    --set backend.image.tag=latest \
    --set frontend.image.tag=latest \
    --wait \
    --timeout 5m

if [ $? -eq 0 ]; then
    echo -e "${GREEN}✓ Helm deployment successful${NC}"
else
    echo -e "${RED}✗ Helm deployment failed!${NC}"
    exit 1
fi
cd ..
echo ""

# Step 6: Verify Deployment
echo -e "${YELLOW}Step 6: Verifying deployment...${NC}"
echo "Checking pods status..."
kubectl get pods -n "$NAMESPACE" | grep brand-master

echo ""
echo "Checking backend logs..."
kubectl logs -n "$NAMESPACE" deployment/brand-master-backend --tail=20

echo ""
echo -e "${GREEN}╔══════════════════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║           Deployment Completed Successfully!            ║${NC}"
echo -e "${GREEN}╚══════════════════════════════════════════════════════════╝${NC}"
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 ""
