package geo

import (
	"os"
	"path/filepath"
	"testing"
)

func TestLookupValidIP(t *testing.T) {
	dbPath := filepath.Join("..", "..", "GeoLite2-City.mmdb")
	if _, err := os.Stat(dbPath); err != nil {
		t.Skipf("mmdb not available at %s: %v", dbPath, err)
	}

	resolver, err := NewResolver(Config{
		Backend:  BackendMMDB,
		MMDBPath: dbPath,
	})
	if err != nil {
		t.Fatalf("failed to open db: %v", err)
	}
	defer resolver.Close()

	loc, err := resolver.Lookup("1.1.1.1")
	if err != nil {
		t.Fatalf("lookup failed: %v", err)
	}

	if loc.IP != "1.1.1.1" {
		t.Errorf("unexpected IP: %s", loc.IP)
	}
}

func TestLookupInvalidIP(t *testing.T) {
	dbPath := filepath.Join("..", "..", "GeoLite2-City.mmdb")
	if _, err := os.Stat(dbPath); err != nil {
		t.Skipf("mmdb not available at %s: %v", dbPath, err)
	}

	resolver, err := NewResolver(Config{
		Backend:  BackendMMDB,
		MMDBPath: dbPath,
	})
	if err != nil {
		t.Fatalf("failed to open db: %v", err)
	}
	defer resolver.Close()

	if _, err := resolver.Lookup("not-an-ip"); err == nil {
		t.Fatal("expected error for invalid IP")
	}
}
