#!/bin/bash
# -----------------------------
# Finaliser une feature avant release
# Usage: ./finish-feature.sh
# -----------------------------

set -e

TARGET_BRANCH="develop"
CURRENT_BRANCH=$(git branch --show-current)

# -----------------------------
# Vérifications
# -----------------------------

if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  echo "❌ Ce dossier n'est pas un dépôt Git."
  exit 1
fi

if [ -z "$CURRENT_BRANCH" ]; then
  echo "❌ Impossible de détecter la branche actuelle."
  exit 1
fi

if [ "$CURRENT_BRANCH" = "master" ] || [ "$CURRENT_BRANCH" = "main" ] || [ "$CURRENT_BRANCH" = "$TARGET_BRANCH" ]; then
  echo "❌ Tu dois être sur une branche feature."
  echo "Branche actuelle : $CURRENT_BRANCH"
  exit 1
fi

echo "✅ Feature détectée : $CURRENT_BRANCH"
echo "Cette branche va être finalisée puis mergée dans $TARGET_BRANCH."
echo ""

read -p "Continuer ? (yes/no) " CONFIRM
if [ "$CONFIRM" != "yes" ]; then
  echo "Annulé."
  exit 1
fi

# -----------------------------
# Commit si nécessaire
# -----------------------------

if [ -n "$(git status --porcelain)" ]; then
  echo ""
  read -p "Message du commit : " COMMIT_MESSAGE

  if [ -z "$COMMIT_MESSAGE" ]; then
    COMMIT_MESSAGE="feature: finalisation de $CURRENT_BRANCH"
  fi

  echo "Commit des changements..."
  git add .
  git commit -m "$COMMIT_MESSAGE"
else
  echo "Aucun changement local à committer."
fi

# -----------------------------
# Récupération des dernières branches
# -----------------------------

echo ""
echo "Récupération des dernières infos Git..."
git fetch origin

# -----------------------------
# Mise à jour de develop
# -----------------------------

echo ""
echo "Mise à jour de $TARGET_BRANCH..."
git checkout "$TARGET_BRANCH"
git pull origin "$TARGET_BRANCH"

# -----------------------------
# Retour sur la feature
# -----------------------------

echo ""
echo "Retour sur la feature $CURRENT_BRANCH..."
git checkout "$CURRENT_BRANCH"

# -----------------------------
# Rebase de la feature sur develop
# -----------------------------

echo ""
echo "Mise à jour de la feature avec $TARGET_BRANCH..."

if ! git rebase "$TARGET_BRANCH"; then
  echo ""
  echo "❌ Conflit pendant le rebase."
  echo "Corrige les conflits, puis fais :"
  echo "git add ."
  echo "git rebase --continue"
  echo ""
  echo "Quand le rebase est terminé, relance :"
  echo "./finish-feature.sh"
  exit 1
fi

# -----------------------------
# Push de la feature
# -----------------------------

echo ""
echo "Push de la feature $CURRENT_BRANCH..."
git push -u origin "$CURRENT_BRANCH" --force-with-lease

# -----------------------------
# Merge dans develop
# -----------------------------

echo ""
echo "Merge de $CURRENT_BRANCH vers $TARGET_BRANCH..."
git checkout "$TARGET_BRANCH"

git merge --no-ff "$CURRENT_BRANCH" -m "merge feature $CURRENT_BRANCH vers $TARGET_BRANCH"

echo ""
echo "Push de $TARGET_BRANCH..."
git push origin "$TARGET_BRANCH"

# -----------------------------
# Suppression optionnelle de la feature
# -----------------------------

echo ""
read -p "Supprimer la branche feature locale et distante ? (yes/no) " DELETE_FEATURE

if [ "$DELETE_FEATURE" = "yes" ]; then
  echo "Suppression de la branche locale..."
  git branch -d "$CURRENT_BRANCH" || echo "⚠️ Impossible de supprimer la branche locale."

  echo "Suppression de la branche distante..."
  git push origin --delete "$CURRENT_BRANCH" || echo "⚠️ Impossible de supprimer la branche distante."
else
  echo "Retour sur la branche feature..."
  git checkout "$CURRENT_BRANCH"
fi

echo ""
echo "✅ Feature finalisée : $CURRENT_BRANCH"
echo "✅ Merge effectué dans : $TARGET_BRANCH"
echo ""
echo "Prochaine étape possible :"
echo "./create-release.sh 1.0.0"