import { IsOptional, IsString, IsNumber, Min, IsIn } from 'class-validator';
import { Type } from 'class-transformer';
import { ApiPropertyOptional } from '@nestjs/swagger';
import { User } from '../entities/user.entity';

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

export class FindUsersDto {
  /**
   * The page number to retrieve.
   * @default 1
   */
  @ApiPropertyOptional({
    description: 'شماره صفحه',
    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: 'تعداد مورد در هر صفحه',
    type: Number,
    default: 10,
    minimum: 1,
  })
  @IsOptional()
  @IsNumber()
  @Min(1)
  @Type(() => Number)
  limit?: number = 10;

  /**
   * A general search term to filter by.
   * Searches firstName, lastName, phone, and nationalCode.
   */
  @ApiPropertyOptional({
    description: 'جستجو بر اساس نام، نام‌خانوادگی یا شماره تماس',
    type: String,
  })
  @IsOptional()
  @IsString()
  search?: string;

  /**
   * The field to sort the results by.
   * @default "createdAt"
   */
  @ApiPropertyOptional({
    description: 'فیلد مرتب‌سازی',
    type: String,
    default: 'createdAt',
  })
  @IsOptional()
  @IsString()
  orderBy?: keyof User = 'createdAt';

  /**
   * The direction to sort the results.
   * @default "desc"
   */
  @ApiPropertyOptional({
    description: 'جهت مرتب‌سازی (asc یا desc)',
    enum: sortOrderOptions,
    default: 'desc',
  })
  @IsOptional()
  @IsIn(sortOrderOptions)
  order?: SortOrder = 'desc';
}
