import { IsEnum, IsNumberString, IsString, IsOptional, IsArray, IsUUID, IsNumber, IsDateString, IsPositive, IsDate } from 'class-validator';
import { Type, Transform } from 'class-transformer';


function parseIndonesianDate(value: string): Date {
  if (!value) return new Date()

  const months: Record<string, number> = {
    Januari: 0,
    Februari: 1,
    Maret: 2,
    April: 3,
    Mei: 4,
    Juni: 5,
    Juli: 6,
    Agustus: 7,
    September: 8,
    Oktober: 9,
    November: 10,
    Desember: 11,
  }

  const [datePart, timePart] = value.split(', ')
  const [day, monthName, year] = datePart.split(' ')
  const [hour, minute] = timePart.split(':')

  return new Date(
    Number(year),
    months[monthName],
    Number(day),
    Number(hour),
    Number(minute)
  )
}

export class CreateTransactionItemDto {
  @IsOptional()
  @IsString()
  itemCode?: string

  @IsString()
  itemTypeId: string

  @Type(() => Number)
  @IsNumber()
  @IsPositive()
  grossWeight: number

  @Type(() => Number)
  @IsNumber()
  @IsPositive()
  netWeight: number

  @IsString()
  caratId: string

  @Type(() => Number)
  @IsNumber()
  @IsPositive()
  pricePerGram: number

  @Transform(({ value }) => parseIndonesianDate(value))
  @IsDate()
  pricePerGramAt: Date

  @Type(() => Number)
  @IsNumber()
  @IsPositive()
  purityPercent: number

  @IsOptional()
  @IsArray()
  @IsUUID('4', { each: true })  // validates each entry is a valid UUID
  tempPhotoIds?: string[];
}