#!/bin/bash
#
# Systemd services setup
#

install_systemd_services() {
    log_info "Installing systemd services..."
    
    # Install API service
    install_api_service
    
    # Reload systemd
    systemctl daemon-reload
    
    # Enable services
    systemctl enable calypso-api
    
    # Enable file sharing services (if installed)
    if systemctl list-unit-files | grep -q nfs-server.service; then
        systemctl enable nfs-server || true
        systemctl start nfs-server || true
        log_info "✓ NFS server enabled"
    fi
    
    if systemctl list-unit-files | grep -q smbd.service; then
        systemctl enable smbd || true
        systemctl enable nmbd || true
        systemctl start smbd || true
        systemctl start nmbd || true
        log_info "✓ Samba services enabled"
    fi
    
    # Enable ClamAV services (if installed)
    if systemctl list-unit-files | grep -q clamav-daemon.service; then
        systemctl enable clamav-daemon || true
        systemctl enable clamav-freshclam || true
        systemctl start clamav-daemon || true
        systemctl start clamav-freshclam || true
        log_info "✓ ClamAV services enabled"
    fi
    
    log_info "✓ Systemd services installed"
}

install_api_service() {
    log_info "Installing calypso-api service..."
    
    cat > /etc/systemd/system/calypso-api.service <<EOF
[Unit]
Description=AtlasOS - Calypso API Server
After=network.target postgresql.service
Requires=postgresql.service

[Service]
Type=simple
User=calypso
Group=calypso
WorkingDirectory=$INSTALL_PREFIX/current
EnvironmentFile=$CONFIG_DIR/secrets.env
ExecStart=$INSTALL_PREFIX/current/bin/calypso-api -config $CONFIG_DIR/config.yaml
Restart=always
RestartSec=5
StandardOutput=journal
StandardError=journal
SyslogIdentifier=calypso-api

# Security settings
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ProtectHome=true
ReadWritePaths=$DATA_DIR $LOG_DIR $LIB_DIR $RUN_DIR

# Resource limits
LimitNOFILE=65536
LimitNPROC=4096

[Install]
WantedBy=multi-user.target
EOF
    
    log_info "✓ API service file created"
}

verify_installation() {
    log_info "Verifying installation..."
    
    local errors=0
    
    # Check binary exists
    if [[ ! -f "$INSTALL_PREFIX/current/bin/calypso-api" ]]; then
        log_error "Binary not found: $INSTALL_PREFIX/current/bin/calypso-api"
        ((errors++))
    else
        log_info "✓ Binary exists"
    fi
    
    # Check frontend assets
    if [[ ! -d "$INSTALL_PREFIX/current/web" ]] || [[ -z "$(ls -A $INSTALL_PREFIX/current/web 2>/dev/null)" ]]; then
        log_warn "Frontend assets not found or empty"
    else
        log_info "✓ Frontend assets exist"
    fi
    
    # Check configuration
    if [[ ! -f "$CONFIG_DIR/config.yaml" ]]; then
        log_error "Configuration file not found: $CONFIG_DIR/config.yaml"
        ((errors++))
    else
        log_info "✓ Configuration file exists"
    fi
    
    # Check database connection
    export PGPASSWORD="$CALYPSO_DB_PASSWORD"
    if psql -h localhost -U calypso -d calypso -c "SELECT 1;" &>/dev/null; then
        log_info "✓ Database connection OK"
    else
        log_warn "Database connection test failed"
    fi
    unset PGPASSWORD
    
    # Check service file
    if [[ ! -f "/etc/systemd/system/calypso-api.service" ]]; then
        log_error "Service file not found"
        ((errors++))
    else
        log_info "✓ Service file exists"
    fi
    
    if [[ $errors -gt 0 ]]; then
        log_error "Installation verification found $errors error(s)"
        return 1
    else
        log_info "✓ Installation verification complete"
        return 0
    fi
}

