#!/bin/bash

# ╔════════════════════════════════════════════════════════════╗
# ║         PHANTRIUM - Miner & Wallet Installer               ║
# ╚════════════════════════════════════════════════════════════╝

set -e

GREEN='\033[0;32m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m'

echo ""
echo -e "${CYAN}╔════════════════════════════════════════════════════════════╗${NC}"
echo -e "${CYAN}║         PHANTRIUM - Miner & Wallet Installer               ║${NC}"
echo -e "${CYAN}╚════════════════════════════════════════════════════════════╝${NC}"
echo ""

INSTALL_DIR="$HOME/phantrium"
PLATFORM="$(uname -s)"

# Detect OS
case "$PLATFORM" in
    Linux*)   OS="linux";;
    Darwin*)  OS="macos";;
    MINGW*|MSYS*|CYGWIN*) OS="win";;
    *)        OS="linux";;
esac

echo -e "${BLUE}[1/4]${NC} Detected platform: $OS"

# Create install directory
mkdir -p "$INSTALL_DIR"
cd "$INSTALL_DIR"

# Remove old versions
echo -e "${BLUE}[2/5]${NC} Removing old version..."
rm -f phantrium-miner phantrium-miner.exe phantrium-wallet phantrium-wallet.exe 2>/dev/null
rm -f miner.js offline-wallet.js 2>/dev/null

# Download executables (no cache)
BASE_URL="https://www.phantrium.com/releases"

echo -e "${BLUE}[3/5]${NC} Downloading Phantrium Miner (latest)..."
if [ "$OS" = "win" ]; then
    curl -fSL -H "Cache-Control: no-cache" "$BASE_URL/phantrium-miner-win.exe" -o phantrium-miner.exe
    curl -fSL -H "Cache-Control: no-cache" "$BASE_URL/phantrium-wallet-win.exe" -o phantrium-wallet.exe
elif [ "$OS" = "macos" ]; then
    curl -fSL -H "Cache-Control: no-cache" "$BASE_URL/phantrium-miner-macos" -o phantrium-miner
    curl -fSL -H "Cache-Control: no-cache" "$BASE_URL/phantrium-wallet-macos" -o phantrium-wallet
    chmod +x phantrium-miner phantrium-wallet
else
    curl -fSL -H "Cache-Control: no-cache" "$BASE_URL/phantrium-miner-linux" -o phantrium-miner
    curl -fSL -H "Cache-Control: no-cache" "$BASE_URL/phantrium-wallet-linux" -o phantrium-wallet
    chmod +x phantrium-miner phantrium-wallet
fi

echo -e "${BLUE}[4/5]${NC} Downloading Phantrium Wallet (latest)..."
echo -e "${GREEN}Done${NC}"

# Create desktop shortcuts (Linux)
if [ "$OS" = "linux" ]; then
    echo -e "${BLUE}[4/4]${NC} Creating shortcuts..."

    # Desktop entry for Miner
    cat > "$HOME/.local/share/applications/phantrium-miner.desktop" 2>/dev/null << EOF
[Desktop Entry]
Name=Phantrium Miner
Comment=Mine PHAN tokens
Exec=$INSTALL_DIR/phantrium-miner
Icon=utilities-terminal
Terminal=true
Type=Application
Categories=Utility;
EOF

    # Desktop entry for Wallet
    cat > "$HOME/.local/share/applications/phantrium-wallet.desktop" 2>/dev/null << EOF
[Desktop Entry]
Name=Phantrium Wallet
Comment=Manage your PHAN tokens
Exec=$INSTALL_DIR/phantrium-wallet
Icon=utilities-terminal
Terminal=true
Type=Application
Categories=Utility;
EOF

    echo -e "${GREEN}Shortcuts created${NC}"
elif [ "$OS" = "macos" ]; then
    echo -e "${BLUE}[4/4]${NC} Setup complete"
    echo "  To run: open Terminal, then:"
    echo "  $INSTALL_DIR/phantrium-miner"
    echo "  $INSTALL_DIR/phantrium-wallet"
else
    echo -e "${BLUE}[4/4]${NC} Setup complete"
fi

echo ""
echo -e "${CYAN}╔════════════════════════════════════════════════════════════╗${NC}"
echo -e "${CYAN}║               INSTALLATION COMPLETE                        ║${NC}"
echo -e "${CYAN}╚════════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e "${GREEN}Installed to:${NC} $INSTALL_DIR"
echo ""
echo -e "${GREEN}Start Miner:${NC}"
if [ "$OS" = "win" ]; then
    echo "  Double-click phantrium-miner.exe"
else
    echo "  $INSTALL_DIR/phantrium-miner"
fi
echo ""
echo -e "${GREEN}Start Wallet:${NC}"
if [ "$OS" = "win" ]; then
    echo "  Double-click phantrium-wallet.exe"
else
    echo "  $INSTALL_DIR/phantrium-wallet"
fi
echo ""
echo "Both apps open automatically in your browser."
echo "Keep the terminal window open while using the app."
echo ""

# Ask to start miner
read -p "Start the miner now? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    if [ "$OS" = "win" ]; then
        ./phantrium-miner.exe
    else
        ./phantrium-miner
    fi
fi
