import { Transform, Type } from 'class-transformer';
import { IsArray, IsDate, IsIn, IsOptional, IsString, IsUUID, MaxLength, MinLength } from 'class-validator';
import { TIPOS_DOCUMENTO, TipoDocumento } from '../tipos-documento';

export class BuscarDocumentoQuery {
  @IsString()
  @MinLength(1)
  @MaxLength(200)
  texto!: string;

  @IsOptional()
  @IsIn(TIPOS_DOCUMENTO)
  tipo?: TipoDocumento;
}

export class DocumentoParams {
  @IsIn(TIPOS_DOCUMENTO)
  tipo!: TipoDocumento;

  @IsUUID()
  id!: string;
}

export class ListarDocumentosQuery {
  @IsOptional()
  @IsIn(TIPOS_DOCUMENTO)
  tipo?: TipoDocumento;

  /** Coma separada: "Rechazado,Error". */
  @IsOptional()
  @Transform(({ value }) =>
    typeof value === 'string'
      ? value
          .split(',')
          .map((v) => v.trim())
          .filter(Boolean)
      : value,
  )
  @IsArray()
  @IsString({ each: true })
  @MaxLength(40, { each: true })
  estado?: string[];

  @IsOptional()
  @Type(() => Date)
  @IsDate()
  desde?: Date;

  @IsOptional()
  @Type(() => Date)
  @IsDate()
  hasta?: Date;
}

export class AlertasQuery {
  @IsOptional()
  @IsString()
  @MaxLength(60)
  modulo?: string;
}

export class GuiasQuery {
  @IsString()
  @MinLength(2)
  @MaxLength(500)
  q!: string;

  @IsOptional()
  @IsString()
  @MaxLength(150)
  screen_key?: string;
}
