import { Type } from 'class-transformer';
import {
  IsArray,
  IsIn,
  IsInt,
  IsNumber,
  IsOptional,
  IsString,
  MaxLength,
  Min,
  ValidateNested,
} from 'class-validator';

export class ReceiptMatchItemDto {
  @IsString()
  @MaxLength(191)
  rawName!: string;

  @IsOptional()
  @IsString()
  @MaxLength(191)
  normalizedName?: string;

  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  productId?: number;

  @IsOptional()
  @Type(() => Number)
  @IsNumber({ maxDecimalPlaces: 2 })
  @Min(0)
  quantity?: number;

  @IsOptional()
  @IsString()
  @MaxLength(24)
  unit?: string;
}

export class ReceiptMatchOverrideDto {
  @Type(() => Number)
  @IsInt()
  @Min(0)
  rowIndex!: number;

  @Type(() => Number)
  @IsInt()
  @Min(1)
  selectionId!: number;
}

export class ReceiptMatchDto {
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ReceiptMatchItemDto)
  items!: ReceiptMatchItemDto[];

  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  sessionId?: number;

  @IsOptional()
  @IsString()
  @MaxLength(16)
  weekKey?: string;

  @IsOptional()
  @IsArray()
  @ValidateNested({ each: true })
  @Type(() => ReceiptMatchOverrideDto)
  overrides?: ReceiptMatchOverrideDto[];

  @IsOptional()
  @IsString()
  @MaxLength(80)
  receiptImportBatchId?: string;

  @IsOptional()
  @IsString()
  @IsIn(['receipt_auto', 'receipt_manual'])
  boughtSource?: 'receipt_auto' | 'receipt_manual';
}
