import { describe, it, expect, beforeAll, afterAll, afterEach } from 'vitest'
import { http, HttpResponse } from 'msw'
import { setupServer } from 'msw/node'
import { getUser } from '../../../features/users/api/getUser'

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

const mockDetail = {
  id: 5,
  username: 'cajero1',
  nombre: 'Juan',
  apellido: 'Pérez',
  email: 'j@x.com',
  rol: 'cajero',
  activo: true,
  mustChangePassword: false,
  ultimoLogin: '2026-04-10T10:00:00Z',
  fechaModificacion: null,
}

const server = setupServer()

beforeAll(() => server.listen({ onUnhandledRequest: 'error' }))
afterEach(() => server.resetHandlers())
afterAll(() => server.close())

describe('getUser api client', () => {
  it('calls GET /api/v1/users/:id and returns UserDetail', async () => {
    server.use(http.get(`${API_URL}/api/v1/users/5`, () => HttpResponse.json(mockDetail)))
    const result = await getUser(5)
    expect(result.id).toBe(5)
    expect(result.username).toBe('cajero1')
    expect(result.mustChangePassword).toBe(false)
  })
})
