#!/bin/bash

# C Nostr Relay Event-Based Configuration System - Installation Script
# This script installs the C Nostr Relay as a systemd service

set -e

# Configuration
SERVICE_NAME="c-relay"
SERVICE_USER="c-relay"
INSTALL_DIR="/opt/c-relay"
BINARY_NAME="c_relay_x86"

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

# Function to print colored output
print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

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

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

print_info "Installing C Nostr Relay with Event-Based Configuration System"
echo

# Check if binary exists
if [ ! -f "build/${BINARY_NAME}" ]; then
    print_error "Binary build/${BINARY_NAME} not found. Please build the project first."
    exit 1
fi

# Create service user
if ! id "${SERVICE_USER}" &>/dev/null; then
    print_info "Creating service user: ${SERVICE_USER}"
    useradd --system --home-dir "${INSTALL_DIR}" --shell /bin/false "${SERVICE_USER}"
    print_success "Service user created"
else
    print_info "Service user ${SERVICE_USER} already exists"
fi

# Create installation directory
print_info "Creating installation directory: ${INSTALL_DIR}"
mkdir -p "${INSTALL_DIR}"
chown "${SERVICE_USER}:${SERVICE_USER}" "${INSTALL_DIR}"

# Copy binary
print_info "Installing binary to ${INSTALL_DIR}/${BINARY_NAME}"
cp "build/${BINARY_NAME}" "${INSTALL_DIR}/"
chown "${SERVICE_USER}:${SERVICE_USER}" "${INSTALL_DIR}/${BINARY_NAME}"
chmod +x "${INSTALL_DIR}/${BINARY_NAME}"

# Install systemd service file
print_info "Installing systemd service file"
cp "systemd/${SERVICE_NAME}.service" "/etc/systemd/system/"

# Reload systemd
print_info "Reloading systemd daemon"
systemctl daemon-reload

print_success "Installation complete!"
echo
print_info "Event-Based Configuration System Information:"
echo "  • No configuration files needed - all config stored as Nostr events"
echo "  • Database files are created automatically as <relay_pubkey>.nrdb"
echo "  • Admin keys are generated and displayed during first startup"
echo "  • Configuration is updated via WebSocket with kind 33334 events"
echo
print_info "To start the service:"
echo "  sudo systemctl start ${SERVICE_NAME}"
echo
print_info "To enable automatic startup:"
echo "  sudo systemctl enable ${SERVICE_NAME}"
echo
print_info "To view service status:"
echo "  sudo systemctl status ${SERVICE_NAME}"
echo
print_info "To view logs:"
echo "  sudo journalctl -u ${SERVICE_NAME} -f"
echo
print_warning "IMPORTANT: On first startup, save the admin private key displayed in the logs!"
print_warning "Use: sudo journalctl -u ${SERVICE_NAME} --since=\"1 hour ago\" | grep \"Admin Private Key\""
echo
print_info "Database files will be created in: ${INSTALL_DIR}/<relay_pubkey>.nrdb"
print_info "The relay will listen on port 8888 by default (configured via Nostr events)"