#!/bin/bash
set -e

# Script to publish the backend to a separate GitHub repository
TARGET_REPO_URL="$1"

if [ -z "$TARGET_REPO_URL" ]; then
  echo "Usage: ./scripts/publish-backend-repo.sh <GITHUB_REPO_URL>"
  echo "Example: ./scripts/publish-backend-repo.sh https://github.com/KararaDMaiyaki83/atiku2027-backend-api.git"
  exit 1
fi

echo "=========================================================="
echo "Publishing Standalone Backend to Separate GitHub Repo"
echo "Target: $TARGET_REPO_URL"
echo "=========================================================="

TEMP_DIR=$(mktemp -d)
echo "Created temporary export workspace at $TEMP_DIR"

# Copy backend files excluding node_modules and dist
rsync -av --exclude='node_modules' --exclude='dist' --exclude='.env' backend/ "$TEMP_DIR/"

cd "$TEMP_DIR"
git init
git branch -M main
git add -A
git commit -m "feat: initial release of atiku2027 standalone backend REST API microservice"

git remote add origin "$TARGET_REPO_URL"
echo "Pushing to $TARGET_REPO_URL..."
git push -u origin main --force

echo "=========================================================="
echo "✅ Successfully published Backend API to separate GitHub repo!"
echo "=========================================================="
rm -rf "$TEMP_DIR"
