import { ApiProperty } from '@nestjs/swagger';
import {
  IsIn,
  IsNotEmpty,
  IsOptional,
  Max,
  Min,
  Validate,
  ValidateIf,
} from 'class-validator';
import { IsNotExist } from '../../../utils/validators/is-not-exists.validator';
import { AvailableSection } from '../entities/discount.entity';

export class CreateDiscountDto {
  @ApiProperty({ example: '' })
  @IsNotEmpty({ message: 'عنوان تخفیف اجباری است' })
  name: string;

  @ApiProperty({ example: '' })
  @IsOptional()
  customerId: number;

  @ApiProperty({ example: '' })
  @IsOptional()
  userId: string;

  @ApiProperty({ example: '' })
  @ValidateIf((p) => !p.discountAmount)
  @Max(100)
  @Min(1)
  @IsNotEmpty({ message: 'درصد تخفیف یا مبلغ ثابت تخفیف اجباری است' })
  discountPercentage: number;

  @ApiProperty({ example: '' })
  @ValidateIf((p) => !p.discountPercentage)
  @IsNotEmpty({ message: 'درصد تخفیف یا مبلغ ثابت تخفیف اجباری است' })
  discountAmount: number;

  @ApiProperty({ example: '' })
  @Validate(IsNotExist, ['DiscountEntity'], {
    message: 'این کوپن تخفیف قبلا رزرو شده، یک کد تخفیف دیگر وارد نمایید',
  })
  couponCode?: string;

  @ApiProperty({ example: 1 })
  @IsNotEmpty()
  @Min(1)
  countCouponCode: number;

  @ApiProperty({ example: '' })
  @IsOptional()
  startDate?: Date;

  @ApiProperty({ example: '' })
  @IsOptional()
  endDate?: Date;

  @ApiProperty({ example: 1, enum: AvailableSection })
  @IsNotEmpty()
  @IsIn(Object.values(AvailableSection))
  section: AvailableSection;

  @ApiProperty()
  @IsOptional()
  promotionPlanId?: number | null;
}
