import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import {
  IsIn,
  IsInt,
  IsNotEmpty,
  IsNumber,
  IsString,
  Length,
  Max,
  Min,
} from 'class-validator';
import { TransactionTypeEnum } from 'src/transactions/transaction-type.enum';
import { RecurringTransactionIntervalEnum } from '../recurring-transaction-interval.enum';

const ALLOWED_TYPES = [TransactionTypeEnum.fee, TransactionTypeEnum.levy];

export class CreateRecurringTransactionDTO {
  @ApiProperty({ example: 2342 })
  @IsInt()
  @Min(1)
  teamId: number;

  @ApiProperty({ example: 'Monatsbeitrag' })
  @Transform(({ value }) =>
    typeof value === 'string' ? value.trim() : value,
  )
  @IsString()
  @Length(1, 120)
  description: string;

  @ApiProperty({ example: 10 })
  @IsNumber({ maxDecimalPlaces: 2 })
  @Min(0.01)
  @Max(10000)
  amount: number;

  @ApiProperty({ enum: ALLOWED_TYPES })
  @IsIn(ALLOWED_TYPES)
  type: TransactionTypeEnum;

  @ApiProperty({ enum: RecurringTransactionIntervalEnum })
  @IsIn(Object.values(RecurringTransactionIntervalEnum))
  interval: RecurringTransactionIntervalEnum;

  @ApiProperty({ example: 'isotimestring' })
  @IsNotEmpty()
  startDate: string;
}
