/**
 * @deprecated This file is kept for reference. Tests have moved to CopyToAllProductTypesDialog.test.tsx
 * The CopyToAllMediaDialog component has been renamed to CopyToAllProductTypesDialog.
 * This file re-exports the new test suite so the old path doesn't break CI.
 */
// Re-run the same suite but importing the new component
import { describe, it, expect, vi, afterEach, beforeAll, afterAll } from 'vitest'
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { CopyToAllProductTypesDialog } from '../components/CopyToAllProductTypesDialog'
import type { ProductTypeListItem } from '../../product-types/types'

vi.mock('sonner', () => ({ toast: { success: vi.fn(), error: vi.fn() } }))

const API_URL = 'http://localhost:5000'

function makeProductType(id: number, nombre: string): ProductTypeListItem {
  return {
    id,
    nombre,
    hasDuration: false,
    requiresText: false,
    requiresCategory: false,
    isBundle: false,
    allowImages: false,
    isActive: true,
  }
}

const productTypes = [
  makeProductType(1, 'La Nación'),
  makeProductType(2, 'Clarín'),
  makeProductType(3, 'Infobae'),
]

const server = setupServer()
beforeAll(() => server.listen({ onUnhandledRequest: 'warn' }))
afterEach(() => { server.resetHandlers(); vi.clearAllMocks() })
afterAll(() => server.close())

function renderDialog(onOpenChange = vi.fn()) {
  const qc = new QueryClient({ defaultOptions: { queries: { retry: false }, mutations: { retry: false } } })

  server.use(
    http.get(`${API_URL}/api/v1/product-types`, () =>
      HttpResponse.json({ items: productTypes, page: 1, pageSize: 200, total: 3 }),
    ),
  )

  return render(
    <QueryClientProvider client={qc}>
      <CopyToAllProductTypesDialog
        open={true}
        onOpenChange={onOpenChange}
        symbol="$"
        pricePerUnit={1.5}
        validFrom="2026-04-25"
        category="Currency"
      />
    </QueryClientProvider>,
  )
}

describe('CopyToAllMediaDialog (renamed → CopyToAllProductTypesDialog)', () => {
  it('shows preview of symbol, price, and validFrom', async () => {
    renderDialog()

    await waitFor(() => expect(screen.getByRole('dialog')).toBeInTheDocument())

    expect(screen.getByText('$')).toBeInTheDocument()
    expect(screen.getByText('1.5')).toBeInTheDocument()
    expect(screen.getByText('25/04/2026')).toBeInTheDocument()
  })

  it('confirm with 3 product types selected calls create mutation 3 times', async () => {
    const createCalls: unknown[] = []
    server.use(
      http.post(`${API_URL}/api/v1/admin/chargeable-chars`, async ({ request }) => {
        const body = await request.json()
        createCalls.push(body)
        return HttpResponse.json(
          { id: createCalls.length, productTypeId: (body as Record<string, unknown>)['productTypeId'], symbol: '$', category: 'Currency', pricePerUnit: 1.5, validFrom: '2026-04-25', validTo: null, isActive: true },
          { status: 201 },
        )
      }),
    )

    renderDialog()

    await waitFor(() => expect(screen.getByText('La Nación')).toBeInTheDocument())

    const confirmBtn = screen.getByRole('button', { name: /confirmar|copiar/i })
    await userEvent.click(confirmBtn)

    await waitFor(() => expect(createCalls.length).toBe(3), { timeout: 5000 })
  })

  it('cancel button closes without making API calls', async () => {
    const createCalls: unknown[] = []
    server.use(
      http.post(`${API_URL}/api/v1/admin/chargeable-chars`, async ({ request }) => {
        createCalls.push(await request.json())
        return HttpResponse.json({}, { status: 201 })
      }),
    )

    const onOpenChange = vi.fn()
    renderDialog(onOpenChange)

    await waitFor(() => expect(screen.getByRole('dialog')).toBeInTheDocument())

    const cancelBtn = screen.getByRole('button', { name: /cancelar/i })
    await userEvent.click(cancelBtn)

    await waitFor(() => expect(onOpenChange).toHaveBeenCalledWith(false))
    expect(createCalls.length).toBe(0)
  })
})
