package contacts

import (
	"testing"

	"github.com/ultisuite/ulti-backend/internal/nextcloud"
)

func TestFieldMatchScoreExactAndPrefix(t *testing.T) {
	if got := fieldMatchScore("Alice Martin", "alice martin"); got != 1 {
		t.Fatalf("exact match: got %v want 1", got)
	}
	prefix := fieldMatchScore("alice@example.com", "alice")
	if prefix < 0.95 {
		t.Fatalf("email prefix too low: %v", prefix)
	}
}

func TestRankContactsByQueryOrder(t *testing.T) {
	contacts := []nextcloud.Contact{
		{FullName: "Jonathan Smith", Email: "jonathan@corp.com"},
		{FullName: "Jon Smith", Email: "jon@corp.com"},
		{FullName: "Bob Builder", Email: "bob@corp.com"},
	}
	ranked := rankContactsByQuery(contacts, "jon")
	if len(ranked) != 2 {
		t.Fatalf("expected 2 matches, got %d", len(ranked))
	}
	if ranked[0].FullName != "Jon Smith" {
		t.Fatalf("expected Jon Smith first, got %q", ranked[0].FullName)
	}
}

func TestRankContactsByQueryNoFuzzy(t *testing.T) {
	contacts := []nextcloud.Contact{
		{FullName: "Jonathan", Email: "jonathan@corp.com"},
	}
	ranked := rankContactsByQuery(contacts, "jhn")
	if len(ranked) != 0 {
		t.Fatalf("expected no fuzzy match, got %d", len(ranked))
	}
}
