#!/bin/bash
#
# AtlasOS - Calypso
# System Requirements Installation Script
# Target OS: Ubuntu Server 24.04 LTS
#
# This script installs all system and development dependencies required
# for building and running the Calypso backup appliance platform.
#
# Run with: sudo ./scripts/install-requirements.sh
#

set -euo pipefail

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

# Logging functions
log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

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

# Check if running as root
if [[ $EUID -ne 0 ]]; then
   log_error "This script must be run as root (use sudo)"
   exit 1
fi

# Detect OS
if ! grep -q "Ubuntu 24.04" /etc/os-release 2>/dev/null; then
    log_warn "This script is designed for Ubuntu 24.04 LTS. Proceeding anyway..."
fi

log_info "Starting AtlasOS - Calypso requirements installation..."

# Update package lists
log_info "Updating package lists..."
apt-get update -qq

# Install base build tools
log_info "Installing base build tools..."
apt-get install -y \
    build-essential \
    curl \
    wget \
    git \
    ca-certificates \
    gnupg \
    lsb-release

# ============================================================================
# Backend Toolchain (Go)
# ============================================================================
log_info "Installing Go toolchain..."

GO_VERSION="1.22.0"
GO_ARCH="linux-amd64"

if ! command -v go &> /dev/null; then
    log_info "Downloading Go ${GO_VERSION}..."
    cd /tmp
    wget -q "https://go.dev/dl/go${GO_VERSION}.${GO_ARCH}.tar.gz"
    rm -rf /usr/local/go
    tar -C /usr/local -xzf "go${GO_VERSION}.${GO_ARCH}.tar.gz"
    rm "go${GO_VERSION}.${GO_ARCH}.tar.gz"
    
    # Add Go to PATH in profile
    if ! grep -q "/usr/local/go/bin" /etc/profile; then
        echo 'export PATH=$PATH:/usr/local/go/bin' >> /etc/profile
    fi
    
    # Add to current session
    export PATH=$PATH:/usr/local/go/bin
    
    log_info "Go ${GO_VERSION} installed successfully"
else
    INSTALLED_VERSION=$(go version | awk '{print $3}' | sed 's/go//')
    log_info "Go is already installed (version: ${INSTALLED_VERSION})"
fi

# ============================================================================
# Frontend Toolchain (Node.js + pnpm)
# ============================================================================
log_info "Installing Node.js and pnpm..."

# Install Node.js 20.x LTS via NodeSource
if ! command -v node &> /dev/null; then
    log_info "Installing Node.js 20.x LTS..."
    curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
    apt-get install -y nodejs
    log_info "Node.js installed successfully"
else
    INSTALLED_VERSION=$(node --version)
    log_info "Node.js is already installed (version: ${INSTALLED_VERSION})"
fi

# Install pnpm
if ! command -v pnpm &> /dev/null; then
    log_info "Installing pnpm..."
    npm install -g pnpm
    log_info "pnpm installed successfully"
else
    INSTALLED_VERSION=$(pnpm --version)
    log_info "pnpm is already installed (version: ${INSTALLED_VERSION})"
fi

# ============================================================================
# System-Level Dependencies
# ============================================================================

# Disk Storage Layer
log_info "Installing disk storage tools (LVM2, XFS, etc.)..."
apt-get install -y \
    lvm2 \
    xfsprogs \
    thin-provisioning-tools \
    smartmontools \
    nvme-cli \
    parted \
    gdisk

# Physical Tape Subsystem
log_info "Installing physical tape tools..."
apt-get install -y \
    lsscsi \
    sg3-utils \
    mt-st \
    mtx

# iSCSI Initiator Tools (for testing)
log_info "Installing iSCSI initiator tools..."
apt-get install -y \
    open-iscsi \
    iscsiadm

# ============================================================================
# PostgreSQL
# ============================================================================
log_info "Installing PostgreSQL..."

if ! command -v psql &> /dev/null; then
    apt-get install -y \
        postgresql \
        postgresql-contrib \
        libpq-dev
    
    # Start and enable PostgreSQL
    systemctl enable postgresql
    systemctl start postgresql
    
    log_info "PostgreSQL installed and started"
else
    log_info "PostgreSQL is already installed"
    # Ensure it's running
    systemctl start postgresql || true
fi

# ============================================================================
# SCST Prerequisites
# ============================================================================
log_info "Installing SCST prerequisites..."
apt-get install -y \
    dkms \
    linux-headers-$(uname -r) \
    build-essential

log_info "SCST prerequisites installed (SCST itself will be built from source)"

# ============================================================================
# Additional System Tools
# ============================================================================
log_info "Installing additional system tools..."
apt-get install -y \
    jq \
    uuid-runtime \
    net-tools \
    iproute2 \
    systemd \
    chrony

# ============================================================================
# Verification Section
# ============================================================================
log_info ""
log_info "=========================================="
log_info "Installation Complete - Verification"
log_info "=========================================="
log_info ""

# Verify Go
if command -v go &> /dev/null; then
    GO_VER=$(go version | awk '{print $3}')
    log_info "✓ Go: ${GO_VER}"
    log_info "  Location: $(which go)"
else
    log_error "✗ Go: NOT FOUND"
    log_warn "  You may need to log out and back in, or run: export PATH=\$PATH:/usr/local/go/bin"
fi

# Verify Node.js
if command -v node &> /dev/null; then
    NODE_VER=$(node --version)
    log_info "✓ Node.js: ${NODE_VER}"
    log_info "  Location: $(which node)"
else
    log_error "✗ Node.js: NOT FOUND"
fi

# Verify pnpm
if command -v pnpm &> /dev/null; then
    PNPM_VER=$(pnpm --version)
    log_info "✓ pnpm: ${PNPM_VER}"
    log_info "  Location: $(which pnpm)"
else
    log_error "✗ pnpm: NOT FOUND"
fi

# Verify PostgreSQL
if systemctl is-active --quiet postgresql; then
    PG_VER=$(psql --version | awk '{print $3}')
    log_info "✓ PostgreSQL: ${PG_VER} (running)"
    log_info "  Status: $(systemctl is-active postgresql)"
else
    log_error "✗ PostgreSQL: NOT RUNNING"
    log_warn "  Start with: sudo systemctl start postgresql"
fi

# Verify required binaries
log_info ""
log_info "Required binaries verification:"

REQUIRED_BINS=(
    "lvm" "pvcreate" "vgcreate" "lvcreate"
    "mkfs.xfs" "xfs_admin"
    "lsscsi" "sg_inq" "mt" "mtx"
    "iscsiadm"
    "psql"
)

MISSING_BINS=()
for bin in "${REQUIRED_BINS[@]}"; do
    if command -v "$bin" &> /dev/null; then
        log_info "  ✓ ${bin}"
    else
        log_error "  ✗ ${bin} - NOT FOUND"
        MISSING_BINS+=("$bin")
    fi
done

if [[ ${#MISSING_BINS[@]} -gt 0 ]]; then
    log_warn ""
    log_warn "Some required binaries are missing. Please review the installation."
    exit 1
fi

log_info ""
log_info "=========================================="
log_info "All requirements installed successfully!"
log_info "=========================================="
log_info ""
log_info "Next steps:"
log_info "1. Verify Go: go version"
log_info "2. Verify Node: node --version && pnpm --version"
log_info "3. Verify PostgreSQL: sudo systemctl status postgresql"
log_info "4. Create database: sudo -u postgres createdb calypso"
log_info "5. Proceed with backend setup"
log_info ""

