package proxmox

import (
	"context"
	"fmt"

	"github.com/pkg/errors"
)

// Network represents a Proxmox network bridge
type Network struct {
	Name    string `json:"iface"`
	Type    string `json:"type"`
	Active  bool   `json:"active"`
	Address string `json:"address,omitempty"`
}

// ListNetworks lists all network bridges on a node
func (c *Client) ListNetworks(ctx context.Context, node string) ([]Network, error) {
	var networks []Network
	if err := c.httpClient.Get(ctx, fmt.Sprintf("/nodes/%s/network", node), &networks); err != nil {
		return nil, errors.Wrapf(err, "failed to list networks on node %s", node)
	}
	return networks, nil
}

// NetworkExists checks if a network bridge exists on a node
func (c *Client) NetworkExists(ctx context.Context, node, networkName string) (bool, error) {
	networks, err := c.ListNetworks(ctx, node)
	if err != nil {
		return false, err
	}

	for _, net := range networks {
		if net.Name == networkName {
			return true, nil
		}
	}

	return false, nil
}

