#!/bin/bash
set -e

# Colors
BLUE='\033[1;34m'
YELLOW='\033[1;33m'
GREEN='\033[1;32m'
RED='\033[1;31m'
NC='\033[0m'

SECRETS_FILE="$HOME/.zsh_secrets"
REMOTE_NAME="hetzner-box"

# Dependency Check
if ! command -v rclone &> /dev/null; then
    echo -e "${RED}Error: rclone is not installed.${NC}"
    exit 1
fi

# Load Secrets
if [ -f "$SECRETS_FILE" ]; then
    source "$SECRETS_FILE"
fi

# Configure Remote (If missing)
if ! rclone listremotes | grep -q "^${REMOTE_NAME}:$"; then
    echo -e "${BLUE}Configuration missing. Starting setup...${NC}"

    if [ -z "$HETZNER_USER" ]; then
        DEFAULT_USER="u513875"
        read -p "Enter Username [$DEFAULT_USER]: " INPUT_USER
        HETZNER_USER="${INPUT_USER:-$DEFAULT_USER}"
    fi
    
    if [ -z "$HETZNER_HOST" ]; then
        DEFAULT_HOST="${HETZNER_USER}.your-storagebox.de"
        read -p "Enter Host [$DEFAULT_HOST]: " INPUT_HOST
        HETZNER_HOST="${INPUT_HOST:-$DEFAULT_HOST}"
    fi
    
    if [ -z "$HETZNER_PASS" ]; then
        echo -e "${YELLOW}Note: Passwords are obscured in config, but saving to secrets file uses plain text.${NC}"
        read -s -p "Enter Password: " HETZNER_PASS
        echo ""
    fi

    # Save to secrets
    if ! grep -q "HETZNER_USER" "$SECRETS_FILE" 2>/dev/null; then
        echo -e "${BLUE}Saving credentials to $SECRETS_FILE...${NC}"
        {
            echo ""
            echo "# Hetzner Storage Box"
            echo "export HETZNER_USER=\"$HETZNER_USER\""
            echo "export HETZNER_HOST=\"$HETZNER_HOST\""
            echo "export HETZNER_PASS=\"$HETZNER_PASS\""
        } >> "$SECRETS_FILE"
    fi

    OBSCURED_PASS=$(rclone obscure "$HETZNER_PASS")
    rclone config create "$REMOTE_NAME" sftp \
        host "$HETZNER_HOST" \
        user "$HETZNER_USER" \
        pass "$OBSCURED_PASS" \
        port 23 \
        --non-interactive
        
    echo -e "${GREEN}Configuration created!${NC}"
fi

# Interactive Menu
echo -e "${BLUE}--- Hetzner Storage Box Manager ---${NC}"
echo "1) List files (ls)"
echo "2) Upload (Copy LOCAL -> REMOTE)"
echo "3) Download (Copy REMOTE -> LOCAL)"
echo "q) Quit"

read -p "Select option: " OPTION

case $OPTION in
    1)
        read -p "Enter depth [1]: " INPUT_DEPTH
        DEPTH="${INPUT_DEPTH:-1}"
        echo -e "${BLUE}Listing files on Hetzner...${NC}"
        rclone ls "${REMOTE_NAME}:" --max-depth "$DEPTH"
        ;;
    2)
        read -p "Enter local folder/file to upload: " LOCAL_PATH
        LOCAL_PATH=${LOCAL_PATH%/}
        
        read -p "Enter remote destination folder (e.g. Backup): " REMOTE_FOLDER
        REMOTE_FOLDER=${REMOTE_FOLDER#/}
        REMOTE_FOLDER=${REMOTE_FOLDER%/}
        
        echo -e "${YELLOW}Uploading '$LOCAL_PATH' to '${REMOTE_NAME}:$REMOTE_FOLDER'...${NC}"
        rclone copy "$LOCAL_PATH" "${REMOTE_NAME}:$REMOTE_FOLDER" -P
        ;;
    3)
        echo -e "${BLUE}Contents of Root:${NC}"
        rclone lsd "${REMOTE_NAME}:" 
        
        read -p "Enter remote path to download (e.g. models/Test): " REMOTE_PATH
        REMOTE_PATH=${REMOTE_PATH#/}
        
        read -p "Enter local destination folder: " LOCAL_DEST
        mkdir -p "$LOCAL_DEST"
        
        echo -e "${YELLOW}Downloading '${REMOTE_NAME}:$REMOTE_PATH' to '$LOCAL_DEST'...${NC}"
        rclone copy "${REMOTE_NAME}:$REMOTE_PATH" "$LOCAL_DEST" -P
        ;;
    q)
        exit 0
        ;;
    *)
        echo "Invalid option"
        ;;
esac