#!/bin/bash

# Deploy web application for production use

echo "🚀 Deploying Azan Peace Web Application"
echo "========================================"
echo ""

# Step 1: Check if port 3000 is in use
echo "🔍 Step 1: Checking if port 3000 is in use..."
PID=$(lsof -ti:3000)

if [ ! -z "$PID" ]; then
    echo "⚠️  Port 3000 is in use by process $PID"
    echo "🔪 Killing existing process..."
    kill -9 $PID
    echo "✅ Process killed"
    sleep 1
else
    echo "✅ Port 3000 is available"
fi

echo ""
echo "📦 Step 2: Installing dependencies..."
npm install

if [ $? -ne 0 ]; then
    echo "❌ Dependency installation failed!"
    exit 1
fi

echo ""
echo "🏗️  Step 3: Building application..."
npm run build

if [ $? -ne 0 ]; then
    echo "❌ Build failed!"
    exit 1
fi

echo ""
echo "📦 Step 4: Starting production server..."
echo "🌐 Application will be available at:"
echo "   - Local:    http://localhost:3000"
echo "   - Network:  http://www.azanpeace.com:3000"
echo "   - IP:       http://$(ipconfig getifaddr en0 2>/dev/null || hostname -I | awk '{print $1}'):3000"
echo ""
echo "⚡ Starting production server..."
echo ""

# Change to dist directory and start server
cd dist
npx serve -s . -l 3000

# Note: Server will keep running. Press Ctrl+C to stop.
