import { Inject, Injectable } from '@nestjs/common';
import { ModelService } from '../model/model.service';
import { Types } from 'mongoose'
import { RagService } from '../rag/rag.service';
@Injectable()
export class CallService {

    constructor(
        private model: ModelService,
        private RagService: RagService,

    ) {

    }


    async save_call(data: any) {
        try {
            let { call_id, stream_id } = data
            await this.model.CallModel.create({
                call_id: call_id,
                stream_id: stream_id
            })
        } catch (error) {
            throw error
        }
    }

    async update_call(data: any) {
        try {
            let { stream_id, status, call_duration, last_message } = data
            await this.model.CallModel.findOneAndUpdate({ stream_id: stream_id },
                {
                    ...(status && { status: status }),
                    ...(call_duration && { call_duration: call_duration }),
                    ...(last_message && { last_message: last_message })
                }
            )
        } catch (error) {
            throw error
        }
    }

    async save_call_transcript(data: any) {
        try {
            let { stream_id, text, role } = data
            let call = await this.model.CallModel.findOne({ stream_id: stream_id })

            await this.model.CallTranscriptModel.create({
                call_id: call._id,
                stream_id: stream_id,
                text: text,
                role: role,
            })
        } catch (error) {
            throw error
        }
    }


    async call_summerize(data: any) {
        try {
            let { stream_id } = data
            let call = await this.model.CallModel.findOne({ stream_id: stream_id })

            let chatData = await this.model.CallTranscriptModel.find({ call_id: call._id }, {}, { sort: { _id: 1 } })
            let chat = chatData
                .map((message) => `${message.role.charAt(0).toUpperCase() + message.role.slice(1)}: ${message.text}`)
                .join('\n');

            let summry = await this.RagService.chatSummrize(chat)
            // console.log("summry", summry);
            await this.model.CallModel.findOneAndUpdate({ stream_id: stream_id },
                {
                    summary: summry
                }
            )
        } catch (error) {
            throw error
        }
    }


    async chat_summerize(chat_id: any) {
        try {
            let call = await this.model.CallModel.findOne({ _id: new Types.ObjectId(chat_id) })

            let chatData = await this.model.CallTranscriptModel.find({ call_id: call._id }, {}, { sort: { _id: 1 } })
            let chat = chatData
                .map((message) => `${message.role.charAt(0).toUpperCase() + message.role.slice(1)}: ${message.text}`)
                .join('\n');

            let summry = await this.RagService.chatSummrize(chat)
            // console.log("summry", summry);
            await this.model.CallModel.findOneAndUpdate({ _id: new Types.ObjectId(chat_id) },
                {
                    summary: summry
                }
            )
        } catch (error) {
            throw error
        }
    }

    async chat_history(stream_id: any) {
        try {
            let call = await this.model.CallModel.findOne({ stream_id: stream_id })
            let chatData = await this.model.CallTranscriptModel.find({ call_id: call._id }, {}, { sort: { _id: 1 } })
            let chat = chatData
                .map((message) => `${message.role.charAt(0).toUpperCase() + message.role.slice(1)}: ${message.text}`)
                .join('\n');

            return chat
        } catch (error) {
            throw error
        }
    }
}
