import { LocalMessage } from 'stream-chat'
import { describe, expect, it } from 'vitest'

import { renderWithProviders, screen } from '../../../test/utils'

import { TipMessage } from '.'

interface MockMessageOptions {
  metadata?: {
    custom_type?: 'MESSAGE_TIP'
    amount_text?: string
  }
  text?: string
}

const createMockMessage = (options?: MockMessageOptions): LocalMessage =>
  ({
    id: 'msg-1',
    text: options?.text ?? 'Hello world',
    type: 'regular',
    created_at: new Date(),
    updated_at: new Date(),
    metadata: options?.metadata,
  }) as LocalMessage

describe('TipMessage', () => {
  it('renders tip pill regardless of accompanying text', () => {
    renderWithProviders(
      <TipMessage
        message={createMockMessage({
          metadata: { custom_type: 'MESSAGE_TIP', amount_text: '$5.50' },
        })}
      />
    )

    const tipTag = screen.getByText('$5.50 tip!')
    expect(tipTag.closest('.message-tip-pill')).not.toBeNull()
  })

  it('renders tip pill label for textless tip messages', () => {
    renderWithProviders(
      <TipMessage
        message={createMockMessage({
          text: '',
          metadata: { custom_type: 'MESSAGE_TIP', amount_text: '$50.00' },
        })}
      />
    )

    const tipTag = screen.getByText('$50.00 tip!')
    expect(tipTag.closest('.message-tip-pill')).not.toBeNull()
  })

  it('returns null when amount_text is missing', () => {
    const { container } = renderWithProviders(
      <TipMessage
        message={createMockMessage({
          metadata: { custom_type: 'MESSAGE_TIP' },
        })}
      />
    )

    expect(container.firstChild).toBeNull()
  })
})
