import { ApiProperty } from '@nestjs/swagger';
import { LoyaltyEarnType } from '@prisma/client';
import { IsEnum, IsInt, IsNumber, Max, Min, ValidateIf } from 'class-validator';

export class UpsertLoyaltySettingsDto {
  @ApiProperty({ example: 200 })
  @IsInt()
  @Min(0)
  minOrderFirstPoint!: number;

  @ApiProperty({ example: 200 })
  @IsInt()
  @Min(0)
  minOrderApplyPoint!: number;

  @ApiProperty({ example: 6 })
  @IsInt()
  @Min(0)
  validityMonths!: number;

  @ApiProperty({ example: 100 })
  @IsInt()
  @Min(0)
  maxEarnablePoints!: number;

  @ApiProperty({ example: 15 })
  @IsInt()
  @Min(0)
  @Max(100)
  maxRedeemPercent!: number;

  @ApiProperty({ enum: LoyaltyEarnType, example: LoyaltyEarnType.INCREMENTAL })
  @IsEnum(LoyaltyEarnType)
  earnType!: LoyaltyEarnType;

  // PERCENTAGE
  @ApiProperty({
    required: false,
    example: 5,
    description: 'Required when earnType=PERCENTAGE (0-100).',
  })
  @ValidateIf(
    (o: UpsertLoyaltySettingsDto) => o.earnType === LoyaltyEarnType.PERCENTAGE,
  )
  @IsNumber()
  @Min(0)
  @Max(100)
  percentageRate?: number;

  // INCREMENTAL
  @ApiProperty({
    required: false,
    example: 100,
    description: 'Required when earnType=INCREMENTAL (₹).',
  })
  @ValidateIf(
    (o: UpsertLoyaltySettingsDto) => o.earnType === LoyaltyEarnType.INCREMENTAL,
  )
  @IsInt()
  @Min(1)
  stepAmount?: number;

  @ApiProperty({
    required: false,
    example: 10,
    description: 'Required when earnType=INCREMENTAL.',
  })
  @ValidateIf(
    (o: UpsertLoyaltySettingsDto) => o.earnType === LoyaltyEarnType.INCREMENTAL,
  )
  @IsInt()
  @Min(1)
  pointsPerStep?: number;

  @ApiProperty({ example: 10 })
  @IsInt()
  @Min(0)
  welcomeBonus!: number;

  @ApiProperty({ example: 10 })
  @IsInt()
  @Min(0)
  referralBonus!: number;
}
