package honeypot

import (
	_ "embed"
	"net"

	"github.com/oschwald/maxminddb-golang"
)

//go:embed Geoacumen-Country.mmdb
var mmdb []byte

func (s *HoneypotServer) LookupCountry(ip net.IP) string {
	db, err := maxminddb.FromBytes(mmdb)
	if err != nil {
		s.Logger.Warnw("Error opening geoip database", "error", err)
		return "??"
	}

	var record struct {
		Country struct {
			ISOCode string `maxminddb:"iso_code"`
		} `maxminddb:"country"`
	}

	err = db.Lookup(ip, &record)
	if err != nil {
		s.Logger.Warnw("Error doing geoip lookup", "error", err)
		return "??"
	}
	return record.Country.ISOCode
}