import {
  IsArray,
  IsBoolean,
  IsEnum,
  IsNotEmpty,
  IsOptional,
  IsPhoneNumber,
  IsString,
  ValidateIf,
} from 'class-validator';
import {
  OTP_ACTION_TYPE,
  OTP_SOURCE,
  OtpRequestEntity,
  OtpVerifyEntity,
} from '../../domain/entities/otp-verification.entity';
import { ApiProperty } from '@nestjs/swagger';
import { Type } from 'class-transformer';

export class OtpRequestDto implements OtpRequestEntity {
  @ApiProperty({
    type: String,
    required: true,
    example: OTP_ACTION_TYPE.CANCEL_TRANSACTION,
    description: 'CANCEL_TRANSACTION || CREATE_DISCOUNT',
  })
  @IsString()
  @IsNotEmpty()
  action_type: OTP_ACTION_TYPE;

  @ApiProperty({
    type: String,
    required: true,
    example: OTP_SOURCE.POS,
    description: 'POS || WEB',
  })
  @IsString()
  @IsNotEmpty()
  source: OTP_SOURCE;

  @ApiProperty({
    name: 'target_id',
    example: 'bccc0c6a-51a0-437f-abc8-dc18851604ee',
  })
  @IsString()
  @ValidateIf((body) => body.target_id)
  target_id: string;

  @ApiProperty({ name: 'reference', example: '0625N21' })
  @IsString()
  @ValidateIf((body) => body.reference)
  reference: string;
}

export class OtpVerifyDto extends OtpRequestDto implements OtpVerifyEntity {
  @ApiProperty({
    name: 'otp_code',
    type: String,
    required: true,
    example: '2345',
  })
  @IsString()
  @IsNotEmpty()
  otp_code: string;
}

export class OtpVerifierCreateDto {
  @ApiProperty({
    example: 'Item Manager',
    description: 'Nama verifier, opsional.',
  })
  @IsOptional()
  @IsString()
  name?: string;

  @ApiProperty({
    example: '6281234567890',
    description: 'Nomor telepon verifier dalam format internasional (E.164).',
  })
  @IsString()
  @IsPhoneNumber('ID')
  phone_number: string;

  @ApiProperty({
    example: false,
    description:
      'True jika verifier boleh memverifikasi semua aksi tanpa batas.',
  })
  @IsBoolean()
  is_all_action: boolean;

  @ApiProperty({
    isArray: true,
    enum: OTP_ACTION_TYPE,
    example: [
      'CREATE_DISCOUNT',
      'CANCEL_TRANSACTION',
      'REJECT_RECONCILIATION',
      'ACTIVATE_ITEM',
      'ACTIVATE_USER',
      'UPDATE_ITEM_PRICE',
      'UPDATE_ITEM_DETAILS',
      'CONFIRM_TRANSACTION',
    ],
    description: 'Daftar tipe aksi yang boleh diverifikasi, jika bukan semua.',
  })
  @IsOptional()
  @IsArray()
  @IsEnum(OTP_ACTION_TYPE, { each: true })
  @Type(() => String)
  action_types?: OTP_ACTION_TYPE[];
}
