#!/bin/bash

# Script para clonar la rama cd-system desde un repositorio origen
# Uso: ./clone-cd-system.sh <repositorio_origen>
# Ejemplo: ./clone-cd-system.sh https://github.com/LACOMPANIADIGITAL/radocbikes.git

# Colores para output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# Función para mostrar mensajes con colores
print_message() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

print_step() {
    echo -e "${BLUE}[STEP]${NC} $1"
}

# Verificar que se proporcione el repositorio origen
if [ $# -eq 0 ]; then
    print_error "Debes proporcionar la URL del repositorio origen"
    echo "Uso: $0 <repositorio_origen>"
    echo "Ejemplo: $0 https://github.com/LACOMPANIADIGITAL/radocbikes.git"
    exit 1
fi

SOURCE_REPO=$1
REMOTE_NAME="source_repo"

print_message "Iniciando clonación de rama cd-system desde: $SOURCE_REPO"

# Verificar que estamos en un repositorio git
if ! git rev-parse --git-dir > /dev/null 2>&1; then
    print_error "No estás en un repositorio git válido"
    exit 1
fi

# Verificar que no existe ya el remote
if git remote | grep -q "^$REMOTE_NAME$"; then
    print_warning "El remote '$REMOTE_NAME' ya existe. Removiendo..."
    git remote remove $REMOTE_NAME
fi

print_step "1. Agregando repositorio origen como remote..."
git remote add $REMOTE_NAME $SOURCE_REPO

if [ $? -ne 0 ]; then
    print_error "Error al agregar el remote"
    exit 1
fi

print_step "2. Verificando remotes..."
git remote -v

print_step "3. Fetching rama cd-system desde repositorio origen..."
git fetch $REMOTE_NAME cd-system

if [ $? -ne 0 ]; then
    print_error "Error al hacer fetch de la rama cd-system"
    print_warning "Removiendo remote temporal..."
    git remote remove $REMOTE_NAME
    exit 1
fi

# Verificar si la rama cd-system ya existe localmente
if git show-ref --verify --quiet refs/heads/cd-system; then
    print_warning "La rama cd-system ya existe localmente"
    read -p "¿Deseas sobrescribirla? (y/N): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        print_step "4. Eliminando rama cd-system existente..."
        git branch -D cd-system
    else
        print_message "Operación cancelada por el usuario"
        git remote remove $REMOTE_NAME
        exit 0
    fi
fi

print_step "4. Creando rama cd-system local desde origen..."
git checkout -b cd-system $REMOTE_NAME/cd-system

if [ $? -ne 0 ]; then
    print_error "Error al crear la rama cd-system"
    git remote remove $REMOTE_NAME
    exit 1
fi

print_step "5. Verificando estado actual..."
git branch -v
echo
git status

print_step "6. Removiendo remote temporal..."
git remote remove $REMOTE_NAME

print_step "7. Verificando remotes finales..."
git remote -v

print_message "✅ ¡Proceso completado exitosamente!"
print_message "Ahora tienes la rama cd-system clonada y lista para trabajar"
print_message "Puedes usar GitHub Desktop para hacer push de esta rama a tu repositorio"

echo
print_message "Comandos útiles para continuar:"
echo "  - git push -u origin cd-system  (para subir la rama al repositorio)"
echo "  - git checkout main             (para cambiar a la rama principal)"
echo "  - git checkout cd-system        (para volver a la rama cd-system)"
