import { Controller, Request, Post, Body, Get, Res, UseGuards, Param, Put, Query, Delete, Patch, Req, HttpException, UseInterceptors, UploadedFile } from "@nestjs/common"
import { ApiParam, ApiQuery, ApiTags, ApiBody, ApiConsumes, ApiBearerAuth } from "@nestjs/swagger"
import { CommonService } from "./common.services"
import { toJsonResponse } from "../../common/response"
import { FileInterceptor } from "@nestjs/platform-express"

import { BrandGuard } from "../auth/guards/brand.guard"
import { AdminBrandGuard } from "../auth/guards/admin-brand.guard"
import { CommonGuard } from "../auth/guards/common.guard"
import { AdminGuard } from "../auth/guards/admin.guard"

// DTO Files
import { CommonUploadFileDto } from "./dtos/common-upload-file.dto"
import { NotificationListQueryDto } from "./dtos/notification-list-query.dto"
import { WhatsAppMessageListQueryDto } from "./dtos/whatsapp-message-list-query.dto"
import { CreateErrorReportDto } from "./dtos/create-error-report.dto"
import { query } from "express"

@ApiTags("common")
@Controller("common")
export class CommonController {
    constructor(private commonService: CommonService) { }

    // JP-
    @UseGuards(CommonGuard)
    @UseInterceptors(FileInterceptor("file"))
    @ApiConsumes("multipart/form-data")
    @ApiBody({
        description: "Common Upload file",
        schema: {
            type: "object",
            properties: {
                file: {
                    type: "string",
                    format: "binary"
                },
                type: {
                    enum: ["profile", "user-excel", "campaign", "draft", "dispute", "advertisement", "bank", "errorlog", "category"]
                },
                root: {
                    enum: ["files", "images"]
                }
            }
        },
        required: true
    })
    @Post("upload-file")
    /**
     * Upload file in AWS Bucket.
     *
     * @param request - An instance of CommonUploadFileDto containing the AWS Bucket directory details.
     * root : Main folder directory name in AWS Bucket.
     * type : Sub folder under to root directory in AWS Bucket.
     * @param response - The response object for sending the HTTP response.
     * @param req - The request object, containing the authenticated user information.
     * 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 commonUploadFile(@UploadedFile() file: Express.Multer.File, @Body() request: CommonUploadFileDto, @Res() response: any, @Req() req: any) {

        const data = await this.commonService.commonUploadFile(request, file, req.user)
        response.status(200).json(data)

    }

    @Get("countries")
    async countries(@Query() query: any, @Res() response: any, @Req() req: any) {

        const data = await this.commonService.countries(query)
        response.status(200).json(data)

    }

    @UseGuards(CommonGuard)
    @Get('notifications')
    async notification(@Query() query: NotificationListQueryDto, @Res() response: any, @Req() req: any) {
        const data = await this.commonService.notification(query, req.user)
        response.status(200).json(data)
    }

    @ApiBearerAuth('access-token')
    @ApiQuery({ name: 'page', required: true, example: 1 })
    @ApiQuery({ name: 'per_page', required: false, example: 10 })
    @ApiQuery({ name: 'sort_by', required: false, type: 'string' })
    @ApiQuery({ name: 'sort', required: false, type: 'string' })
    @ApiQuery({ name: 'search', required: false, type: 'string' })
    @ApiQuery({ name: 'direction', required: false, enum: ['inbound', 'outbound'] })
    @ApiQuery({ name: 'message_type', required: false, type: 'string' })
    @ApiQuery({ name: 'registration_status', required: false, enum: ['registered', 'non_registered'] })
    @ApiQuery({ name: 'user_id', required: false, type: 'string' })
    @ApiQuery({ name: 'phone_number', required: false, type: 'string' })
    @ApiQuery({ name: 'is_auto_reply', required: false, type: 'string' })
    @UseGuards(AdminGuard)
    @Get('whatsapp-messages')
    async whatsappMessageList(@Query() query: WhatsAppMessageListQueryDto, @Res() response: any) {

        const data = await this.commonService.whatsappMessageList(query)
        response.status(200).json(data)
    }

    @UseGuards(CommonGuard)
    @Patch('notifications')
    async readNotification(@Query() query: any, @Res() response: any, @Req() req: any) {
        await this.commonService.readNotification(req.user, query)
        response.status(201).json()
    }

    @Get('script/assign-permissions')
    async assignPermissions(@Res() response: any, @Req() req: any) {
        const data = await this.commonService.assignPermissions()
        response.status(200).json(data)
    }

    @UseGuards(CommonGuard)
    @UseInterceptors(FileInterceptor('file'))
    @ApiConsumes('multipart/form-data')
    @ApiBody({
        description: 'Excel file with english_categories and chinese_categories columns',
        schema: {
            type: 'object',
            properties: {
                file: { type: 'string', format: 'binary' }
            }
        }
    })
    @Post('script/import-category-chinese')
    async importCategoryChinese(@UploadedFile() file: Express.Multer.File, @Res() response: any, @Req() req: any) {
        const result = await this.commonService.importCategoryChineseTranslations(file)
        response.status(200).json(result)
    }

    @UseGuards(CommonGuard)
    @UseInterceptors(FileInterceptor('file'))
    @ApiConsumes('multipart/form-data')
    @ApiBody({
        description: 'Excel file with english_keywords and chinese_keywords columns',
        schema: {
            type: 'object',
            properties: {
                file: { type: 'string', format: 'binary' }
            }
        }
    })
    @Post('script/import-keyword-chinese')
    async importKeywordChinese(@UploadedFile() file: Express.Multer.File, @Res() response: any, @Req() req: any) {
        const result = await this.commonService.importKeywordChineseTranslations(file)
        response.status(200).json(result)
    }

    @UseGuards(CommonGuard)
    @Post('error-report')
    async ErrorReport(@Body() body: CreateErrorReportDto, @Res() response: any, @Req() req: any) {
        await this.commonService.errorReport(body, req.user)
        response.status(201).json()
    }

    @Get('sitemap')
    async sitemap(@Res() response: any, @Req() req: any) {
        const data = await this.commonService.sitemap()
        response.status(200).json(data)
    }

    @Get('sitemap/categories')
    async sitemapCategories(@Res() response: any, @Req() req: any) {
        const data = await this.commonService.sitemapCategories()
        response.status(200).json(data);
    }

    @Get('sitemap/:page')
    async sitemapPage(@Param('page') page: number, @Res() response: any, @Req() req: any) {

        const data = await this.commonService.sitemapPage(page)
        response.status(200).json(data)

    }

    @Get('script/comet-chat-email-notification')
    async cometChatEmailNotification(@Res() response: any, @Req() req: any) {
        const data = await this.commonService.cometChatEmailNotification()
        response.status(200).json(data)
    }

    @Get('s3/presigned-url')
    async generatePresignedUrl(@Query() query: CommonUploadFileDto, @Res() response: any, @Req() req: any) {
        const data = await this.commonService.generatePresignedUrl(query)
        response.status(200).json(data)
    }

}
