import { IsOptional, IsString, IsNumber, Min, IsIn, IsDateString } from 'class-validator';
import { Type } from 'class-transformer';
import { ApiPropertyOptional } from '@nestjs/swagger';
import { WalletTransactionType, WalletTransactionReason } from '../interface/wallet';

// Define the valid sort directions
const sortOrderOptions = ['asc', 'desc'] as const;
type SortOrder = (typeof sortOrderOptions)[number];

export class FindWalletTransactionsDto {
    /**
     * The page number to retrieve.
     * @default 1
     */
    @ApiPropertyOptional({
        description: 'Page number',
        type: Number,
        default: 1,
        minimum: 1,
    })
    @IsOptional()
    @IsNumber()
    @Min(1)
    @Type(() => Number)
    page?: number = 1;

    /**
     * The number of items per page.
     * @default 10
     */
    @ApiPropertyOptional({
        description: 'Items per page',
        type: Number,
        default: 10,
        minimum: 1,
    })
    @IsOptional()
    @IsNumber()
    @Min(1)
    @Type(() => Number)
    limit?: number = 10;

    /**
     * Filter by transaction type
     */
    @ApiPropertyOptional({
        description: 'Transaction type filter',
        enum: WalletTransactionType,
    })
    @IsOptional()
    @IsIn(Object.values(WalletTransactionType))
    type?: WalletTransactionType;

    /**
     * Filter by transaction reason
     */
    @ApiPropertyOptional({
        description: 'Transaction reason filter',
        enum: WalletTransactionReason,
    })
    @IsOptional()
    @IsIn(Object.values(WalletTransactionReason))
    reason?: WalletTransactionReason;

    /**
     * Filter transactions from this date onwards
     */
    @ApiPropertyOptional({
        description: 'Start date filter (ISO 8601 format)',
        type: String,
    })
    @IsOptional()
    @IsDateString()
    startDate?: string;

    /**
     * Filter transactions up to this date
     */
    @ApiPropertyOptional({
        description: 'End date filter (ISO 8601 format)',
        type: String,
    })
    @IsOptional()
    @IsDateString()
    endDate?: string;

    /**
     * Minimum amount filter
     */
    @ApiPropertyOptional({
        description: 'Minimum amount filter',
        type: Number,
    })
    @IsOptional()
    @IsNumber()
    @Type(() => Number)
    minAmount?: number;

    /**
     * Maximum amount filter
     */
    @ApiPropertyOptional({
        description: 'Maximum amount filter',
        type: Number,
    })
    @IsOptional()
    @IsNumber()
    @Type(() => Number)
    maxAmount?: number;

    /**
     * The field to sort the results by.
     * @default "createdAt"
     */
    @ApiPropertyOptional({
        description: 'Sort field',
        type: String,
        default: 'createdAt',
    })
    @IsOptional()
    @IsString()
    orderBy?: string = 'createdAt';

    /**
     * The direction to sort the results.
     * @default "desc"
     */
    @ApiPropertyOptional({
        description: 'Sort direction',
        enum: sortOrderOptions,
        default: 'desc',
    })
    @IsOptional()
    @IsIn(sortOrderOptions)
    order?: SortOrder = 'desc';
}
