import { ArgumentMetadata, ValidationPipe } from '@nestjs/common';
import { CreateOfertaDto } from './dto/create-oferta.dto';
import { EvaluarOfertasDto } from './dto/evaluar-ofertas.dto';
import { UpdateOfertaDto } from './dto/update-oferta.dto';

const pipe = new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true });
const validar = (metatype: any, value: unknown) => pipe.transform(value, { type: 'body', metatype } as ArgumentMetadata);
const BASE = { nombre: 'Promo', tipo: 'descuento_porcentaje', valor: 10, fecha_inicio: '2026-11-05T11:00:00.000Z', fecha_fin: '2026-11-30T11:00:00.000Z' };
const ITEM = { producto_id: '66e305bb-27ba-4d72-80ae-6714c25cd185', cantidad: 1, precio_unitario: 100 };

describe('DTOs de ofertas', () => {
  it('alta válida pasa', async () => expect(await validar(CreateOfertaDto, BASE)).toBeInstanceOf(CreateOfertaDto));
  it('nombre de 201 → 400', async () => expect(validar(CreateOfertaDto, { ...BASE, nombre: 'x'.repeat(201) })).rejects.toMatchObject({ status: 400 }));
  it('valor 10.123 → 400', async () => expect(validar(CreateOfertaDto, { ...BASE, valor: 10.123 })).rejects.toMatchObject({ status: 400 }));
  it('valor de más de 13 cifras → 400', async () => expect(validar(CreateOfertaDto, { ...BASE, valor: 99999999999999 })).rejects.toMatchObject({ status: 400 }));
  it('prioridad 1.5 → 400', async () => expect(validar(CreateOfertaDto, { ...BASE, prioridad: 1.5 })).rejects.toMatchObject({ status: 400 }));
  it('moneda "usd1" → 400', async () => expect(validar(CreateOfertaDto, { ...BASE, moneda: 'usd1' })).rejects.toMatchObject({ status: 400 }));
  it('moneda "USD" pasa', async () => expect((await validar(CreateOfertaDto, { ...BASE, moneda: 'USD' })).moneda).toBe('USD'));
  it('descripcion null pasa', async () => expect((await validar(CreateOfertaDto, { ...BASE, descripcion: null })).descripcion).toBeNull());
  it('descripcion número → 400', async () => expect(validar(CreateOfertaDto, { ...BASE, descripcion: 5 })).rejects.toMatchObject({ status: 400 }));
  it('EvaluarOfertasDto con moneda "USD" pasa; "" → 400; sin moneda pasa', async () => {
    expect((await validar(EvaluarOfertasDto, { items: [ITEM], moneda: 'USD' })).moneda).toBe('USD');
    await expect(validar(EvaluarOfertasDto, { items: [ITEM], moneda: '' })).rejects.toMatchObject({ status: 400 });
    expect(await validar(EvaluarOfertasDto, { items: [ITEM] })).toBeInstanceOf(EvaluarOfertasDto);
  });
  it('UpdateOfertaDto parcial pasa', async () => expect(await validar(UpdateOfertaDto, { nombre: 'Otro' })).toBeInstanceOf(UpdateOfertaDto));
});
