import { describe, it, expect } from 'vitest'
import { render, fireEvent } from '@testing-library/react'
import { Canvas } from '@/components/wysiwyg/Canvas'
import type { Frame } from '@/lib/wysiwyg/schema'

function makeFrame(): Frame {
  return {
    id: 'f1',
    width: 400,
    height: 300,
    background: '#ffffff',
    layers: [
      {
        id: 'l1',
        name: 'L1',
        visible: true,
        locked: false,
        objects: [
          { id: 'o1', type: 'image', x: 101, y: 100, width: 50, height: 40, rotate: 0, zIndex: 0, props: { src: 'x', alt: '' } },
        ],
      },
    ],
  }
}

describe('WYSIWYG Canvas mouse drag with 8px snap', () => {
  it('dragging object snaps to nearest 8px', () => {
    const frame = makeFrame()
    const { getByTestId } = render(<Canvas frame={frame} />)
    const canvas = getByTestId('wysiwyg-canvas')
    const obj = getByTestId('obj-o1')

    // start drag
    fireEvent.mouseDown(obj, { clientX: 200, clientY: 150 })
    // move by +5,+5 (should snap from 101->104, 100->104)
    fireEvent.mouseMove(canvas, { clientX: 205, clientY: 155 })
    fireEvent.mouseUp(canvas)

    expect(obj).toHaveStyle({ left: '104px', top: '104px' })
  })
})
