package render

import (
	"testing"

	"leadb2b.org/internal/api"
)

func TestTelefoneCount(t *testing.T) {
	d1, t1, d2, t2 := "11", "99999999", "11", "88888888"
	item := api.EstabelecimentoOut{
		DDD1:      &d1,
		Telefone1: &t1,
		DDD2:      &d2,
		Telefone2: &t2,
	}
	if got := TelefoneCount(item); got != 2 {
		t.Fatalf("got %d want 2", got)
	}
}

func TestListContactBadges(t *testing.T) {
	d1, t1, email := "11", "63167812", "a@b.com"
	item := api.EstabelecimentoOut{
		CNPJBasico:        "07367073",
		DDD1:              &d1,
		Telefone1:         &t1,
		CorreioEletronico: &email,
	}
	badges := ListContactBadges(item, map[string]int{"07367073": 2})
	if len(badges) != 3 {
		t.Fatalf("badges: got %d want 3", len(badges))
	}
	if badges[0].Label != "1 telefone" || badges[1].Label != "1 e-mail" || badges[2].Label != "2 sócios" {
		t.Fatalf("labels: %+v", badges)
	}
}

func TestEnrichMaskedSearchCNPJs(t *testing.T) {
	masked := []api.MaskedCompanyOut{{
		CNPJMasked: "**." + "123" + "." + "456" + "/" + "7890" + "-**",
	}}
	full := []api.EstabelecimentoOut{{CNPJ: "07123456789012"}}
	out := EnrichMaskedSearchCNPJs(masked, full)
	if out[0].CNPJ == nil || *out[0].CNPJ != "07123456789012" {
		t.Fatalf("cnpj not enriched: %+v", out[0].CNPJ)
	}
	if got := MaskedCompanyCNPJ(out[0]); got != "07123456789012" {
		t.Fatalf("MaskedCompanyCNPJ: got %q", got)
	}
}

func TestEnrichMaskedSearchCNPJs_indexAlign(t *testing.T) {
	name := "Imobiliaria Exemplo"
	city := "Sao Paulo"
	uf := "SP"
	masked := []api.MaskedCompanyOut{{
		CNPJMasked:  "**.0001/0001-**",
		CompanyName: &name,
		City:        &city,
		UF:          &uf,
	}}
	full := []api.EstabelecimentoOut{{
		CNPJ:          "07367073000165",
		RazaoSocial:   "IMOBILIARIA EXEMPLO LTDA",
		MunicipioNome: "Sao Paulo",
		UF:            "SP",
	}}
	out := EnrichMaskedSearchCNPJs(masked, full)
	if got := MaskedCompanyCNPJ(out[0]); got != "07367073000165" {
		t.Fatalf("index align: got %q", got)
	}
}

func TestPartialCNPJMatchesFull_shortMask(t *testing.T) {
	if !partialCNPJMatchesFull("07367073000165", "**.0001/0001-**") {
		t.Fatal("expected branch digits to match short mask")
	}
}
