import { ApiExtraModels, ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
import { IsBoolean, IsDefined, IsNotEmpty, IsNumber, IsObject, IsOptional, IsString } from "class-validator";
import { EsResponseHits } from "../../interfaces/elastic/es-response-hits.interface";

/**
 * List of allowed properties in this DTO
 */
const allowedProperties = ['took', 'timed_out', '_shards', 'hits', 'pit_id'];

/**
 * Elasticsearch response DTO
 */
@ApiExtraModels()
export class EsResponseDto {
    /**
     * Number of milliseconds it 
     * took Elasticsearch to execute the request 
     */
    @IsDefined()
    @IsNotEmpty()
    @IsNumber()
    @ApiProperty({
        description: 'The time that it took Elasticsearch to process the query',
        example: 5
    })
    took: number;
    
    /**
     * Status of the request
     * If 'true' - the request timed out before completion
     */
    @IsDefined()
    @IsNotEmpty()
    @IsBoolean()
    @ApiProperty({
        description: 'Shows if request timed out before completion',
        example: false,
    })
    timed_out: boolean;
    
    /**
     * Contains a number of Elasticsearch shards
     * used for the request
     */
    @IsOptional()
    @IsObject()
    @ApiProperty({
        description: 'Contains a count of Elasticsearch shards used to process the request',
        example: {
            total: 1,
            successful: 1,
            skipped: 0,
            failed: 0,
        }
    })
    _shards: object;

    /**
     * Contains returned documents and metadata
     */
    @IsOptional()
    @IsObject()
    @ApiProperty({
        description: 'Contains returned documents and metadata',
        example: {
            total: {
                value: 3,
                relation: 'eq'
            },
            max_score: 1.2,
            hits: [{
                _index: 'papers',
                _id: '01002',
                _score: 1.2,
                _source: {},
                fields: {}
            }],
        }
    })
    hits: EsResponseHits;

    /**
     * ID of the PIT used in the search
     */
    @IsString()
    @IsOptional()
    @ApiPropertyOptional({
        description: 'Contains PIT ID used to search for results',
        example: '46ToAwMDaWR5BXV1aWQyKwZub2RlXzMAAAAAAAAAACoBYwADaWR4BXV1aWQxAgZub2RlXzEAAAAAAAAAAAEBYQADaWR5BXV1aWQyKgZub2RlXzIAAAAAAAAAAAwBYgACBXV1aWQyAAAFdXVpZDEAAQltYXRjaF9hbGw_gAAAAA=='
    })
    pit_id?: string;
}