#!/usr/bin/env bash

# MeTube installation script with offline Deno installation
# Place this script and deno-x86_64-unknown-linux-gnu.zip in the same directory

set -e

APP="MeTube"
APP_DIR="/opt/metube"
DENO_INSTALL_DIR="/usr/local"
DENO_ZIP="deno-x86_64-unknown-linux-gnu.zip"
GITHUB_REPO="alexta69/metube"
SERVICE_USER="root"

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

info() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }
step() { echo -e "${BLUE}[STEP]${NC} $1"; }

# Check if running as root
if [[ $EUID -ne 0 ]]; then
   error "This script must be run as root"
fi

# Check for local Deno archive
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DENO_ARCHIVE_PATH="${SCRIPT_DIR}/${DENO_ZIP}"

if [[ ! -f "$DENO_ARCHIVE_PATH" ]]; then
    error "Deno archive not found at $DENO_ARCHIVE_PATH"
fi

info "Found Deno archive: $DENO_ARCHIVE_PATH"

# Install dependencies
step "Installing system dependencies"
apt-get update
apt-get install -y curl wget unzip git build-essential python3 python3-pip python3-venv

step "Installing NODEJS"
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
apt install -y nodejs
npm install -g pm2

# Install Deno from local archive
step "Installing Deno from local archive"
if [[ -d "${DENO_INSTALL_DIR}/deno" ]]; then
    rm -rf "${DENO_INSTALL_DIR}/deno"
fi

unzip -o "$DENO_ARCHIVE_PATH" -d /tmp/deno_extract
chmod +x /tmp/deno_extract/deno
mv /tmp/deno_extract/deno "${DENO_INSTALL_DIR}/bin/deno"
rm -rf /tmp/deno_extract

if command -v deno >/dev/null 2>&1; then
    info "Deno installed successfully: $(deno --version)"
else
    error "Deno installation failed"
fi

# Install pnpm
step "Installing pnpm"
curl -fsSL https://get.pnpm.io/install.sh | sh -

# Download and setup MeTube
step "Downloading MeTube from GitHub"
LATEST_URL=$(curl -s "https://api.github.com/repos/${GITHUB_REPO}/releases/latest" | grep "tarball_url" | cut -d '"' -f 4)

if [[ -z "$LATEST_URL" ]]; then
    error "Failed to get latest release URL"
fi

if [[ -d "$APP_DIR" ]]; then
    warn "Existing installation found, backing up..."
    mv "$APP_DIR" "${APP_DIR}_bak_$(date +%Y%m%d_%H%M%S)"
fi

mkdir -p "$APP_DIR"
cd "$APP_DIR"

curl -L "$LATEST_URL" -o metube.tar.gz
tar -xzf metube.tar.gz --strip-components=1
rm metube.tar.gz

# Build frontend
step "Building frontend"
if [[ -d "$APP_DIR/ui" ]]; then
    cd "$APP_DIR/ui"
    pnpm install --frozen-lockfile
    pnpm run build
else
    error "UI directory not found"
fi

# Setup Python backend
step "Setting up Python backend"
cd "$APP_DIR"
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install yt-dlp aiohttp python-dotenv

# Check for requirements.txt
if [[ -f "$APP_DIR/requirements.txt" ]]; then
    pip install -r requirements.txt
fi

# Create .env file
step "Creating configuration"
if [[ ! -f "$APP_DIR/.env" ]]; then
    cat > "$APP_DIR/.env" <<EOF
# MeTube Configuration
DOWNLOAD_DIR=/downloads
STATE_DIR=/opt/metube/state
URL_PREFIX=
HTTP_USERNAME=
HTTP_PASSWORD=
YTDL_OPTIONS=
EOF
fi

# Create download and state directories
mkdir -p /downloads
mkdir -p "$APP_DIR/state"

# Create systemd service
step "Creating systemd service"
cat > /etc/systemd/system/metube.service <<EOF
[Unit]
Description=MeTube - YouTube Downloader
After=network.target

[Service]
Type=simple
WorkingDirectory=${APP_DIR}
EnvironmentFile=${APP_DIR}/.env
ExecStart=${APP_DIR}/.venv/bin/python3 ${APP_DIR}/app/main.py
Restart=always
User=root
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target
EOF

# Reload systemd and start service
step "Starting MeTube service"
systemctl daemon-reload
systemctl enable metube
systemctl start metube

# Check if service is running
sleep 3
if systemctl is-active --quiet metube; then
    info "MeTube service started successfully"
else
    error "MeTube service failed to start. Check logs with: journalctl -u metube"
fi

# Get IP address
IP_ADDRESS=$(hostname -I | awk '{print $1}')

echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}MeTube installation completed!${NC}"
echo -e "${GREEN}========================================${NC}"
echo -e "Access MeTube at: ${YELLOW}http://${IP_ADDRESS}:8081${NC}"
echo ""
echo -e "Commands:"
echo -e "  Start:   ${BLUE}systemctl start metube${NC}"
echo -e "  Stop:    ${BLUE}systemctl stop metube${NC}"
echo -e "  Status:  ${BLUE}systemctl status metube${NC}"
echo -e "  Logs:    ${BLUE}journalctl -u metube -f${NC}"
echo ""
echo -e "Download directory: ${YELLOW}/downloads${NC}"
echo -e "Config file: ${YELLOW}${APP_DIR}/.env${NC}"