import { Controller, Get, Param, ParseIntPipe, Query } from "@nestjs/common";
import { Access } from "src/common/decorators/module-access.decorators";
import { Result } from "src/common/errors/result-error.factory";
import { TicketList } from "src/customer-support/tickets/dtos/ticket-list.dto";
import { GetTicketListService } from "src/customer-support/tickets/services/get-ticket-list.service";

@Controller('tickets')
export class TicketController {

    constructor(private readonly getService: GetTicketListService) { }

    @Get(':status')
    async findTicketByFilters(
        @Access('email') email: string,
        @Param('status') status: string,
        @Query('sortOrder') sortOrder: string,
        @Query('sortType') sortType: 'DESC' | 'ASC',
        @Query('email') filterEmail?: string,
        @Query('offset', ParseIntPipe) offset?: number,
        @Query('limit', ParseIntPipe) limit?: number,
    ): Promise<Result<TicketList[], string>> {
        const statusFilters = status.split(',');
        const queryEmail = filterEmail ?? email;

        return await this.getService.getListOfAllTicketByFilters(
            statusFilters,
            offset,
            limit,
            sortOrder,
            sortType,
            queryEmail,
        );
    }

    @Get('list')
    async findTicketList(
        @Access('email') email:string,
        @Query('offset', ParseIntPipe) offset: number,
        @Query('limit', ParseIntPipe) limit: number,
    ) {
        return await this.getService.getListOfTickets(
            email,
            offset,
            limit
        );
    }
}