import { ApiExtraModels, ApiPropertyOptional } from "@nestjs/swagger";
import { Type } from "class-transformer";
import { IsDefined, IsInt, IsNotEmpty, IsOptional, IsString, Min } from "class-validator";

/**
 * List of allowed properties in this DTO
 */
const allowedProperties = ['query', 'limit', 'offset', 'order'];

/**
 * Elasticsearch response DTO
 */
@ApiExtraModels()
export class SearchQueryDto {
    /**
     * Given query string to perform the search on.
     */
    @IsDefined()
    @IsNotEmpty()
    @IsString()
    @ApiPropertyOptional({
        description: 'Given query string to perform the search on',
        example: 'Particle Accelerator',
    })
    query: string;

    /**
     * Limits the number of displayed elements.
     */
    @IsOptional()
    @IsInt()
    @Type(() => Number)
    @Min(1)
    @ApiPropertyOptional({
        description: 'Limits the number of displayed elements',
        example: 10,
        required: false
    })
    limit?: number;

    /**
     * Offset from the start of the list of hits.
     */
    @IsOptional()
    @IsInt()
    @Type(() => Number)
    @Min(0)
    @ApiPropertyOptional({
        description: 'Offset from the start of the list of hits',
        example: 0,
        required: false,
    })
    offset?: number;

    /**
     * Indicates in which order elements need to be displayed.
     */
    @IsOptional()
    @IsString()
    @ApiPropertyOptional({
        description: 'Indicates in which order elements need to be displayed',
        example: 'asc',
        required: false,
    })
    order?: string;

    /**
     * Constructs an object with provided parameters
     * @param query 
     * @param page 
     * @param limit 
     * @param order 
     */
    constructor(query: string = undefined, limit: number = 10, offset: number = 0, order: string = undefined) {
        this.query = query;
        this.limit = limit;
        this.offset = offset;
        this.order = order;
    }
}