import { render, screen, fireEvent } from '@testing-library/react'
import { NextIntlClientProvider } from 'next-intl'
import { describe, expect, test } from 'vitest'
import BuilderClientPage from './BuilderClientPage'
import messages from '../messages/en.json'

function Wrapper({ children }: { children: React.ReactNode }) {
  return (
    <NextIntlClientProvider messages={messages} locale="en">
      {children}
    </NextIntlClientProvider>
  )
}

describe('Inspector Add buttons title consistency', () => {
  test('Features/Testimonials/FAQ Add buttons have title matching visible label', async () => {
    render(
      <Wrapper>
        <BuilderClientPage />
      </Wrapper>
    )

    // Features
    fireEvent.click(screen.getByTestId('card-features'))
    fireEvent.click(await screen.findByLabelText('Select 0'))
    const addFeature = await screen.findByRole('button', { name: /Add Feature Item/i })
    expect(addFeature).toBeInTheDocument()
    expect(addFeature.getAttribute('title')).toBe('Add Feature Item')

    // Testimonials
    fireEvent.click(screen.getByTestId('card-testimonials'))
    fireEvent.click(await screen.findByLabelText('Select 1'))
    const addTestimonial = await screen.findByRole('button', { name: /Add Testimonial/i })
    expect(addTestimonial).toBeInTheDocument()
    expect(addTestimonial.getAttribute('title')).toBe('Add Testimonial')

    // FAQ
    fireEvent.click(screen.getByTestId('card-faq'))
    fireEvent.click(await screen.findByLabelText('Select 2'))
    const addFaq = await screen.findByRole('button', { name: /Add FAQ Item/i })
    expect(addFaq).toBeInTheDocument()
    expect(addFaq.getAttribute('title')).toBe('Add FAQ Item')
  })
})
