#!/bin/bash
#
# Post-installation setup and verification
#

post_install_setup() {
    log_info "Running post-installation setup..."
    
    # Start services
    log_info "Starting services..."
    systemctl start calypso-api || log_warn "Failed to start calypso-api service"
    
    # Wait for API to be ready
    log_info "Waiting for API to be ready..."
    local max_wait=30
    local count=0
    
    while ! curl -s http://localhost:8080/api/v1/health &>/dev/null && [[ $count -lt $max_wait ]]; do
        sleep 1
        ((count++))
    done
    
    if curl -s http://localhost:8080/api/v1/health &>/dev/null; then
        log_info "✓ API is ready"
    else
        log_warn "API did not become ready within $max_wait seconds"
    fi
    
    # Print access information
    print_access_info
    
    log_info "✓ Post-installation setup complete"
}

print_access_info() {
    local server_ip=$(hostname -I | awk '{print $1}')
    
    log_info ""
    log_info "=========================================="
    log_info "Calypso Appliance is Ready!"
    log_info "=========================================="
    log_info ""
    log_info "Access Information:"
    log_info "  Web UI:      http://$server_ip:3000"
    log_info "  API:         http://$server_ip:8080"
    log_info "  Health:      http://$server_ip:8080/api/v1/health"
    log_info ""
    log_info "Default Credentials:"
    log_info "  Username:    admin"
    log_info "  Password:    $(cat /tmp/calypso_admin_password 2>/dev/null || echo 'Check installation log')"
    log_info ""
    log_info "Configuration:"
    log_info "  Config:      /etc/calypso/config.yaml"
    log_info "  Secrets:     /etc/calypso/secrets.env"
    log_info "  Logs:        sudo journalctl -u calypso-api -f"
    log_info ""
    log_warn "IMPORTANT: Change the default admin password after first login!"
    log_info ""
}

