import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { RelationshipBuilder } from '../RelationshipBuilder'

const types = [
  { id: 'uuid-artist', key: 'artist', label: 'Artist' },
  { id: 'uuid-release', key: 'release', label: 'Release' },
]

describe('RelationshipBuilder', () => {
  it('renders source + target pickers and a define button', () => {
    render(<RelationshipBuilder types={types} createRelationship={jest.fn()} />)
    expect(screen.getByText('From (source)')).toBeInTheDocument()
    expect(screen.getByText('To (target)')).toBeInTheDocument()
    expect(screen.getByRole('button', { name: /define relationship/i })).toBeInTheDocument()
  })

  it('warns and does not persist when no endpoints are chosen', async () => {
    const createRelationship = jest.fn().mockResolvedValue({})
    const notify = { success: jest.fn(), error: jest.fn() }
    render(
      <RelationshipBuilder types={types} createRelationship={createRelationship} notify={notify} />,
    )
    await userEvent.setup().click(screen.getByRole('button', { name: /define relationship/i }))
    expect(createRelationship).not.toHaveBeenCalled()
    expect(notify.error).toHaveBeenCalled()
  })

  it('prompts to create types first when none exist (submit disabled)', () => {
    render(<RelationshipBuilder types={[]} createRelationship={jest.fn()} />)
    expect(screen.getByText(/create at least one entity type first/i)).toBeInTheDocument()
    expect(screen.getByRole('button', { name: /define relationship/i })).toBeDisabled()
  })
})
