import {
    Body,
    Controller,
    Delete,
    Get,
    Param,
    Post,
    Put,
    Query,
    UseGuards,
    Request,
    Patch,
} from '@nestjs/common';
// import { AuthGuard } from 'src/auth/auth.guard';

import { ApiBearerAuth, ApiTags, ApiOperation } from '@nestjs/swagger';
import * as dto from './dto/index';
import { AgentService } from './agent.service';
import { AuthGuard } from '../auth/auth.guard';
// import { RolesGuard } from 'src/auth/roles.guard';
import { CallService } from '../call/call.service';

@ApiTags('Agent')
@Controller('agent')
export class AgentController {
    constructor(private readonly AgentService: AgentService,
        private readonly CallService: CallService
    ) { }


    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Post()
    async create_agent(@Body() dto: dto.Agent.agentCreate, @Request() req: any) {
        try {
            let data = await this.AgentService.create_agent(dto, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }


    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get()
    async get_agent(@Query() dto: dto.Agent.get_agent, @Request() req: any) {
        try {
            let data = await this.AgentService.get_agent(dto, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    // @UseGuards(AuthGuard)
    // @ApiBearerAuth('authorization')
    @Get('/:_id')
    async get_agent_detail(@Param('_id') agent_id: string, @Request() req: any) {
        try {
            let data = await this.AgentService.get_agent_detail(agent_id, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Put('/:_id')
    async edit_agent(@Param('_id') agent_id: string, @Body() dto: dto.Agent.agentEdit) {
        try {
            let data = await this.AgentService.edit_agent(agent_id, dto);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }


    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Post('default-agent')
    async default_agent(@Body() dto: dto.Agent.defaultAgentCreate, @Request() req: any) {
        try {
            let data = await this.AgentService.default_agent(dto, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('default/agent')
    async default_agent_get(@Query() dto: dto.Agent.get_agent, @Request() req: any) {
        try {
            let data = await this.AgentService.default_agent_get(dto, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('default/agent/:_id')
    async default_agent_detail(@Param('_id') default_agent_id: string, @Request() req: any) {
        try {

            let data = await this.AgentService.default_agent_detail(default_agent_id, req);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('agent/:agent_id/chat')
    async agent_chat(@Param('agent_id') agent_id: string, @Query() body: dto.Agent.get_agent_chat, @Request() req: any) {
        try {
            let data = await this.AgentService.chat_list(agent_id, body);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('/:_id/chart')
    async view_agent_details_chart(@Param('_id') agent_id: string, @Request() req: any) {
        try {
            let data = await this.AgentService.get_emotion_percentages_by_agent(agent_id);
            let response = {
                success: true,
                message: "Emotion data processed successfully",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Put('/default/web')
    async defaultAgentWebUpdate(@Body() dto: dto.Agent.websiteDefaultAgent) {
        try {
            let data = await this.AgentService.defaultAgentWebUpdate(dto);
            let response = {
                success: true,
                message: "Success! The agent has been updated",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }


    @Get('/default/web')
    async defaultAgentWebGet() {
        try {
            let data = await this.AgentService.getWebDefaultAgent();
            let response = {
                success: true,
                message: "Success! The agent has been found",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Patch('/:agent_id/status/:status')
    async agent_status_update(@Param() dto: dto.Agent.agentStatusEdit) {
        try {
            let data = await this.AgentService.agent_status_update(dto);
            let response = {
                success: true,
                message: `Agent is successfully updated.`,
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Delete('/:_id')
    async delete_agent(@Param('_id') agent_id: string) {
        try {
            let data = await this.AgentService.delete_agent(agent_id);
            let response = {
                success: true,
                message: `The agent has been successfully deleted`,
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

}
