import { describe, it, expect } from 'vitest'
import { render, screen, fireEvent } from '@testing-library/react'
import React from 'react'
import { Provider } from 'react-redux'
import { configureStore } from '@reduxjs/toolkit'
import ExtractionView from '../src/views/ExtractionView'
import { MemoryRouter } from 'react-router-dom'
import { documentReducer } from '../src/store/documentSlice'
import { appReducer } from '../src/store/appSlice'

type UnknownRecord = Record<string, unknown>

function makeStore(initialState: UnknownRecord) {
  return configureStore({
    reducer: { app: appReducer, document: documentReducer },
    preloadedState: initialState as any,
  })
}

describe('ExtractionView - Onglets entités', () => {
  it('affiche les 4 onglets attendus et permet de changer d’onglet', () => {
    const initialState: UnknownRecord = {
      app: {},
      document: {
        folderResults: [
          {
            fileHash: 'fh',
            document: { fileName: 'a.pdf', mimeType: 'application/pdf', fileSize: 1, uploadTimestamp: new Date().toISOString() },
            classification: { documentType: 'Document', confidence: 1, subType: '', language: 'fr', pageCount: 1 },
            extraction: {
              text: { raw: 'abc', processed: 'abc', wordCount: 1, characterCount: 3, confidence: 1 },
              entities: {
                persons: [{ id: 'p1', firstName: 'A', lastName: 'B', description: '' }],
                addresses: [{ id: 'a1', street: 'r', postalCode: 'p', city: 'c', country: 'fr', description: '' }],
                companies: [{ id: 'c1', name: 'X', description: '' }],
                contractual: { clauses: [{ title: 'T', text: 'txt' }], signatures: [{ signer: 'S', status: 'ok' }] },
              },
            },
            metadata: { processing: {}, quality: { globalConfidence: 0.9, textExtractionConfidence: 0.9, entityExtractionConfidence: 0.9, classificationConfidence: 0.9 } },
            status: { success: true, errors: [], warnings: [], timestamp: new Date().toISOString() },
          },
        ],
        currentResultIndex: 0,
        loading: false,
        currentFolderHash: 'fh1',
      },
    }

    const store = makeStore(initialState)
    render(
      <MemoryRouter>
        <Provider store={store}>
          <ExtractionView />
        </Provider>
      </MemoryRouter>
    )

    expect(screen.getByText(/Personnes \(/)).toBeTruthy()
    expect(screen.getByText(/Adresses \(/)).toBeTruthy()
    expect(screen.getByText(/Entreprises \(/)).toBeTruthy()
    expect(screen.getByText(/Contractuel \(/)).toBeTruthy()

    fireEvent.click(screen.getByText(/Adresses \(/))
    expect(screen.getByText('Adresses')).toBeTruthy()
  })
})
