# keepass_delete
# --------------
# Elimina una entry del KeePassXC database.
#
# REQUIERE:
#   - keepassxc-cli instalado
#   - KEEPASS_DB (env): ruta absoluta al .kdbx
#   - master password en pass o env KEEPASS_PASSWORD
#
# USO (sourced):
#   source keepass_delete.sh
#   keepass_delete "Servers/old-server"

keepass_delete() {
    local entry="$1"

    if [ -z "$entry" ]; then
        echo "keepass_delete: se requiere entry" >&2
        return 1
    fi

    local db="${KEEPASS_DB:-}"
    if [ -z "$db" ] || [ ! -f "$db" ]; then
        echo "keepass_delete: KEEPASS_DB no valida: $db" >&2
        return 1
    fi

    local master
    if [ -n "${KEEPASS_PASSWORD:-}" ]; then
        master="$KEEPASS_PASSWORD"
    else
        master=$(pass show "${KEEPASS_MASTER_ENTRY:-meta/keepassxc-master}" 2>/dev/null | head -n1)
        if [ -z "$master" ]; then
            echo "keepass_delete: no master pass" >&2
            return 1
        fi
    fi

    printf '%s\n' "$master" | keepassxc-cli rm -q "$db" "$entry" >/dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "keepass_delete: fallo al borrar '$entry'" >&2
        return 1
    fi
}
