import { Controller, Get, HttpCode, HttpException, Next, Param, ParseUUIDPipe, Put, Query, Req, Res, UseInterceptors } from "@nestjs/common";
import { SearchService } from "../../core/services/common/search.service";
import { PageInterceptor } from "src/core/interceptors/page.interceptor";
import { SearchResultDto } from "src/core/domain/dtos/search-result.dto";
import { ApiOperation, ApiResponse } from "@nestjs/swagger";
import { RequestDto } from "src/core/domain/dtos/request.dto";

/**
 * /papers/ route controller
 */
@Controller('papers')
export class PapersController {
    constructor(private searchService: SearchService) {}

    /**
     * Request handler for: GET /papers/search
     * @param query 
     * @param response 
     * @returns a response with a set of matching papers
     */
     @ApiOperation({ summary: 'Finds papers by context based on the query.' })
     @ApiResponse({
       status: 200,
       description: 'Returns back acquired papers.',
       type: SearchResultDto,
     })
    @Get('search')
    @UseInterceptors(PageInterceptor)
    @HttpCode(200)
    getByContext(@Req() query: RequestDto): object {
        return this.searchService.findByContext(query.es_query).then(
            (response: SearchResultDto) => {
                return response.data;
            },
            (error: SearchResultDto) => {
                throw new HttpException(error.data, error.statusCode);
            }
        );
    }

    /**
     * Request handler for GET /papers/{uuid}
     * @param uuid 
     * @param response 
     * @returns a response with a requested object
     */
     @ApiOperation({ summary: 'Finds paper by its UUID.' })
     @ApiResponse({
       status: 200,
       description: 'Returns back acquired paper.',
       type: SearchResultDto,
     })
    @Get(':uuid')
    @UseInterceptors(PageInterceptor)
    @HttpCode(200)
    getByID(@Param('uuid', ParseUUIDPipe) uuid: string): object {
        return this.searchService.findByID(uuid).then(
            (response) => {
                return response.data;
            },
            (error) => {
                throw new HttpException(error.data, error.status);
            }
        );
    }
}