#!/bin/bash

DOTFILES_REPO="acedanger/shell"
DOTFILES_BRANCH="main"
DOTFILES_DIR="$HOME/shell"

# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

echo -e "${GREEN}Setting up your system...${NC}"

# Detect OS
if [ -f /etc/os-release ]; then
    . /etc/os-release
    OS_NAME=$ID
    OS_VERSION=$VERSION_ID
else
    echo -e "${YELLOW}Unable to detect OS, assuming Debian/Ubuntu...${NC}"
    OS_NAME="ubuntu"
    OS_VERSION="22.04"
fi

echo -e "${GREEN}Detected OS: ${OS_NAME} ${OS_VERSION}${NC}"

# Function to determine the package manager to use
determine_pkg_manager() {
  if command -v nala &> /dev/null; then
    echo "nala"
  elif [ "$OS_NAME" = "fedora" ]; then
    echo "dnf"
  else
    echo "apt"
  fi
}

# Determine which package manager to use
PKG_MANAGER=$(determine_pkg_manager)
echo -e "${GREEN}Using package manager: $PKG_MANAGER${NC}"

# Install git if not present
if ! command -v git &>/dev/null; then
    echo -e "${YELLOW}Installing git...${NC}"
    case $PKG_MANAGER in
        nala)
            sudo nala install -y git
            ;;
        dnf)
            # Enable COPR repositories for Fedora before installing packages
            if [ "$OS_NAME" = "fedora" ]; then
                echo -e "${YELLOW}Setting up COPR repositories for Fedora...${NC}"
                sudo dnf copr enable -y alternateved/eza 2>/dev/null || echo -e "${YELLOW}Eza COPR repo already enabled or unavailable${NC}"
                sudo dnf copr enable -y shaps/lazygit 2>/dev/null || echo -e "${YELLOW}Lazygit COPR repo already enabled or unavailable${NC}"
            fi
            sudo dnf install -y git
            ;;
        apt)
            sudo apt update && sudo apt install -y git
            ;;
    esac
fi

# Clone or update repository
if [ -d "$DOTFILES_DIR/.git" ]; then
    echo -e "${YELLOW}Updating existing shell repository...${NC}"
    cd "$DOTFILES_DIR"
    git pull origin $DOTFILES_BRANCH
elif [ -d "$DOTFILES_DIR" ]; then
    echo -e "${YELLOW}Directory exists but is not a git repository.${NC}"
    # Check if directory is empty
    if [ -z "$(ls -A "$DOTFILES_DIR")" ]; then
        echo -e "${YELLOW}Directory is empty. Cloning...${NC}"
        git clone "https://github.com/$DOTFILES_REPO.git" "$DOTFILES_DIR"
    else
        echo -e "${YELLOW}Backing up existing directory...${NC}"
        mv "$DOTFILES_DIR" "${DOTFILES_DIR}.bak.$(date +%s)"
        echo -e "${YELLOW}Cloning shell repository...${NC}"
        git clone "https://github.com/$DOTFILES_REPO.git" "$DOTFILES_DIR"
    fi
    cd "$DOTFILES_DIR"
else
    echo -e "${YELLOW}Cloning shell repository...${NC}"
    git clone "https://github.com/$DOTFILES_REPO.git" "$DOTFILES_DIR"
    cd "$DOTFILES_DIR"
fi

# Make scripts executable
chmod +x "$DOTFILES_DIR/setup/setup.sh"
chmod +x "$DOTFILES_DIR/completions/backup-scripts-completion.bash" 2>/dev/null || true
chmod +x "$DOTFILES_DIR/completions/plex-scripts-completion.bash" 2>/dev/null || true
chmod +x "$DOTFILES_DIR/completions/env-backup-completion.bash" 2>/dev/null || true

# Run setup script
"$DOTFILES_DIR/setup/setup.sh"

# Source the aliases file if it exists to make aliases available in the current session
ZSH_CUSTOM="$HOME/.oh-my-zsh/custom"
ALIASES_FILE="$ZSH_CUSTOM/aliases.zsh"
if [ -f "$ALIASES_FILE" ]; then
    echo -e "${YELLOW}Loading aliases into current session...${NC}"
    source "$ALIASES_FILE"
fi

echo -e "${GREEN}Bootstrap completed! Your aliases have been set up.${NC}"
echo -e "${YELLOW}Tip: Run 'source ~/.zshrc' or start a new terminal session to use all zsh features.${NC}"
