package utils

import (
	"encoding/json"
	"fmt"
	"io"
	"net"
	"net/http"
	"net/url"
	"time"
)

const dnsAPIBase = "https://cf-dns.smyhub.com/api/dns?domain="

type dnsResponse struct {
	Status string   `json:"status"`
	IPv4   []string `json:"ipv4"`
	IPv6   []string `json:"ipv6"`
}

// ResolveDomainIPs 通过 DNS API 解析域名的 IPv4 + IPv6 地址
func ResolveDomainIPs(rawURL string) ([]string, error) {
	parsed, err := url.Parse(rawURL)
	if err != nil {
		return nil, err
	}

	hostname := parsed.Hostname()
	if hostname == "" {
		return nil, fmt.Errorf("no hostname in URL")
	}

	if net.ParseIP(hostname) != nil {
		return []string{hostname}, nil
	}

	client := &http.Client{Timeout: 10 * time.Second}
	resp, err := client.Get(dnsAPIBase + hostname)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(io.LimitReader(resp.Body, 1024*10))
	if err != nil {
		return nil, err
	}

	var dnsResp dnsResponse
	if err := json.Unmarshal(body, &dnsResp); err != nil {
		return nil, err
	}

	if dnsResp.Status != "success" {
		return nil, fmt.Errorf("DNS lookup failed for %s", hostname)
	}

	ips := append(dnsResp.IPv4, dnsResp.IPv6...)
	return ips, nil
}
