import { Product } from '@/entities/product.entity';
import { SystemLang } from '@/system/lang/system.lang';
import { IsExistInDatabase } from '@/system/validators/decorators/is-exist-in-database';
import { Type } from 'class-transformer';
import { IsArray, IsNumber, IsOptional, IsString, Min } from 'class-validator';

export class UpdateProductDto {
  @IsNumber()
  id: number;

  @IsString()
  @IsOptional()
  title: string;

  @IsArray()
  @IsString({ each: true })
  @IsOptional()
  images: string[];

  @Type(() => Number) // đảm bảo chuyển đổi từ string -> number khi gửi form-data
  @IsNumber({ maxDecimalPlaces: 2 })
  @Min(0)
  @IsOptional()
  price: number;

  @IsString()
  @IsOptional()
  category: string;

  @IsString()
  @IsOptional()
  condition: string;

  @IsString()
  @IsOptional()
  description: string;

  @IsArray()
  @IsString({ each: true })
  @IsOptional()
  tags: string[];

  @IsString()
  @IsOptional()
  @IsExistInDatabase(
    Product,
    'sku',
    {},
    {
      message: SystemLang.getCustomText({
        en: 'SKU must be unique',
        vi: 'SKU phải là duy nhất',
      }),
    },
  )
  sku: string;

  @IsOptional()
  @IsString()
  location?: string;
}
