import { Controller, Request, Post, Body, Get, Res, UseGuards, Param, Put, Query, Delete, Patch, Req, HttpException } from '@nestjs/common';
import { ApiParam, ApiQuery, ApiTags, ApiBody, ApiConsumes, ApiBearerAuth } from '@nestjs/swagger';
import { SettingsService } from './settings.services';
import { toJsonResponse } from '../../common/response';

import { AdminGuard } from 'src/modules/auth/guards/admin.guard';

// DTO Files
import { SettingsListQueryDto } from "./dtos/settings-list-query.dto";
import { SettingsUpdateDto } from './dtos/settings-update.dto';
import { CommonGuard } from '../auth/guards/common.guard';

@ApiTags('settings')
@Controller('settings')
export class SettingsController {
    constructor(private settingsService: SettingsService) { }


    @ApiBearerAuth('access-token')
    @UseGuards(CommonGuard)
    @Get(':type')
    /**
     * Retrieves a list of platform settings based on query parameters.
     *
     * @param queryParam - Query parameters for listing settings, 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 settings if successful.
     * Logs the error and responds with the error status and message if an exception occurs.
     */
    async list(@Param() param: any, @Query() queryParam: SettingsListQueryDto, @Request() req, @Res() response: any) {
        try {
            const data = await this.settingsService.list(param, queryParam, req.user);
            response.status(200).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    // @ApiBearerAuth('access-token')
    // @UseGuards(CommonGuard)
    // @Get('general')
    // /**
    // * Retrieves a list of general settings based on query parameters.
    // *
    // * @param queryParam - Query parameters for listing settings, 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 settings if successful.
    // * Logs the error and responds with the error status and message if an exception occurs.
    // */
    // async general(@Query() queryParam: SettingsListQueryDto, @Request() req, @Res() response: any) {
    //     try {

    //         const data = await this.settingsService.general(queryParam, req.user);
    //         response.status(200).json(data);
    //     } catch (error) {
    //         console.log(error);
    //         response.status(error.status).json(toJsonResponse(error.status, error.message));
    //     }
    // }

    @ApiBearerAuth('access-token')
    @ApiBody({ type: SettingsUpdateDto })
    @UseGuards(AdminGuard)
    @Put()
    /**
     * Updates the settings based on the provided request data.
     *
     * @param request - An instance of SettingsUpdateDto containing the settings information to be updated.
     * @param response - The response object for sending the HTTP response.
     *
     * Responds with a 201 status if the update is successful.
     * In case of an error, logs the error and responds with the respective error status and message.
     */
    async update(@Body() request: SettingsUpdateDto, @Res() response: any) {
        try {
            await this.settingsService.update(request);
            response.status(201).json();
        } catch (error) {
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }
}
