import { describe, it, expect } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'
import { WysiwygBuilderPage } from '@/components/wysiwyg/WysiwygBuilderPage'

describe('WYSIWYG Builder - Form Textarea', () => {
  it('adds a Textarea, edits props, and reflects on Canvas', async () => {
    render(<WysiwygBuilderPage />)

    // Add Textarea
    fireEvent.click(screen.getByRole('button', { name: /Add Textarea/i }))

    const obj = await screen.findByTestId(/obj-textarea-/)
    fireEvent.mouseDown(obj, { clientX: 80, clientY: 80 })
    fireEvent.mouseUp(screen.getByTestId('wysiwyg-canvas'))

    const panel = await screen.findByLabelText('Properties Panel')
    const nameInput = panel.querySelector('input[aria-label="Name"]') as HTMLInputElement
    const labelInput = panel.querySelector('input[aria-label="Label"]') as HTMLInputElement
    const placeholderInput = panel.querySelector('input[aria-label="Placeholder"]') as HTMLInputElement
    const rowsInput = panel.querySelector('input[aria-label="Rows"]') as HTMLInputElement
    const requiredCheckbox = panel.querySelector('input[aria-label="Required"]') as HTMLInputElement

    fireEvent.change(nameInput, { target: { value: 'message' } })
    fireEvent.change(labelInput, { target: { value: 'Message' } })
    fireEvent.change(placeholderInput, { target: { value: 'Type here...' } })
    fireEvent.change(rowsInput, { target: { value: '5' } })
    fireEvent.click(requiredCheckbox)

    const rendered = await screen.findByTestId(/obj-textarea-/)
    expect(rendered).toHaveTextContent('Message')
    const ta = rendered.querySelector('textarea') as HTMLTextAreaElement
    expect(ta).toBeTruthy()
    expect(ta.placeholder).toBe('Type here...')
    expect(ta.required).toBe(true)
    expect(ta.rows).toBe(5)
  })
})
