import { crearIntegracionMock } from './__helpers__/integracion-mock';
import { CONT_ORIGEN_REGISTRY_SEED } from 'src/contabilidad-conciliacion/registry.seed';
import { FaltaMapeoError } from './falta-mapeo.error';

const embarque = {
  id: 'e1',
  empresa_id: 'emp',
  numero: 'IMP-1',
  estado: 'CERRADO',
  fecha_cierre: new Date('2026-10-22T00:00:00.000Z'),
  costo_total_gs: 1000,
  usuario_id: 'u1',
  despacho: { id: 'd1', fecha_despacho: new Date() },
};

const crear = (saldo: { debe: number; haber: number }, emb: any = embarque) =>
  crearIntegracionMock({
    imp_embarques: { findUnique: jest.fn().mockResolvedValue(emb) },
    imp_despachos: { findMany: jest.fn().mockResolvedValue([{ id: 'd1' }]) },
    imp_componentes_costo: { findMany: jest.fn().mockResolvedValue([{ id: 'k1', compra_id: 'c1', gasto_id: null }, { id: 'k2', compra_id: null, gasto_id: 'g1' }, { id: 'k3', compra_id: null, gasto_id: null }]), findFirst: jest.fn() },
    tes_movimientos: { findMany: jest.fn().mockResolvedValue([{ id: 't1' }]) },
    nota_credito_compra_cab: { findMany: jest.fn().mockResolvedValue([{ id: 'nc1' }]) },
    cont_documentos: {
      findFirst: jest.fn().mockResolvedValue(null),
      findMany: jest.fn().mockResolvedValue([{ id: 'doc-d1' }, { id: 'doc-c1' }]),
      create: jest.fn().mockResolvedValue({ id: 'doc-cierre' }),
    },
    cont_asientos: { findMany: jest.fn().mockResolvedValue([{ id: 'a1' }, { id: 'a2' }]) },
    cont_asientos_det: { aggregate: jest.fn().mockResolvedValue({ _sum: { debe_pyg: saldo.debe, haber_pyg: saldo.haber } }) },
  });

describe('cierre de importación', () => {
  it('traslada solo lo que entró a la cuenta puente', async () => {
    const s = crear({ debe: 800, haber: 0 });
    await s.integrarCierreImportacion('e1');
    const l = s.asientos.crearConfirmado.mock.calls[0][3].lineas;
    expect(l).toEqual([
      expect.objectContaining({ cuenta_id: 'cta-INVENTARIO', debe_moneda: 800 }),
      expect.objectContaining({ cuenta_id: 'cta-IMPORTACIONES_EN_CURSO', haber_moneda: 800 }),
    ]);
  });

  it('[RF-4] con saldo 0 (despacho revertido) no crea asiento: NO_APLICA', async () => {
    const s = crear({ debe: 500, haber: 500 });
    await expect(s.integrarCierreImportacion('e1')).resolves.toEqual({ estado: 'NO_APLICA' });
    expect(s.asientos.crearConfirmado).not.toHaveBeenCalled();
  });

  it('devuelve GENERADO con el documento y va con la fecha de cierre, no con hoy (D5)', async () => {
    const s = crear({ debe: 800, haber: 0 });
    s.asientos.crearConfirmado.mockResolvedValue({ id: 'a', numero: 9 });
    await expect(s.integrarCierreImportacion('e1')).resolves.toEqual({ estado: 'GENERADO', documento_id: 'doc-cierre', numero: '9' });
    expect(s.asientos.crearConfirmado.mock.calls[0][3].fecha).toBe('2026-10-22');
    expect(s.periodos.resolverPeriodoPorFecha).toHaveBeenCalledWith('emp', embarque.fecha_cierre);
  });

  it('sin fecha de cierre (SELLADO legado) lanza FECHA_CIERRE_REQUERIDA: nunca usa hoy', async () => {
    const s = crear({ debe: 800, haber: 0 }, { ...embarque, estado: 'SELLADO', fecha_cierre: null });
    await expect(s.integrarCierreImportacion('e1')).rejects.toMatchObject({ response: expect.objectContaining({ codigo: 'FECHA_CIERRE_REQUERIDA' }) });
    expect(s.asientos.crearConfirmado).not.toHaveBeenCalled();
  });

  it('sin período abierto en la fecha de cierre → PENDIENTE PERIODO_CERRADO (ya no logger.error + return)', async () => {
    const s = crear({ debe: 800, haber: 0 });
    const { BadRequestException } = await import('@nestjs/common');
    s.periodos.resolverPeriodoPorFecha.mockRejectedValue(new BadRequestException('No existe un período contable abierto para la fecha 2026-10-22'));
    await expect(s.integrarCierreImportacion('e1')).resolves.toEqual({ estado: 'PENDIENTE', codigo: 'PERIODO_CERRADO', mensaje: expect.stringContaining('2026-10-22') });
  });

  it('con un documento CONFIRMADO es idempotente: GENERADO con su número, sin otro asiento', async () => {
    const s = crear({ debe: 800, haber: 0 });
    s.prisma.cont_documentos.findFirst.mockResolvedValue({ id: 'doc-viejo', estado: 'CONFIRMADO', asiento: { numero: 4 } });
    await expect(s.integrarCierreImportacion('e1')).resolves.toEqual({ estado: 'GENERADO', documento_id: 'doc-viejo', numero: '4' });
    expect(s.asientos.crearConfirmado).not.toHaveBeenCalled();
  });

  it('un documento REVERTIDO (reabrir) se desvincula en la misma tx y se genera otro (B18)', async () => {
    const s = crear({ debe: 800, haber: 0 });
    s.prisma.cont_documentos.findFirst.mockResolvedValue({ id: 'doc-rev', estado: 'REVERTIDO', asiento: { numero: 4 } });
    s.prisma.cont_documentos.update = jest.fn().mockResolvedValue({});
    s.prisma.cont_asientos.findMany.mockImplementation(({ where }: any) => Promise.resolve(where.documento_id === null ? [] : [{ id: 'a1' }, { id: 'a2' }]));
    await s.integrarCierreImportacion('e1');
    expect(s.prisma.cont_documentos.update).toHaveBeenCalledWith(expect.objectContaining({ where: { id: 'doc-rev' }, data: expect.objectContaining({ origen_tipo: null, origen_id: null }) }));
    expect(s.asientos.crearConfirmado).toHaveBeenCalledTimes(1);
  });

  it('busca los documentos del despacho, compras, sus NC, gastos y pagos de la importación', async () => {
    const s = crear({ debe: 800, haber: 0 });
    await s.integrarCierreImportacion('e1');
    const where = s.prisma.cont_documentos.findMany.mock.calls[0][0].where;
    expect(where.OR).toEqual(expect.arrayContaining([
      { origen_tipo: 'imp_despacho', origen_id: { in: ['d1'] } },
      { origen_tipo: 'compra_cab', origen_id: { in: ['c1'] } },
      { origen_tipo: 'nota_credito_compra_cab', origen_id: { in: ['nc1'] } },
      { origen_tipo: 'gasto_cab', origen_id: { in: ['g1'] } },
      { origen_tipo: 'tes_movimientos', origen_id: { in: ['t1'] } },
    ]));
    // F4: los complementarios de los costos tardíos también entran a la puente.
    expect(where.OR).toContainEqual({ origen_tipo: 'imp_componente', origen_id: { in: ['k1', 'k2', 'k3'] } });
    expect(s.prisma.nota_credito_compra_cab.findMany.mock.calls[0][0].where).toEqual({ empresa_id: 'emp', compra_id: { in: ['c1'] } });
    expect(s.prisma.cont_asientos_det.aggregate.mock.calls[0][0].where).toEqual({ asiento_id: { in: ['a1', 'a2'] }, cuenta_id: 'cta-IMPORTACIONES_EN_CURSO' });
  });

  it('solo cuenta asientos vigentes (los revertidos no suman)', async () => {
    const s = crear({ debe: 800, haber: 0 });
    await s.integrarCierreImportacion('e1');
    expect(s.prisma.cont_asientos.findMany.mock.calls[0][0].where).toEqual(
      expect.objectContaining({ estado: 'CONFIRMADO' }),
    );
    expect(s.prisma.tes_movimientos.findMany.mock.calls[0][0].where).toEqual(
      expect.objectContaining({ embarque_id: 'e1', estado: { not: 'ANULADO' } }),
    );
  });

  it('el asiento lleva el usuario del embarque, nunca el id de la empresa', async () => {
    const s = crear({ debe: 800, haber: 0 });
    await s.integrarCierreImportacion('e1');
    expect(s.asientos.crearConfirmado.mock.calls[0][1]).toBe('u1');
  });

  it('sin usuario en el embarque, el asiento va sin usuario (null)', async () => {
    const s = crear({ debe: 800, haber: 0 }, { ...embarque, usuario_id: null });
    await s.integrarCierreImportacion('e1');
    expect(s.asientos.crearConfirmado.mock.calls[0][1]).toBeNull();
  });
  it('sin fila de mapeo de la puente: la empresa no usa el modelo de puente (NO_APLICA, sin asiento ni aviso)', async () => {
    const s = crearIntegracionMock({ imp_embarques: { findUnique: jest.fn().mockResolvedValue(embarque) } }, ['IMPORTACIONES_EN_CURSO']);
    await expect(s.integrarCierreImportacion('e1')).resolves.toEqual({ estado: 'NO_APLICA' });
    expect(s.asientos.crearConfirmado).not.toHaveBeenCalled();
    expect(s.avisosMapeo.registrarFaltaMapeo).not.toHaveBeenCalled();
  });

  it('puente mapeada a una cuenta inutilizable: queda pendiente con aviso (no se descarta en silencio)', async () => {
    const s = crear({ debe: 800, haber: 0 });
    const original = s.mapeo.getCuentaPorConcepto.getMockImplementation();
    s.mapeo.getCuentaPorConcepto.mockImplementation((e: string, c: string) =>
      c === 'IMPORTACIONES_EN_CURSO'
        ? Promise.reject(new FaltaMapeoError(e, [c], 'mapeado a 1.1.4.04, que está desactivada', 'CUENTA_INUTILIZABLE', '1.1.4.04'))
        : original(e, c),
    );
    await expect(s.integrarCierreImportacion('e1')).resolves.toEqual({ estado: 'PENDIENTE', codigo: 'MAPEO_FALTANTE', mensaje: expect.any(String) });
    expect(s.asientos.crearConfirmado).not.toHaveBeenCalled();
    expect(s.avisosMapeo.registrarFaltaMapeo).toHaveBeenCalledWith({
      empresaId: 'emp', origenTipo: 'imp_cierre', origenId: 'e1', conceptos: ['IMPORTACIONES_EN_CURSO'], motivo: 'CUENTA_INUTILIZABLE',
    });
  });

  it('un error que no es de mapeo al leer la puente se relanza', async () => {
    const s = crear({ debe: 800, haber: 0 });
    s.mapeo.getCuentaPorConcepto.mockRejectedValue(new Error('conexión caída'));
    await expect(s.integrarCierreImportacion('e1')).rejects.toThrow('conexión caída');
  });
});

describe('registro de Conciliación de Importaciones (F4)', () => {
  const entrada = (origen: string) => CONT_ORIGEN_REGISTRY_SEED.find((e) => e.origen_tipo === origen);

  it('el cierre usa la fecha de cierre', () => {
    expect(entrada('imp_cierre')).toMatchObject({ campo_fecha: 'fecha_cierre', service_method: 'integrarCierreImportacion' });
  });

  it('el complementario de un costo tardío es un origen propio, solo de los costos que lo llevan', () => {
    expect(entrada('imp_componente')).toMatchObject({
      modulo: 'IMPORTACIONES',
      tabla_origen: 'imp_componentes_costo',
      campo_fecha: 'fecha_devengo',
      campo_monto: 'importe_gs',
      service_name: 'ContabilidadIntegracionService',
      service_method: 'integrarComponenteImportacion',
      filtro_extra: { activo: true, asiento_estado: { not: null } },
    });
  });
});
