import { estadoOferta, whereEstadoOferta } from './estado-oferta';

const ahora = new Date('2026-11-05T12:00:00.000Z');
const ms = (n: number) => new Date(ahora.getTime() + n);
const o = (d: Partial<{ active: boolean | null; fecha_inicio: Date; fecha_fin: Date }> = {}) => ({ active: true, fecha_inicio: ms(-1000), fecha_fin: ms(1000), ...d });

describe('estadoOferta', () => {
  it('inactiva con active false y con null', () => {
    expect(estadoOferta(o({ active: false }), ahora)).toBe('inactiva');
    expect(estadoOferta(o({ active: null }), ahora)).toBe('inactiva');
  });
  it('programada si empieza 1 ms después', () => expect(estadoOferta(o({ fecha_inicio: ms(1) }), ahora)).toBe('programada'));
  it('vencida si terminó 1 ms antes', () => expect(estadoOferta(o({ fecha_fin: ms(-1) }), ahora)).toBe('vencida'));
  it('activa en el borde exacto de inicio y de fin', () => {
    expect(estadoOferta(o({ fecha_inicio: ahora }), ahora)).toBe('activa');
    expect(estadoOferta(o({ fecha_fin: ahora }), ahora)).toBe('activa');
  });
  it('inactiva manda sobre las fechas', () => expect(estadoOferta(o({ active: false, fecha_fin: ms(-1) }), ahora)).toBe('inactiva'));
});

describe('whereEstadoOferta', () => {
  it('activa', () => expect(whereEstadoOferta('activa', ahora)).toEqual({ active: true, fecha_inicio: { lte: ahora }, fecha_fin: { gte: ahora } }));
  it('programada', () => expect(whereEstadoOferta('programada', ahora)).toEqual({ active: true, fecha_inicio: { gt: ahora } }));
  it('vencida', () => expect(whereEstadoOferta('vencida', ahora)).toEqual({ active: true, fecha_fin: { lt: ahora } }));
  it('inactiva', () => expect(whereEstadoOferta('inactiva', ahora)).toEqual({ OR: [{ active: false }, { active: null }] }));
  it('desconocido no filtra', () => {
    expect(whereEstadoOferta('x', ahora)).toEqual({});
    expect(whereEstadoOferta(undefined, ahora)).toEqual({});
  });
  it('cada fila cae en el estado de su filtro', () => {
    const filas = [o(), o({ fecha_inicio: ms(1) }), o({ fecha_fin: ms(-1) }), o({ active: false })];
    expect(filas.map((f) => estadoOferta(f, ahora))).toEqual(['activa', 'programada', 'vencida', 'inactiva']);
  });
});
