package render

import (
	"strings"
	"testing"

	"leadb2b.org/internal/api"
)

func TestIsPreciseResult(t *testing.T) {
	tests := []struct {
		typ   string
		want  bool
	}{
		{"house", true},
		{"building", true},
		{"road", true},
		{"city", false},
		{"town", false},
		{"administrative", false},
	}
	for _, tc := range tests {
		if got := isPreciseResult(nominatimResult{Type: tc.typ}); got != tc.want {
			t.Errorf("type %q: got %v want %v", tc.typ, got, tc.want)
		}
	}
}

func TestBuildStreetLine(t *testing.T) {
	d := &api.EstabelecimentoDetailOut{
		Endereco: api.EnderecoOut{
			TipoLogradouro: "RUA",
			Logradouro:     "EXEMPLO",
			Numero:         "100",
		},
	}
	if got := buildStreetLine(d); got != "RUA EXEMPLO, 100" {
		t.Fatalf("got %q", got)
	}
	d.Endereco.Numero = "SN"
	if got := buildStreetLine(d); got != "RUA EXEMPLO" {
		t.Fatalf("sem numero: got %q", got)
	}
}

func TestEmpresaGeocodeQueries_prefersStreetOverCity(t *testing.T) {
	d := &api.EstabelecimentoDetailOut{}
	d.Endereco.TipoLogradouro = "AV"
	d.Endereco.Logradouro = "PAULISTA"
	d.Endereco.Numero = "1000"
	d.Endereco.Bairro = "BELA VISTA"
	d.Endereco.CEP = "01310100"
	d.Endereco.UF = "SP"
	d.Endereco.Municipio.Descricao = "SAO PAULO"
	qs := empresaGeocodeQueries(d)
	if len(qs) == 0 {
		t.Fatal("expected queries")
	}
	for _, q := range qs {
		if q == "SAO PAULO, SP, Brasil" {
			t.Fatal("cidade sozinha não deve estar na lista principal")
		}
	}
	found := false
	for _, q := range qs {
		if strings.Contains(q, "AV PAULISTA") && strings.Contains(q, "SAO PAULO") {
			found = true
			break
		}
	}
	if !found {
		t.Fatalf("expected street query, got %v", qs)
	}
}
