import { Controller, Request, Post, Body, Get, Res, UseGuards, UseInterceptors, UploadedFile, Patch, Query, Put } from '@nestjs/common';
import { ApiOperation, ApiQuery, ApiResponse, ApiTags, ApiBody, ApiConsumes, ApiBearerAuth } from '@nestjs/swagger';
import { FileManagerService } from './file-manager.services';
import { toJsonResponse } from '../../common/response';
import { AdminGuard } from '../auth/guards/admin.guard';

// DTO Files
import { FileManagerListQueryDto } from './dtos/file-manager-list-query.dto';
import { ErrorLogListQueryDto } from './dtos/error-log-list-query.dto';
import { ImportUsersDto } from './dtos/import-users.dto';
import { ExportUserDto } from './dtos/export-users.dto';
import { QueryCommonGuard } from '../auth/guards/common-query.guard';

@ApiTags('file-manager')
@Controller('file-manager')
export class FileManagerController {
    constructor(
        private fileManagerService: FileManagerService,
    ) { }

    // JP-
    @ApiBearerAuth('access-token')
    @ApiQuery({ name: 'page', required: true, example: 1 })
    @ApiQuery({ name: 'per_page', required: true, example: 50 })
    @ApiQuery({ name: 'sort_by', required: false, type: 'string' })
    @ApiQuery({ name: 'sort', required: false, type: 'string' })
    @ApiQuery({ name: 'search', required: false, type: 'string' })
    @ApiQuery({ name: 'role', required: true, type: 'string' })
    @UseGuards(AdminGuard)
    @Get()
    /**
    * Retrieves a list of submitted import excel files on query parameters.
    *
    * @param queryParam - Query parameters for listing file manager list, including pagination and sorting options.
    * @param req - The request object, containing the authenticated user information.
    * @param response - The response object for sending the JSON response.
    *
    * Responds with a 200 status and the list of file manager if successful.
    * Logs the error and responds with the error status and message if an exception occurs.
    */
    async fileMangerList(@Query() queryParam: FileManagerListQueryDto, @Request() req, @Res() response: any) {
        try {
            const data = await this.fileManagerService.fileMangerList(queryParam, req.user);
            response.status(200).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    // JP-
    @ApiBearerAuth('access-token')
    @ApiQuery({ name: 'file_manager_id', required: true, type: 'string' })
    @ApiQuery({ name: 'page', required: true, example: 1 })
    @ApiQuery({ name: 'per_page', required: true, example: 50 })
    @ApiQuery({ name: 'sort_by', required: false, type: 'string' })
    @ApiQuery({ name: 'sort', required: false, type: 'string' })
    @ApiQuery({ name: 'search', required: false, type: 'string' })
    @ApiQuery({ name: 'role', required: true, type: 'string' })
    @UseGuards(AdminGuard)
    @Get('error-logs')
    /**
    * Retrieves a list of error logs from file manager id on query parameters.
    *
    * @param queryParam - Query parameters for listing error log list, including pagination and sorting options.
    * @param req - The request object, containing the authenticated user information.
    * @param response - The response object for sending the JSON response.
    *
    * Responds with a 200 status and the list of error logs if successful.
    * Logs the error and responds with the error status and message if an exception occurs.
    */
    async errorLogList(@Query() queryParam: ErrorLogListQueryDto, @Request() req, @Res() response: any) {
        try {
            const data = await this.fileManagerService.errorLogList(queryParam, req.user);
            response.status(200).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    /**
    * Create or Update a user details based on parameters.
    *
    * @Body request - An instance of ImportUsersDto containing the file name to process of storing data.
    * @param response - The response object for sending the HTTP response.
    *
    * Responds with a 201 status and the user activation status update if successful.
    * Logs the error and responds with the error status and message if an exception occurs.
    */
    @ApiBearerAuth('access-token')
    @UseGuards(AdminGuard)
    @Post('import/users-data')
    async importUsers(@Body() request: ImportUsersDto, @Request() req, @Res() response: any) {
        try {
            const data = await this.fileManagerService.importUsers(request, req.user);
            response.status(200).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }
    @UseGuards(QueryCommonGuard)
    @ApiBearerAuth('access-token')
    @Get('export/influencer-data')
    async exportUsers(@Query() request: ExportUserDto, @Request() req, @Res() response: any) {
        try {
            const data = await this.fileManagerService.exportUsers(request, req.user, response);
            response.status(200).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    @UseGuards(QueryCommonGuard)
    @ApiBearerAuth('access-token')
    @Get('export/brand-data')
    async exportBrand(@Query() request: ExportUserDto, @Request() req, @Res() response: any) {
        try {
            const data = await this.fileManagerService.exportBrand(request, req.user, response);
            response.status(200).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }
}
