import { Injectable } from '@nestjs/common';
import * as dto from './dto/index';
import { ModelService } from '../model/model.service';
import { CommonService } from '../common/common.service';
import { dashboard_aggregation } from './dashboard.aggrigation';


@Injectable()
export class DashboardService {

    constructor(
        private readonly Model: ModelService,
        private readonly CommonService: CommonService
    ) {

    }


    async getAgentsWithCalls(query: dto.Dashboard.getAgents) {
        try {
            const { pagination, limit, workspace_id } = query;

            const agents = await this.Model.CallModel.aggregate([
                await dashboard_aggregation.match_data_with_workspace_id(workspace_id),
                await dashboard_aggregation.match_data_with_type("CALL"),
                await dashboard_aggregation.parse_call_duration(),
                await dashboard_aggregation.group_call_by_agents(),
                await dashboard_aggregation.sentiment_breakdown(),
                await dashboard_aggregation.sentiment_percentage_and_top_emotion(),
                await dashboard_aggregation.agent_lookup(),
                await dashboard_aggregation.agent_unwind(),
                await dashboard_aggregation.call_duration_from_seconds(),
                await dashboard_aggregation.project_agent_list_call(),
                await dashboard_aggregation.facetData(pagination, limit)
            ]);
    
            return {
                data: agents[0]?.data || [],
                count: agents[0]?.count[0]?.count || 0 
            }
        } catch (error) {
            throw error
        }
    }

    async getAgentsWithChats(query: dto.Dashboard.getAgents) {
        try {
            const { pagination, limit, workspace_id } = query;

            const agents = await this.Model.CallModel.aggregate([
                await dashboard_aggregation.match_data_with_workspace_id(workspace_id),
                await dashboard_aggregation.match_data_with_type("CHAT"),
                await dashboard_aggregation.parse_chat_duration(),
                await dashboard_aggregation.group_call_by_agents(),
                await dashboard_aggregation.sentiment_breakdown(),
                await dashboard_aggregation.sentiment_percentage_and_top_emotion(),
                await dashboard_aggregation.agent_lookup(),
                await dashboard_aggregation.agent_unwind(),
                await dashboard_aggregation.call_duration_from_miliseconds(),
                await dashboard_aggregation.project_agent_list_call(),
                await dashboard_aggregation.facetData(pagination, limit)
            ]);
    
            return {
                data: agents[0]?.data || [],
                count: agents[0]?.count[0]?.count || 0 
            }
        } catch (error) {
            throw error
        }
    }

    async getVendorDashboardCounts() {
        try {
            const data = await this.Model.CallModel.aggregate([
                {
                    $facet: {
                      activeCall: [
                        { $match: { type: "CALL", status: "ACTIVE" } },
                        { $count: "count" },
                      ],
                      totalCall: [
                        { $match: { type: "CALL" } },
                        { $count: "count" },
                      ],
                      activeChat: [
                        { $match: { type: "CHAT", status: "ACTIVE" } },
                        { $count: "count" },
                      ],
                      totalChat: [
                        { $match: { type: "CHAT" } },
                        { $count: "count" },
                      ],
                    },
                },
                {
                    $project: {
                        activeCall: { $ifNull: [{ $arrayElemAt: ["$activeCall.count", 0] }, 0] },
                        totalCall: { $ifNull: [{ $arrayElemAt: ["$totalCall.count", 0] }, 0] },
                        activeChat: { $ifNull: [{ $arrayElemAt: ["$activeChat.count", 0] }, 0] },
                        totalChat: { $ifNull: [{ $arrayElemAt: ["$totalChat.count", 0] }, 0] },
                    },
                },
            ]);
    
            return data[0]
        } catch (error) {
            throw error
        }
    }
}
