package reservation

import (
	"context"
	"dhcp/internal/dhcpServer/data"
	"github.com/insomniacslk/dhcp/dhcpv4"
	"net/netip"
)

// setDHCPOpts takes a client dhcpServer packet and data (typically from a macAndIPXE) and creates a slice of dhcpServer packet modifiers.
// m is the dhcpServer request from a client. d is the data to use to create the dhcpServer packet modifiers.
// This is most likely the place where we would have any business logic for determining dhcpServer option setting.
func (h *Handler) setDHCPOpts(_ context.Context, _ *dhcpv4.DHCPv4, d *data.DHCP) []dhcpv4.Modifier {
	mods := []dhcpv4.Modifier{
		dhcpv4.WithLeaseTime(d.LeaseTime),
		dhcpv4.WithYourIP(d.IPAddress.AsSlice()),
	}
	if len(d.NameServers) > 0 {
		mods = append(mods, dhcpv4.WithDNS(d.NameServers...))
	}
	if len(d.DomainSearch) > 0 {
		mods = append(mods, dhcpv4.WithDomainSearchList(d.DomainSearch...))
	}
	if len(d.NTPServers) > 0 {
		mods = append(mods, dhcpv4.WithOption(dhcpv4.OptNTPServers(d.NTPServers...)))
	}
	if d.BroadcastAddress.Compare(netip.Addr{}) != 0 {
		mods = append(mods, dhcpv4.WithGeneric(dhcpv4.OptionBroadcastAddress, d.BroadcastAddress.AsSlice()))
	}
	if d.DomainName != "" {
		mods = append(mods, dhcpv4.WithGeneric(dhcpv4.OptionDomainName, []byte(d.DomainName)))
	}
	if len(d.SubnetMask) > 0 {
		mods = append(mods, dhcpv4.WithNetmask(d.SubnetMask))
	}
	if d.DefaultGateway.Compare(netip.Addr{}) != 0 {
		mods = append(mods, dhcpv4.WithRouter(d.DefaultGateway.AsSlice()))
	}

	return mods
}
