import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import {
  IsNotEmpty,
  IsNumber,
  IsOptional,
  IsString,
  Max,
  Min,
} from 'class-validator';

export class CreateOrderDto {
  @ApiProperty({ description: 'Seller user ID' })
  @IsString()
  @IsNotEmpty()
  sellerId!: string;

  @ApiProperty({ description: 'Listing ID for the order' })
  @IsString()
  @IsNotEmpty()
  listingId!: string;

  @ApiProperty({
    type: Number,
    description: 'Amount in VND (1 – 100,000,000,000)',
    example: 5_000_000_000,
  })
  @IsNotEmpty()
  @IsNumber()
  @Min(1, { message: 'Số tiền phải lớn hơn 0' })
  @Max(100_000_000_000, { message: 'Số tiền vượt quá giới hạn (100 tỷ VND)' })
  @Transform(({ value }) => {
    const num = Number(value);
    if (!Number.isFinite(num) || !Number.isInteger(num)) return value;
    return num;
  }, { toClassOnly: true })
  amountVND!: number;

  @ApiPropertyOptional({ description: 'Idempotency key to prevent duplicate orders' })
  @IsOptional()
  @IsString()
  idempotencyKey?: string;
}
