package resolver

import "ekdns/platform"

// BuildResolverPool constructs the ordered list of DNS resolver addresses for
// the given ServerMode and discovered NetworkInfo.
//
// Modes:
//   - "default"  → local DNS servers + bootstrap resolvers (deduplicated, local first)
//   - "local"    → only the local DNS servers from NetworkInfo.DNSServers
//   - "gateway"  → only NetworkInfo.Gateway
//   - "explicit" → only ServerMode.ExplicitAddr
//
// Returns nil for unknown modes. Callers should validate that the pool is
// non-empty before proceeding (e.g. gateway mode with no discovered gateway).
func BuildResolverPool(mode ServerMode, info platform.NetworkInfo) []string {
	switch mode.Mode {
	case "default":
		seen := make(map[string]bool, len(info.DNSServers)+len(BootstrapResolvers))
		pool := make([]string, 0, len(info.DNSServers)+len(BootstrapResolvers))

		for _, r := range info.DNSServers {
			if !seen[r] {
				seen[r] = true
				pool = append(pool, r)
			}
		}
		for _, r := range BootstrapResolvers {
			if !seen[r] {
				seen[r] = true
				pool = append(pool, r)
			}
		}
		return pool

	case "local":
		return info.DNSServers

	case "gateway":
		return []string{info.Gateway}

	case "explicit":
		return []string{mode.ExplicitAddr}

	default:
		return nil
	}
}
