import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { PaymentProvider, PaymentType } from '@prisma/client';
import { Transform } from 'class-transformer';
import {
  IsEnum,
  IsNotEmpty,
  IsNumber,
  IsOptional,
  IsString,
  IsUrl,
  Max,
  Min,
  MinLength,
} from 'class-validator';

export class CreatePaymentDto {
  @ApiProperty({ enum: PaymentProvider, description: 'Payment provider' })
  @IsEnum(PaymentProvider)
  provider!: PaymentProvider;

  @ApiProperty({ enum: PaymentType, description: 'Payment type' })
  @IsEnum(PaymentType)
  type!: PaymentType;

  @ApiProperty({ type: Number, description: 'Amount in VND (1 – 100,000,000,000)', example: 500000 })
  @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 cho phép (100 tỷ VND)' })
  @Transform(({ value }) => {
    const num = Number(value);
    if (!Number.isFinite(num) || !Number.isInteger(num)) return value;
    return num;
  }, { toClassOnly: true })
  amountVND!: number;

  @ApiProperty({ description: 'Payment description', example: 'Listing fee' })
  @IsString()
  @MinLength(1)
  description!: string;

  @ApiProperty({ description: 'URL to redirect after payment', example: 'https://example.com/return' })
  @IsUrl()
  returnUrl!: string;

  @ApiPropertyOptional({ description: 'External transaction ID' })
  @IsOptional()
  @IsString()
  transactionId?: string;

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