package bot

import (
	"fmt"
	"net"

	"github.com/bwmarrin/discordgo"
)

func HandlePingCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
	// Get the host parameter
	options := i.ApplicationCommandData().Options
	host := options[0].StringValue()

	// Optionally, do a DNS lookup or ICMP ping
	ips, err := net.LookupIP(host)
	if err != nil {
		s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
			Type: discordgo.InteractionResponseChannelMessageWithSource,
			Data: &discordgo.InteractionResponseData{
				Content: fmt.Sprintf("Could not resolve host `%s`: %v", host, err),
			},
		})
		return
	}

	// // host is the user-provided hostname
	// pinger, err := probing.NewPinger(host)
	// if err != nil {
	// 	s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
	// 		Type: discordgo.InteractionResponseChannelMessageWithSource,
	// 		Data: &discordgo.InteractionResponseData{
	// 			Content: fmt.Sprintf("Error creating pinger: %v", err),
	// 		},
	// 	})
	// 	return
	// }
	// pinger.Count = 3
	// pinger.SetPrivileged(false) // <- key for Windows non-admin
	// err = pinger.Run()
	// if err != nil {
	// 	s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
	// 		Type: discordgo.InteractionResponseChannelMessageWithSource,
	// 		Data: &discordgo.InteractionResponseData{
	// 			Content: fmt.Sprintf("Error running ping: %v", err),
	// 		},
	// 	})
	// 	return
	// }
	// stats := pinger.Statistics()

	// Respond with the first IP found
	s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
		Type: discordgo.InteractionResponseChannelMessageWithSource,
		Data: &discordgo.InteractionResponseData{
			Content: fmt.Sprintf("Host `%s` resolves to %s ✅", host, ips[0].String()),
		},
	})
}
