#!/bin/bash

# Delete a peer by name. Doesn't touch backups, SSH keys, or any actual peer
# state on the other host — just removes the local label.

peerRemove()
{
    local name="$1"
    if [[ -z "$name" ]]; then isError "peerRemove: name required"; return 1; fi

    local existing
    existing=$(sqlite3 "$(_peerDb)" "SELECT id FROM peers WHERE name='$(peerSqlEscape "$name")';" 2>/dev/null)
    if [[ -z "$existing" ]]; then
        isNotice "No peer named '$name'"
        return 0
    fi

    sqlite3 "$(_peerDb)" "DELETE FROM peers WHERE name='$(peerSqlEscape "$name")';" 2>/dev/null
    if [[ $? -eq 0 ]]; then
        isSuccessful "Peer '$name' removed"
        declare -F webuiGeneratePeers >/dev/null 2>&1 && webuiGeneratePeers >/dev/null 2>&1 || true
        return 0
    else
        isError "Failed to remove peer '$name'"
        return 1
    fi
}
