import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Transform, Type } from 'class-transformer';
import { IsIn, IsInt, IsOptional, IsString, IsUUID, Matches, Min } from 'class-validator';

/** Ids separados por coma, como los manda el front. */
const ListaDeIds = () => Transform(({ value }) => (typeof value === 'string' ? value.split(',').filter(Boolean) : value));

const MSG_DEPOSITO = 'Elegí un depósito de la lista.';
const MSG_CATEGORIAS = 'Elegí las categorías de la lista.';
const MSG_MARCAS = 'Elegí las marcas de la lista.';
const UUID = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';

class Paginado {
  @ApiPropertyOptional()
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(0)
  skip?: number;

  @ApiPropertyOptional()
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  take?: number;
}

export class ResumenZonaQueryDto {
  @ApiProperty()
  @IsUUID(undefined, { message: MSG_DEPOSITO })
  depositoId: string;

  @ApiPropertyOptional({ description: 'IDs separados por coma' })
  @IsOptional()
  @ListaDeIds()
  @IsUUID(undefined, { each: true, message: MSG_CATEGORIAS })
  categoriaIds?: string[];

  @ApiPropertyOptional({ description: 'IDs separados por coma' })
  @IsOptional()
  @ListaDeIds()
  @IsUUID(undefined, { each: true, message: MSG_MARCAS })
  marcaIds?: string[];
}

export class ProductosZonaQueryDto extends Paginado {
  @ApiProperty()
  @IsUUID(undefined, { message: 'Elegí una sesión de la lista.' })
  sesionId: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsUUID(undefined, { message: MSG_DEPOSITO })
  depositoId?: string;

  @ApiPropertyOptional({ description: 'IDs separados por coma' })
  @IsOptional()
  @ListaDeIds()
  @IsUUID(undefined, { each: true, message: MSG_CATEGORIAS })
  categoriaId?: string[];

  @ApiPropertyOptional({ description: 'IDs separados por coma' })
  @IsOptional()
  @ListaDeIds()
  @IsUUID(undefined, { each: true, message: MSG_MARCAS })
  marcaId?: string[];

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  busqueda?: string;

  @ApiPropertyOptional({ enum: ['contados', 'pendientes'] })
  @IsOptional()
  @IsIn(['contados', 'pendientes'])
  filtro?: 'contados' | 'pendientes';
}

export class NoContadosQueryDto extends Paginado {
  @ApiPropertyOptional()
  @IsOptional()
  @IsUUID(undefined, { message: MSG_DEPOSITO })
  depositoId?: string;

  @ApiPropertyOptional({ description: 'IDs separados por coma' })
  @IsOptional()
  @ListaDeIds()
  @IsUUID(undefined, { each: true, message: MSG_CATEGORIAS })
  categoriaId?: string[];

  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  busqueda?: string;

  @ApiPropertyOptional({ description: 'Solo lo que tiene stock: es lo que el cierre pondría en cero', enum: ['1'] })
  @IsOptional()
  @IsIn(['1'])
  con_stock?: '1';
}

export class DeshacerConteoQueryDto {
  @ApiProperty()
  @IsUUID(undefined, { message: 'Elegí un producto de la lista.' })
  productoId: string;

  @ApiProperty()
  @IsUUID(undefined, { message: MSG_DEPOSITO })
  depositoId: string;

  @ApiPropertyOptional({ description: 'Solo ese lote; «sin-lote» = la parte sin lote. Sin loteId, todo el producto.' })
  @IsOptional()
  @Matches(new RegExp(`^(sin-lote|${UUID})$`, 'i'), { message: 'Elegí un lote de la lista.' })
  loteId?: string;
}
