package integration

// ObjectLabels must resolve against a real database. A pure unit test cannot
// catch encoding-level failures: pgtype.Array without explicit Dims encoded as
// an empty array, so the query ran fine and returned nothing — green tests,
// dead feature. This hits a throwaway database like the rest of this package.
import (
	"context"
	"testing"

	"github.com/TaruLab/keluargamart-backend/internal/system"
)

func TestObjectLabelsResolves(t *testing.T) {
	pool := testPool(t)
	repo := system.NewRepository(pool)
	ctx := context.Background()

	var id int64
	if err := pool.QueryRow(ctx,
		`INSERT INTO users (name, email, password) VALUES ('Label Probe', 'label-probe@keluargamart.local', 'x') RETURNING id`,
	).Scan(&id); err != nil {
		t.Fatal(err)
	}
	t.Cleanup(func() {
		_, _ = pool.Exec(context.Background(), `DELETE FROM users WHERE id = $1`, id)
	})
	got, err := repo.ObjectLabels(ctx, "users", []int64{id})
	if err != nil {
		t.Fatal(err)
	}
	if got[id] != "Label Probe" {
		t.Fatalf("ObjectLabels(users,[%d]) = %v, want the probe name", id, got)
	}

	// Unknown types resolve to nothing, not an error: the list must not fail
	// because one row references something unmapped.
	got, err = repo.ObjectLabels(ctx, "no-such-type", []int64{1})
	if err != nil {
		t.Fatal(err)
	}
	if len(got) != 0 {
		t.Fatalf("unknown type resolved %v, want empty", got)
	}

	// Missing rows resolve to nothing.
	got, err = repo.ObjectLabels(ctx, "users", []int64{999999})
	if err != nil {
		t.Fatal(err)
	}
	if len(got) != 0 {
		t.Fatalf("missing id resolved %v, want empty", got)
	}
}
