import { TurnosResolverService } from './turnos-resolver.service';

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

  const turnoBase = {
    id: 'turno-1',
    nombre: 'Turno Base',
    tipo: 'SIMPLE',
    activo: true,
    lunes: true,
    martes: true,
    miercoles: true,
    jueves: true,
    viernes: true,
    sabado: false,
    domingo: false,
    bloques: [
      {
        id: 'bloq-1',
        orden: 1,
        hora_entrada: new Date('1970-01-01T08:00:00Z'),
        hora_salida: new Date('1970-01-01T17:00:00Z'),
      },
    ],
  };

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

  it('prioriza asignación OCASIONAL sobre PERPETUA', async () => {
    prisma.rrhh_empleados_turnos.findMany.mockResolvedValue([
      {
        id: 'asig-perpetua',
        turno_id: 'turno-perpetua',
        tipo_asignacion: 'PERPETUA',
        prioridad: 100,
        created_at: new Date('2026-05-01T00:00:00Z'),
        turno: { ...turnoBase, id: 'turno-perpetua', nombre: 'Perpetua' },
      },
      {
        id: 'asig-ocasional',
        turno_id: 'turno-ocasional',
        tipo_asignacion: 'OCASIONAL',
        prioridad: 100,
        created_at: new Date('2026-05-10T00:00:00Z'),
        turno: { ...turnoBase, id: 'turno-ocasional', nombre: 'Ocasional' },
      },
    ]);

    const out = await service.resolverTurnoVigente('empresa-1', 'emp-1', '2026-05-18');

    expect(out?.turno_id).toBe('turno-ocasional');
    expect(out?.tipo_asignacion).toBe('OCASIONAL');
  });

  it('retorna null cuando no hay turnos aplicables para el día', async () => {
    prisma.rrhh_empleados_turnos.findMany.mockResolvedValue([
      {
        id: 'asig-1',
        turno_id: 'turno-domingo',
        tipo_asignacion: 'PERPETUA',
        prioridad: 100,
        created_at: new Date('2026-05-01T00:00:00Z'),
        turno: {
          ...turnoBase,
          lunes: false,
          martes: false,
          miercoles: false,
          jueves: false,
          viernes: false,
          sabado: false,
          domingo: true,
        },
      },
    ]);

    const out = await service.resolverTurnoVigente('empresa-1', 'emp-1', '2026-05-18');

    expect(out).toBeNull();
  });
});
