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

const messages: Record<string, string> = enMessages as Record<string, string>

describe('BuilderClientPage - SectionCanvas integration', () => {
  it('섹션을 추가하고 이동/삭제가 동작한다', () => {
    render(
      <NextIntlClientProvider locale="en" messages={messages}>
        <BuilderClientPage />
      </NextIntlClientProvider>
    )

    // 섹션 3개 추가: Hero, FAQ, CTA (data-testid 사용)
    fireEvent.click(screen.getByTestId('btn-add-hero'))
    fireEvent.click(screen.getByTestId('btn-add-faq'))
    fireEvent.click(screen.getByTestId('btn-add-cta'))

    const canvas = screen.getByTestId('section-canvas')
    expect(canvas).toBeInTheDocument()

    // 초기 순서 확인: hero, faq, cta
    let items = canvas.querySelectorAll('li')
    expect(items.length).toBe(3)
    expect(items[0].textContent).toMatch(/hero/)
    expect(items[1].textContent).toMatch(/faq/)
    expect(items[2].textContent).toMatch(/cta/)

    // 두 번째 항목(faq)을 위로 이동 (Up 1)
    fireEvent.click(screen.getByLabelText('Up 1'))
    items = canvas.querySelectorAll('li')
    expect(items[0].textContent).toMatch(/faq/)
    expect(items[1].textContent).toMatch(/hero/)

    // 마지막 항목(cta)을 위로 이동 (Up 2)
    fireEvent.click(screen.getByLabelText('Up 2'))
    items = canvas.querySelectorAll('li')
    expect(items[0].textContent).toMatch(/faq/)
    expect(items[1].textContent).toMatch(/cta/)
    expect(items[2].textContent).toMatch(/hero/)

    // 가운데 항목(cta) 삭제 (Remove 1)
    fireEvent.click(screen.getByLabelText('Remove 1'))
    items = canvas.querySelectorAll('li')
    expect(items.length).toBe(2)
    expect(items[0].textContent).toMatch(/faq/)
    expect(items[1].textContent).toMatch(/hero/)
  })
})
