import { Body, Controller, Delete, Get, Param, Patch, Post, Put, Query, Request, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { AdminAiAgentService } from './admin_ai_agent.service';
import * as dto from './dto/index';
import { AuthGuard } from '../auth/auth.guard';

/**
 * Controller for managing Admin AI Agents.
 * Handles CRUD operations and data retrieval.
 */
@ApiTags('Admin AI Agent')
@Controller('admin/ai-agent')
export class AdminAiAgentController {
    constructor(private readonly AdminAiAgentService: AdminAiAgentService) { }

    @Get('test')
    async getTestAgent() {
        try {
            let data = await this.AdminAiAgentService.getTestAgent()
            let response = {
                message: "Test agent found",
                success: true,
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    /**
     * Creates a new AI agent.
     * 
     * @param {dto.AIAgent.agentCreate} dto - The DTO containing agent details.
     * @param {Request} req - The request object containing user data.
     * @returns {Promise<any>} - The response object.
     */
    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Post()
    async createAgent(@Body() dto: dto.AIAgent.agentCreate, @Request() req: any): Promise<any> {
        try {
            const data = await this.AdminAiAgentService.createAgent(dto, req);
            return { success: true, message: "Agent created successfully", data };
        } catch (err) {
            console.error("Error creating agent:", err);
            throw err;
        }
    }

    /**
     * Retrieves a paginated list of AI agents.
     * 
     * @param {dto.AIAgent.get_agent} dto - The DTO containing pagination details.
     * @param {Request} req - The request object.
     * @returns {Promise<any>} - The response object with agents list.
     */
    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get()
    async getAgents(@Query() dto: dto.AIAgent.get_agent, @Request() req: any): Promise<any> {
        try {
            const data = await this.AdminAiAgentService.getAdminAIAgents(req, dto);
            return { success: true, message: "Agents retrieved successfully", data };
        } catch (err) {
            console.error("Error retrieving agents:", err);
            throw err;
        }
    }

    /**
     * Edits an existing AI agent.
     * 
     * @param {string} agent_id - The ID of the agent to update.
     * @param {dto.AIAgent.agentEdit} dto - The DTO containing updated agent details.
     * @returns {Promise<any>} - The response object with updated agent data.
     */
    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Put('/:_id')
    async editAgent(@Param('_id') agent_id: string, @Body() dto: dto.AIAgent.agentEdit): Promise<any> {
        try {
            const data = await this.AdminAiAgentService.updateAgent(agent_id, dto);
            return { success: true, message: "Agent updated successfully", data };
        } catch (err) {
            console.error("Error updating agent:", err);
            throw err;
        }
    }

    /**
     * Soft deletes an AI agent by setting `is_deleted` to `true`.
     * 
     * @param {string} agent_id - The ID of the agent to delete.
     * @returns {Promise<any>} - The response object with updated agent data.
     */
    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Delete('/:_id')
    async deleteAgent(@Param('_id') agent_id: string): Promise<any> {
        try {
            const data = await this.AdminAiAgentService.deleteAgent(agent_id);
            return { success: true, message: "Agent deleted successfully", data };
        } catch (err) {
            console.error("Error deleting agent:", err);
            throw err;
        }
    }

    /**
     * Retrieves the details of a specific AI agent.
     * 
     * @param {string} agent_id - The ID of the agent to retrieve.
     * @param {Request} req - The request object.
     * @returns {Promise<any>} - The response object containing agent details.
     */
    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('/:_id')
    async getAgentDetails(@Param('_id') agent_id: string, @Request() req: any): Promise<any> {
        try {
            const data = await this.AdminAiAgentService.getAgentDetail(agent_id, req);
            return { success: true, message: "Agent details retrieved successfully", data };
        } catch (err) {
            console.error("Error retrieving agent details:", err);
            throw err;
        }
    }

    /**
     * Retrieves a paginated list of default AI agents.
     * 
     * @param {dto.AIAgent.get_agent} dto - The DTO containing pagination details.
     * @param {Request} req - The request object.
     * @returns {Promise<any>} - The response object with agents list.
     */
    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('default/list')
    async getDefaultAIAgents(@Query() dto: dto.AIAgent.get_agent, @Request() req: any): Promise<any> {
        try {
            const data = await this.AdminAiAgentService.getDefaultAIAgents(req, dto);
            return { success: true, message: "Agents retrieved successfully", data };
        } catch (err) {
            console.error("Error retrieving agents:", err);
            throw err;
        }
    }


    /**
     * Retrieves a paginated history of chat logs of selected agent.
     * 
     * @param {dto.AIAgent.get_agent} dto - The DTO containing pagination details.
     * @param {Request} req - The request object.
     * @returns {Promise<any>} - The response object with agents list.
     */
    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Get('/:_id/logs')
    async agentCallChatHistory(@Param('_id') agent_id: string, @Query() body: dto.AIAgent.get_agent_chat, @Request() req: any): Promise<any> {
        try {
            let data = await this.AdminAiAgentService.chatLogsHistory(agent_id, body);
            let response = {
                success: true,
                message: "success",
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

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

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Post('/:_id/script')
    async workspace_script_create(@Param('_id') agent_id: string, @Body() body: dto.AIAgent.workspace_script_create, @Request() req: any) {
        try {
            let data = await this.AdminAiAgentService.scriptCreate(agent_id, body, req)
            let response = {
                message: "success",
                success: true,
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Put('/:_id/script')
    async workspace_script_edit(@Param('_id') agent_id: string, @Body() body: dto.AIAgent.workspace_script_edit, @Request() req: any) {
        try {
            let data = await this.AdminAiAgentService.scriptEdit(agent_id, body, req)
            let response = {
                message: "success",
                success: true,
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

    @UseGuards(AuthGuard)
    @ApiBearerAuth('authorization')
    @Patch('/:_id/script/:script_id')
    async script_enable_disable(
        @Param('_id') agent_id: string,
        @Param('script_id') script_id: string,
        @Body() body: dto.AIAgent.workspace_script_status,
        @Request() req: any
    ) {
        try {
            let data = await this.AdminAiAgentService.scriptEnableDisable(agent_id, script_id, req, body)
            let response = {
                message: "success",
                success: true,
                data: data,
            };
            return response;
        } catch (err) {
            throw err;
        }
    }

}

