import app from "../../gong.app.mjs";
export default {
  key: "gong-retrieve-transcripts-of-calls",
  name: "Retrieve Transcripts Of Calls",
  description: "Retrieve transcripts of calls. [See the documentation](https://us-66463.app.gong.io/settings/api/documentation#post-/v2/calls/transcript)",
  type: "action",
  version: "0.0.5",
  annotations: {
    destructiveHint: false,
    openWorldHint: true,
    readOnlyHint: false,
  },
  props: {
    app,
    fromDateTime: {
      optional: true,
      propDefinition: [
        app,
        "fromDateTime",
      ],
    },
    toDateTime: {
      optional: true,
      propDefinition: [
        app,
        "toDateTime",
      ],
    },
    workspaceId: {
      optional: true,
      propDefinition: [
        app,
        "workspaceId",
      ],
    },
    callIds: {
      propDefinition: [
        app,
        "callIds",
      ],
    },
    returnSimplifiedTranscript: {
      type: "boolean",
      label: "Return Simplified Transcript",
      description: "If true, returns a simplified version of the transcript with normalized speaker IDs and formatted timestamps",
      optional: true,
      default: false,
    },
  },
  methods: {
    retrieveTranscriptsOfCalls(args = {}) {
      return this.app.post({
        path: "/calls/transcript",
        ...args,
      });
    },
    millisToTimestamp(millis) {
      const totalSeconds = Math.floor(millis / 1000);
      const minutes = Math.floor(totalSeconds / 60);
      const seconds = totalSeconds % 60;
      return `[${minutes.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}]`;
    },
    simplifyTranscript(originalResponse) {
      const simplified = {
        ...originalResponse,
        callTranscripts: originalResponse.callTranscripts.map((callTranscript) => {
          
          const speakerMap = new Map();
          let speakerCounter = 1;
          let currentSpeaker = null;
          let currentTopic = null;
          let formattedTranscript = "";
          
          const allSentences = [];
          callTranscript.transcript.forEach((segment) => {
            segment.sentences.forEach((sentence) => {
              allSentences.push({
                ...sentence,
                speakerId: segment.speakerId,
                topic: segment.topic,
              });
            });
          });
          
          allSentences.sort((a, b) => a.start - b.start);
          
          allSentences.forEach((sentence) => {
            
            if (!speakerMap.has(sentence.speakerId)) {
              speakerMap.set(sentence.speakerId, `Speaker ${speakerCounter}`);
              speakerCounter++;
            }
            const speaker = speakerMap.get(sentence.speakerId);
            const timestamp = this.millisToTimestamp(sentence.start);
            
            if (sentence.topic !== currentTopic) {
              currentTopic = sentence.topic;
              if (currentTopic) {
                formattedTranscript += `\nTopic: ${currentTopic}\n-------------------\n\n`;
              }
            }
            
            if (speaker !== currentSpeaker) {
              currentSpeaker = speaker;
              formattedTranscript += `\n${speaker}:\n`;
            }
            
            formattedTranscript += `${timestamp} ${sentence.text}\n`;
          });
          return {
            callId: callTranscript.callId,
            formattedTranscript: formattedTranscript.trim(),
          };
        }),
      };
      return simplified;
    },
  },
  async run({ $: step }) {
    const {
      retrieveTranscriptsOfCalls,
      returnSimplifiedTranscript,
      simplifyTranscript,
      ...filter
    } = this;
    const response = await retrieveTranscriptsOfCalls({
      step,
      data: {
        filter,
      },
      summary: (response) => `Successfully retrieved transcripts of calls with request ID \`${response.requestId}\`.`,
    });
    if (returnSimplifiedTranscript) {
      return simplifyTranscript(response);
    }
    return response;
  },
};