import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { describe, expect, it, vi } from 'vitest';
import { CrmConvertedLeadBanner } from '../src/converted-lead-banner';

function makeHost() {
  return {
    navigateToRecord: vi.fn(),
  };
}

describe('CrmConvertedLeadBanner', () => {
  it('renders app-owned converted Lead target links', () => {
    render(
      <CrmConvertedLeadBanner
        record={{
          Id: '00Q000000001',
          IsConverted: true,
          ConvertedAccountId: '001ACCT001',
          ConvertedContactId: '003CON001',
          ConvertedOpportunityId: '006OPP001',
        }}
        host={makeHost()}
      />,
    );

    expect(screen.getByText(/converted/i)).toBeInTheDocument();
    expect(screen.getByRole('button', { name: /view account/i })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: /view contact/i })).toBeInTheDocument();
    expect(screen.getByRole('button', { name: /view opportunity/i })).toBeInTheDocument();
  });

  it('omits absent target links without host-side CRM assumptions', () => {
    render(
      <CrmConvertedLeadBanner
        record={{
          Id: '00Q000000001',
          ConvertedAccountId: '001ACCT001',
          ConvertedContactId: null,
          ConvertedOpportunityId: null,
        }}
        host={makeHost()}
      />,
    );

    expect(screen.getByRole('button', { name: /view account/i })).toBeInTheDocument();
    expect(screen.queryByRole('button', { name: /view contact/i })).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: /view opportunity/i })).not.toBeInTheDocument();
  });

  it('navigates through the host panel service', async () => {
    const host = makeHost();
    render(
      <CrmConvertedLeadBanner
        record={{
          ConvertedAccountId: '001ACCT001',
        }}
        host={host}
      />,
    );

    await userEvent.click(screen.getByRole('button', { name: /view account/i }));
    expect(host.navigateToRecord).toHaveBeenCalledWith('Account', '001ACCT001');
  });
});
