#!/bin/bash
set -e

REPO="ssh://git@git.meshkee.com:222/sajjad.talkhabi/test.git"
APP_DIR="/root/banu-front"
IMAGE="banu-front"
CONTAINER="banu-front"
PORT="8080"
VITE_API_BASE_URL="//api.madresebanoo.ir/api"

echo "==> Pulling latest code..."
if [ -d "$APP_DIR/.git" ]; then
  git -C "$APP_DIR" pull
else
  git clone "$REPO" "$APP_DIR"
fi

echo "==> Building Docker image..."
docker build \
  --build-arg VITE_API_BASE_URL="$VITE_API_BASE_URL" \
  -t "$IMAGE" "$APP_DIR"

echo "==> Stopping old container..."
docker rm -f "$CONTAINER" 2>/dev/null || true

echo "==> Starting new container..."
docker run -d \
  --name "$CONTAINER" \
  --restart unless-stopped \
  -p "$PORT:80" \
  "$IMAGE"

echo "==> Done. App running at http://$(hostname -I | awk '{print $1}'):$PORT"
