package config

import (
	"encoding/json"
	"os"
)

// LoadNetworkMap loads a network map from a JSON file
func LoadNetworkMap(path string) (*NetworkMap, error) {
	data, err := os.ReadFile(path)
	if err != nil {
		return nil, err
	}

	var nm NetworkMap
	if err := json.Unmarshal(data, &nm); err != nil {
		return nil, err
	}

	return &nm, nil
}

// SaveNetworkMap saves a network map to a JSON file
func SaveNetworkMap(nm *NetworkMap, path string) error {
	data, err := json.MarshalIndent(nm, "", "  ")
	if err != nil {
		return err
	}

	return os.WriteFile(path, data, 0644)
}

// DefaultNetworkMapPath returns a default path for the network map file
func DefaultNetworkMapPath() string {
	return "network_map.json"
}

// CreateSampleNetworkMap creates a sample network map for testing
func CreateSampleNetworkMap() *NetworkMap {
	nm := NewNetworkMap()

	nm.AddNode(NetworkNode{
		Name:        "Asterisk PBX",
		IP:          "192.168.1.10",
		Type:        NodeTypePBX,
		Description: "Main PBX server",
	})

	nm.AddNode(NetworkNode{
		Name:        "Kamailio Proxy",
		IP:          "192.168.1.20",
		Type:        NodeTypeProxy,
		Aliases:     []string{"10.0.0.20"},
		Description: "SIP proxy/load balancer",
	})

	nm.AddNode(NetworkNode{
		Name:        "RTPEngine",
		IP:          "192.168.1.30",
		Type:        NodeTypeMediaServer,
		Description: "Media relay server",
	})

	nm.AddNode(NetworkNode{
		Name:        "PSTN Gateway",
		IP:          "192.168.1.40",
		Type:        NodeTypeGateway,
		Description: "Gateway to PSTN",
	})

	return nm
}
