#!/bin/bash

# Production deployment script for nrel.navana-realestate.com
# This script handles the complete deployment process

set -e

echo "======================================"
echo "  NREL Production Deployment Script"
echo "======================================"
echo ""

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

# Configuration
CONTAINER_NAME="nrel-2025"
IMAGE_NAME="nrel-2025"
# External port (Apache proxy connects to this) - high port to avoid cPanel firewall
HOST_PORT="50000"
# Internal container port (Next.js runs on this)
CONTAINER_PORT="3000"

# Check if Docker is accessible
if ! docker info >/dev/null 2>&1; then
    echo -e "${RED}Error: Unable to connect to Docker daemon.${NC}"
    echo "Possible causes:"
    echo "1. Docker is not running"
    echo "2. You need to run this script with sudo"
    echo "   (Try: sudo ./deploy.sh)"
    echo ""
    echo "Actual error from docker info:"
    docker info 2>&1 | head -n 3
    exit 1
fi

echo -e "${YELLOW}Step 1: Pulling latest code (if using git)${NC}"
# Uncomment if you want auto-pull
# git pull origin main

echo -e "${YELLOW}Step 2: Building Docker image...${NC}"
docker build -t $IMAGE_NAME .

echo -e "${YELLOW}Step 3: Stopping existing containers...${NC}"
# Stop container by specific name
docker stop $CONTAINER_NAME 2>/dev/null || true
docker rm $CONTAINER_NAME 2>/dev/null || true

# Also find and stop ANY container listening on the target port
echo "Checking for other containers on port $HOST_PORT..."
EXISTING_CONTAINER_ID=$(docker ps -q --filter "publish=$HOST_PORT")
if [ ! -z "$EXISTING_CONTAINER_ID" ]; then
  echo "Found another container ($EXISTING_CONTAINER_ID) using port $HOST_PORT. Stopping it..."
  docker stop $EXISTING_CONTAINER_ID
  docker rm $EXISTING_CONTAINER_ID
fi

echo -e "${YELLOW}Step 4: Starting new container...${NC}"
docker run -d \
  --name $CONTAINER_NAME \
  --restart unless-stopped \
  -p $HOST_PORT:$CONTAINER_PORT \
  -e NODE_ENV=production \
  -e NEXT_TELEMETRY_DISABLED=1 \
  -e HOSTNAME=0.0.0.0 \
  -e PORT=$CONTAINER_PORT \
  -e NEXT_PUBLIC_STRAPI_URL=https://cms.navana-realestate.com \
  --log-opt max-size=10m \
  --log-opt max-file=3 \
  $IMAGE_NAME

echo -e "${YELLOW}Step 5: Waiting for container to be healthy...${NC}"
sleep 5

# Check if container is running
if docker ps | grep -q $CONTAINER_NAME; then
    echo ""
    echo -e "${GREEN}======================================"
    echo "  ✅ Deployment Successful!"
    echo "======================================${NC}"
    echo ""
    echo "Container Status:"
    docker ps --filter "name=$CONTAINER_NAME" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
    echo ""
    echo "📋 Quick Commands:"
    echo "   docker logs -f $CONTAINER_NAME    # View logs"
    echo "   docker restart $CONTAINER_NAME    # Restart"
    echo "   docker stop $CONTAINER_NAME       # Stop"
    echo ""
    echo "🌐 Website: https://nrel.navana-realestate.com"
else
    echo -e "${RED}❌ Deployment failed! Container is not running${NC}"
    echo "Check logs with: docker logs $CONTAINER_NAME"
    exit 1
fi
