package config

import "fmt"

// NodeType represents the type of network node
type NodeType string

const (
	NodeTypePBX         NodeType = "PBX"
	NodeTypeProxy       NodeType = "Proxy"
	NodeTypeMediaServer NodeType = "MediaServer"
	NodeTypeGateway     NodeType = "Gateway"
	NodeTypeEndpoint    NodeType = "Endpoint"
	NodeTypeUnknown     NodeType = "Unknown"
)

// NetworkNode represents a network entity in the SIP infrastructure
type NetworkNode struct {
	Name        string   `json:"name"`
	IP          string   `json:"ip"`
	Type        NodeType `json:"type"`
	Aliases     []string `json:"aliases,omitempty"` // Alternative IPs or hostnames
	Description string   `json:"description,omitempty"`
}

// NetworkMap holds the configured network topology
type NetworkMap struct {
	Nodes []NetworkNode `json:"nodes"`
}

// NewNetworkMap creates an empty network map
func NewNetworkMap() *NetworkMap {
	return &NetworkMap{
		Nodes: make([]NetworkNode, 0),
	}
}

// AddNode adds a new node to the network map
func (nm *NetworkMap) AddNode(node NetworkNode) {
	nm.Nodes = append(nm.Nodes, node)
}

// FindByIP looks up a node by IP address
func (nm *NetworkMap) FindByIP(ip string) *NetworkNode {
	for i := range nm.Nodes {
		if nm.Nodes[i].IP == ip {
			return &nm.Nodes[i]
		}
		for _, alias := range nm.Nodes[i].Aliases {
			if alias == ip {
				return &nm.Nodes[i]
			}
		}
	}
	return nil
}

// LabelForIP returns a human-readable label for an IP, or the IP itself if unknown
func (nm *NetworkMap) LabelForIP(ip string) string {
	if node := nm.FindByIP(ip); node != nil {
		return fmt.Sprintf("%s (%s)", node.Name, node.Type)
	}
	return ip
}
