import { fireEvent, render, screen } from '@testing-library/react';
import { describe, expect, it, vi } from 'vitest';

import { RecordLink } from '../src/platform/components/RecordLink';
import { createCrmUiFieldComponents, type CrmUiFieldHost } from '../src/ui-field-components';

// The lookup renderer decides link-vs-plain-text for reference fields. A link
// must promise navigation to a real record. Record Type (and other metadata
// lookups) resolve to platform metadata, not a user-facing record, so they must
// render as plain text on every surface — not a blue link that goes nowhere.

const host: CrmUiFieldHost = {
  SearchableLookup: () => null,
  RecordLink,
  decodeHtmlEntities: value => value,
  formatCurrency: value => String(value),
  formatDate: value => value,
  formatDateTime: value => value,
  getRecordValue: (record, fieldName) => record?.[fieldName],
  parseMultipicklistSelections: value => (Array.isArray(value) ? value.map(String) : []),
  joinMultipicklistSelections: values => values.join(';'),
};

const { fieldRenderers } = createCrmUiFieldComponents(host);
const LookupRenderer = fieldRenderers.reference;

describe('LookupRenderer navigability rule', () => {
  it('renders a Record Type (metadata) lookup as plain text in the detail variant', () => {
    const onNavigate = vi.fn();
    render(
      <LookupRenderer
        field={{ name: 'RecordTypeId', label: 'Record Type', type: 'reference', referenceTo: ['RecordType'] }}
        value="012000000000001"
        record={{ RecordTypeId_Name: 'Expansion' }}
        variant="detail"
        onNavigate={onNavigate}
      />,
    );

    expect(screen.getByText('Expansion')).toBeInTheDocument();
    // No link/button affordance, and nothing to click.
    expect(screen.queryByRole('link')).toBeNull();
    expect(screen.queryByRole('button')).toBeNull();
    expect(onNavigate).not.toHaveBeenCalled();
  });

  it('renders a business-object lookup as a real anchor with an href', () => {
    const onNavigate = vi.fn();
    render(
      <LookupRenderer
        field={{ name: 'AccountId', label: 'Account', type: 'reference', referenceTo: ['Account'] }}
        value="001000000000001"
        record={{ AccountId_Name: 'Acme Corporation' }}
        variant="detail"
        onNavigate={onNavigate}
      />,
    );

    const link = screen.getByRole('link', { name: 'Acme Corporation' });
    expect(link).toHaveClass('text-blue-600');
    expect(link).toHaveAttribute('href', '/object/Account/001000000000001');
  });

  it('intercepts a plain click for in-app navigation but lets a modifier-click open a new tab', () => {
    const onNavigate = vi.fn();
    render(
      <LookupRenderer
        field={{ name: 'AccountId', label: 'Account', type: 'reference', referenceTo: ['Account'] }}
        value="001000000000001"
        record={{ AccountId_Name: 'Acme Corporation' }}
        variant="detail"
        onNavigate={onNavigate}
      />,
    );
    const link = screen.getByRole('link', { name: 'Acme Corporation' });

    fireEvent.click(link);
    expect(onNavigate).toHaveBeenCalledWith('Account', '001000000000001');

    onNavigate.mockClear();
    fireEvent.click(link, { metaKey: true });
    expect(onNavigate).not.toHaveBeenCalled();
  });
});
