package uiapi

import (
	"context"
	"testing"
	"time"
)

// TestResolveObjectTypeWithHint verifies hint is returned when provided
func TestResolveObjectTypeWithHint(t *testing.T) {
	// When hintType is provided, it should be returned directly
	// This test validates the fast path without mocking dependencies

	// Create a minimal service with nil dependencies (only testing hint path)
	svc := &Service{
		objCache:    newCache(time.Minute),
		layoutCache: newCache(time.Minute),
	}
	defer svc.Close()

	objectType, err := svc.ResolveObjectType(context.Background(), "any-id", "account", nil)
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if objectType != "account" {
		t.Errorf("expected 'account', got %q", objectType)
	}
}

// TestResolveObjectTypeWithEmptyHint tests fallback behavior
func TestResolveObjectTypeWithEmptyHint(t *testing.T) {
	// When hint is empty, the service attempts to resolve by querying schemas
	// Without proper mocks, this returns empty string (not an error)

	svc := &Service{
		metadataSvc: nil, // Will cause early return without querying
		recordSvc:   nil,
		objCache:    newCache(time.Minute),
		layoutCache: newCache(time.Minute),
	}
	defer svc.Close()

	// This will panic due to nil metadataSvc - skip for unit test
	// Integration tests with proper mocks should cover this path
	t.Skip("Requires mock MetadataService for full coverage")
}

// TestClose verifies graceful shutdown and idempotency
func TestClose(t *testing.T) {
	svc := &Service{
		objCache:    newCache(time.Minute), // Use non-zero TTL
		layoutCache: newCache(time.Minute),
	}

	// First close - should not panic
	svc.Close()

	// Second close - should still not panic (idempotent via sync.Once)
	svc.Close()
}

// TestNewServiceCachesInitialized tests constructor
func TestNewServiceCachesInitialized(t *testing.T) {
	// NewService requires non-nil dependencies, but we test Close is safe
	svc := &Service{
		objCache:    nil,
		layoutCache: nil,
	}
	// Close should handle nil caches gracefully
	svc.Close()
}
