package idem

import (
	"testing"
)

func TestFingerprint_StableAndTrimmed(t *testing.T) {
	svc := New()

	a, err := svc.Fingerprint(FPInput{
		OrganizationRef:  "  65f1a2c6f3c5e2e7a1b2c3d4  ",
		QuotationRef:     "  quote-1  ",
		IntentRef:        "  intent-1  ",
		ClientPaymentRef: "  client-1  ",
	})
	if err != nil {
		t.Fatalf("Fingerprint returned error: %v", err)
	}
	b, err := svc.Fingerprint(FPInput{
		OrganizationRef:  "65F1A2C6F3C5E2E7A1B2C3D4",
		QuotationRef:     "quote-1",
		IntentRef:        "intent-1",
		ClientPaymentRef: "client-1",
	})
	if err != nil {
		t.Fatalf("Fingerprint returned error: %v", err)
	}
	if a != b {
		t.Fatalf("expected deterministic fingerprint, got %q vs %q", a, b)
	}
}

func TestFingerprint_ChangesOnPayload(t *testing.T) {
	svc := New()

	base, err := svc.Fingerprint(FPInput{
		OrganizationRef:  "65f1a2c6f3c5e2e7a1b2c3d4",
		QuotationRef:     "quote-1",
		IntentRef:        "intent-1",
		ClientPaymentRef: "client-1",
	})
	if err != nil {
		t.Fatalf("Fingerprint returned error: %v", err)
	}

	diffQuote, err := svc.Fingerprint(FPInput{
		OrganizationRef:  "65f1a2c6f3c5e2e7a1b2c3d4",
		QuotationRef:     "quote-2",
		IntentRef:        "intent-1",
		ClientPaymentRef: "client-1",
	})
	if err != nil {
		t.Fatalf("Fingerprint returned error: %v", err)
	}
	if base == diffQuote {
		t.Fatalf("expected different fingerprint for different quotation_ref")
	}

	diffClient, err := svc.Fingerprint(FPInput{
		OrganizationRef:  "65f1a2c6f3c5e2e7a1b2c3d4",
		QuotationRef:     "quote-1",
		IntentRef:        "intent-1",
		ClientPaymentRef: "client-2",
	})
	if err != nil {
		t.Fatalf("Fingerprint returned error: %v", err)
	}
	if base == diffClient {
		t.Fatalf("expected different fingerprint for different client_payment_ref")
	}

	diffIntent, err := svc.Fingerprint(FPInput{
		OrganizationRef:  "65f1a2c6f3c5e2e7a1b2c3d4",
		QuotationRef:     "quote-1",
		IntentRef:        "intent-2",
		ClientPaymentRef: "client-1",
	})
	if err != nil {
		t.Fatalf("Fingerprint returned error: %v", err)
	}
	if base == diffIntent {
		t.Fatalf("expected different fingerprint for different intent_ref")
	}
}

func TestFingerprint_RequiresBusinessFields(t *testing.T) {
	svc := New()

	if _, err := svc.Fingerprint(FPInput{
		QuotationRef: "quote-1",
	}); err == nil {
		t.Fatal("expected error for empty organization_ref")
	}

	if _, err := svc.Fingerprint(FPInput{
		OrganizationRef: "65f1a2c6f3c5e2e7a1b2c3d4",
	}); err == nil {
		t.Fatal("expected error for empty quotation_ref")
	}
}
