import { LegajosConsultaService } from './legajos-consulta.service';

describe('LegajosConsultaService', () => {
  let service: LegajosConsultaService;
  let prisma: any;

  beforeEach(() => {
    prisma = {
      rrhh_legajo_documentos: {
        findMany: jest.fn(),
      },
    };
    service = new LegajosConsultaService(prisma);
  });

  it('aplica límite máximo de 100 y entrega cursor_next cuando hay más filas', async () => {
    prisma.rrhh_legajo_documentos.findMany.mockResolvedValue(
      Array.from({ length: 101 }).map((_, i) => ({
        id: `doc-${String(i).padStart(3, '0')}`,
        empresa_id: 'empresa-1',
        empleado_id: 'emp-1',
        tipo_documento_id: 'tipo-1',
        nombre_original: `file-${i}.pdf`,
        nombre_storage: `legajos/file-${i}.pdf`,
        mime_type: 'application/pdf',
        extension: 'pdf',
        tamanio_bytes: BigInt(10),
        hash_sha256: 'a'.repeat(64),
        fecha_documento: null,
        fecha_vencimiento: null,
        estado_vencimiento: 'SIN_VENCIMIENTO',
        descripcion: null,
        version: 1,
        documento_previo_id: null,
        estado: 'ACTIVO',
        subido_por: 'user-1',
        subido_en: new Date(`2026-05-${String((i % 28) + 1).padStart(2, '0')}T10:00:00.000Z`),
        anulado_por: null,
        anulado_en: null,
        motivo_anulacion: null,
        tipo_documento: { id: 'tipo-1', codigo: 'CI', nombre: 'Cédula' },
        empleado: {
          id: 'emp-1',
          numero_empleado: 'EMP-1',
          nombres: 'Juan',
          apellidos: 'Pérez',
          sucursal_id: null,
          departamento_id: null,
          sucursal: null,
          departamento: null,
        },
      })),
    );

    const out = await service.consultar('empresa-1', { limit: 500 } as any);

    expect(out.limit).toBe(100);
    expect(out.data).toHaveLength(100);
    expect(out.has_more).toBe(true);
    expect(out.cursor_next).toBeTruthy();
  });

  it('sin página extra retorna has_more=false y cursor_next=null', async () => {
    prisma.rrhh_legajo_documentos.findMany.mockResolvedValue([
      {
        id: 'doc-1',
        empresa_id: 'empresa-1',
        empleado_id: 'emp-1',
        tipo_documento_id: 'tipo-1',
        nombre_original: 'ci.pdf',
        nombre_storage: 'legajos/ci.pdf',
        mime_type: 'application/pdf',
        extension: 'pdf',
        tamanio_bytes: BigInt(10),
        hash_sha256: 'a'.repeat(64),
        fecha_documento: null,
        fecha_vencimiento: null,
        estado_vencimiento: 'SIN_VENCIMIENTO',
        descripcion: null,
        version: 1,
        documento_previo_id: null,
        estado: 'ACTIVO',
        subido_por: 'user-1',
        subido_en: new Date('2026-05-19T10:00:00.000Z'),
        anulado_por: null,
        anulado_en: null,
        motivo_anulacion: null,
        tipo_documento: { id: 'tipo-1', codigo: 'CI', nombre: 'Cédula' },
        empleado: {
          id: 'emp-1',
          numero_empleado: 'EMP-1',
          nombres: 'Juan',
          apellidos: 'Pérez',
          sucursal_id: null,
          departamento_id: null,
          sucursal: null,
          departamento: null,
        },
      },
    ]);

    const out = await service.consultar('empresa-1', { limit: 10 } as any);

    expect(out.data).toHaveLength(1);
    expect(out.has_more).toBe(false);
    expect(out.cursor_next).toBeNull();
  });
});
