var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import IncidentService from "../../Services/IncidentService";
import IncidentStateTimelineService from "../../Services/IncidentStateTimelineService";
import IncidentInternalNoteService from "../../Services/IncidentInternalNoteService";
import IncidentPublicNoteService from "../../Services/IncidentPublicNoteService";
import WorkspaceUtil from "../Workspace/Workspace";
import WorkspaceProjectAuthTokenService from "../../Services/WorkspaceProjectAuthTokenService";
import logger from "../Logger";
import CaptureSpan from "../Telemetry/CaptureSpan";
import OneUptimeDate from "../../../Types/Date";
import SortOrder from "../../../Types/BaseDatabase/SortOrder";
export default class IncidentAIContextBuilder {
    static async buildIncidentContext(data) {
        const incident = await IncidentService.findOneById({
            id: data.incidentId,
            select: {
                _id: true,
                title: true,
                description: true,
                createdAt: true,
                postmortemNote: true,
                remediationNotes: true,
                rootCause: true,
                customFields: true,
                projectId: true,
                incidentSeverity: {
                    name: true,
                    color: true,
                },
                currentIncidentState: {
                    name: true,
                    color: true,
                },
                monitors: {
                    name: true,
                },
                labels: {
                    name: true,
                    color: true,
                },
                postUpdatesToWorkspaceChannels: true,
            },
            props: {
                isRoot: true,
            },
        });
        if (!incident) {
            throw new Error("Incident not found");
        }
        // Fetch state timeline
        const stateTimeline = await IncidentStateTimelineService.findBy({
            query: {
                incidentId: data.incidentId,
            },
            select: {
                _id: true,
                createdAt: true,
                startsAt: true,
                endsAt: true,
                rootCause: true,
                incidentState: {
                    name: true,
                    color: true,
                },
                createdByUser: {
                    name: true,
                    email: true,
                },
            },
            sort: {
                startsAt: SortOrder.Ascending,
            },
            limit: 100,
            skip: 0,
            props: {
                isRoot: true,
            },
        });
        // Fetch internal notes
        const internalNotes = await IncidentInternalNoteService.findBy({
            query: {
                incidentId: data.incidentId,
            },
            select: {
                _id: true,
                note: true,
                createdAt: true,
                createdByUser: {
                    name: true,
                    email: true,
                },
            },
            sort: {
                createdAt: SortOrder.Ascending,
            },
            limit: 100,
            skip: 0,
            props: {
                isRoot: true,
            },
        });
        // Fetch public notes
        const publicNotes = await IncidentPublicNoteService.findBy({
            query: {
                incidentId: data.incidentId,
            },
            select: {
                _id: true,
                note: true,
                createdAt: true,
                postedAt: true,
                createdByUser: {
                    name: true,
                    email: true,
                },
            },
            sort: {
                createdAt: SortOrder.Ascending,
            },
            limit: 100,
            skip: 0,
            props: {
                isRoot: true,
            },
        });
        // Fetch workspace messages if requested and channels exist
        let workspaceMessages = [];
        const workspaceChannels = incident.postUpdatesToWorkspaceChannels;
        if (data.includeWorkspaceMessages &&
            workspaceChannels &&
            workspaceChannels.length > 0 &&
            incident.projectId) {
            try {
                const fetchParams = {
                    projectId: incident.projectId,
                    workspaceChannels: workspaceChannels,
                    limit: data.workspaceMessageLimit || 500,
                };
                if (incident.createdAt) {
                    fetchParams.oldestTimestamp = incident.createdAt;
                }
                workspaceMessages =
                    await this.getWorkspaceMessagesForIncident(fetchParams);
            }
            catch (error) {
                logger.error(`Error fetching workspace messages: ${error}`);
                // Continue without workspace messages
            }
        }
        return {
            incident,
            stateTimeline,
            internalNotes,
            publicNotes,
            workspaceMessages,
        };
    }
    static formatIncidentContextForPostmortem(contextData, template) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
        const { incident, stateTimeline, internalNotes, publicNotes, workspaceMessages, } = contextData;
        let contextText = "";
        // Basic incident information
        contextText += "# Incident Information\n\n";
        contextText += `**Title:** ${incident.title || "N/A"}\n\n`;
        contextText += `**Description:** ${incident.description || "N/A"}\n\n`;
        contextText += `**Severity:** ${((_a = incident.incidentSeverity) === null || _a === void 0 ? void 0 : _a.name) || "N/A"}\n\n`;
        contextText += `**Current State:** ${((_b = incident.currentIncidentState) === null || _b === void 0 ? void 0 : _b.name) || "N/A"}\n\n`;
        contextText += `**Created At:** ${incident.createdAt ? OneUptimeDate.getDateAsFormattedString(incident.createdAt) : "N/A"}\n\n`;
        // Affected monitors
        if (incident.monitors && incident.monitors.length > 0) {
            contextText += "**Affected Monitors:** ";
            contextText += incident.monitors
                .map((m) => {
                return m.name;
            })
                .join(", ");
            contextText += "\n\n";
        }
        // Labels
        if (incident.labels && incident.labels.length > 0) {
            contextText += "**Labels:** ";
            contextText += incident.labels
                .map((l) => {
                return l.name;
            })
                .join(", ");
            contextText += "\n\n";
        }
        // Root cause if available
        if (incident.rootCause) {
            contextText += `**Root Cause:** ${incident.rootCause}\n\n`;
        }
        // Remediation notes if available
        if (incident.remediationNotes) {
            contextText += `**Remediation Notes:** ${incident.remediationNotes}\n\n`;
        }
        // State timeline
        if (stateTimeline.length > 0) {
            contextText += "# State Timeline\n\n";
            for (const timeline of stateTimeline) {
                const startTime = timeline.startsAt
                    ? OneUptimeDate.getDateAsFormattedString(timeline.startsAt)
                    : "N/A";
                const stateName = ((_d = (_c = timeline.incidentState) === null || _c === void 0 ? void 0 : _c.name) === null || _d === void 0 ? void 0 : _d.toString()) || "Unknown";
                const createdBy = ((_f = (_e = timeline.createdByUser) === null || _e === void 0 ? void 0 : _e.name) === null || _f === void 0 ? void 0 : _f.toString()) ||
                    ((_h = (_g = timeline.createdByUser) === null || _g === void 0 ? void 0 : _g.email) === null || _h === void 0 ? void 0 : _h.toString()) ||
                    "System";
                contextText += `- **${startTime}**: State changed to **${stateName}** by ${createdBy}\n`;
                if (timeline.rootCause) {
                    contextText += `  - Root cause noted: ${timeline.rootCause}\n`;
                }
            }
            contextText += "\n";
        }
        // Internal notes
        if (internalNotes.length > 0) {
            contextText += "# Internal Notes (Private)\n\n";
            for (const note of internalNotes) {
                const noteTime = note.createdAt
                    ? OneUptimeDate.getDateAsFormattedString(note.createdAt)
                    : "N/A";
                const createdBy = ((_k = (_j = note.createdByUser) === null || _j === void 0 ? void 0 : _j.name) === null || _k === void 0 ? void 0 : _k.toString()) ||
                    ((_m = (_l = note.createdByUser) === null || _l === void 0 ? void 0 : _l.email) === null || _m === void 0 ? void 0 : _m.toString()) ||
                    "Unknown";
                contextText += `**[${noteTime}] ${createdBy}:**\n`;
                contextText += `${note.note || "N/A"}\n\n`;
            }
        }
        // Public notes
        if (publicNotes.length > 0) {
            contextText += "# Public Notes\n\n";
            for (const note of publicNotes) {
                const noteTime = note.postedAt
                    ? OneUptimeDate.getDateAsFormattedString(note.postedAt)
                    : note.createdAt
                        ? OneUptimeDate.getDateAsFormattedString(note.createdAt)
                        : "N/A";
                const createdBy = ((_p = (_o = note.createdByUser) === null || _o === void 0 ? void 0 : _o.name) === null || _p === void 0 ? void 0 : _p.toString()) ||
                    ((_r = (_q = note.createdByUser) === null || _q === void 0 ? void 0 : _q.email) === null || _r === void 0 ? void 0 : _r.toString()) ||
                    "Unknown";
                contextText += `**[${noteTime}] ${createdBy}:**\n`;
                contextText += `${note.note || "N/A"}\n\n`;
            }
        }
        // Workspace messages (Slack/Teams)
        if (workspaceMessages.length > 0) {
            contextText += "# Discussion from Incident Channel\n\n";
            contextText += WorkspaceUtil.formatMessagesAsContext(workspaceMessages, {
                includeTimestamp: true,
                includeUsername: true,
                maxLength: 30000,
            });
            contextText += "\n\n";
        }
        // System prompt for postmortem generation
        let systemPrompt;
        if (template) {
            // When a template is provided, strictly fill only the template
            systemPrompt = `You are an expert Site Reliability Engineer (SRE) and incident response specialist. Your task is to fill in an incident postmortem template based on the provided incident data.

CRITICAL INSTRUCTIONS:
- You MUST use ONLY the exact template structure provided below
- Fill in each section of the template with relevant information from the incident data
- Do NOT add any new sections, headers, or content that is not part of the template
- Do NOT add introductions, conclusions, or any text outside the template structure
- If a section in the template has no relevant data, write "No data available" or leave the placeholder text
- Be blameless - focus on systemic improvements rather than individual blame
- Write in a professional, clear, and concise manner

TEMPLATE TO FILL (use this exact structure):

${template}`;
        }
        else {
            // When no template is provided, use standard format
            systemPrompt = `You are an expert Site Reliability Engineer (SRE) and incident response specialist. Your task is to generate a comprehensive, well-structured incident postmortem based on the provided incident data.

The postmortem should:
1. Be written in a blameless manner, focusing on systemic improvements rather than individual blame
2. Include a clear executive summary
3. Provide a detailed timeline of events
4. Identify the root cause(s) and contributing factors
5. Outline the impact on users and systems
6. List actionable items to prevent recurrence
7. Include lessons learned

Use a standard incident postmortem format with sections for: Executive Summary, Timeline, Root Cause Analysis, Impact, Action Items, and Lessons Learned.

Write in a professional, clear, and concise manner. Use markdown formatting for better readability.`;
        }
        // Build user message based on whether template is provided
        const userMessage = template
            ? `Fill in the template above using ONLY the following incident data. Output only the filled template, nothing else:\n\n${contextText}`
            : `Based on the following incident data, please generate a comprehensive incident postmortem:\n\n${contextText}`;
        // Build messages array
        const messages = [
            {
                role: "system",
                content: systemPrompt,
            },
            {
                role: "user",
                content: userMessage,
            },
        ];
        return {
            contextText,
            systemPrompt,
            messages,
        };
    }
    static formatIncidentContextForNote(contextData, noteType, template) {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
        const { incident, stateTimeline, internalNotes, publicNotes, workspaceMessages, } = contextData;
        let contextText = "";
        // Basic incident information
        contextText += "# Incident Information\n\n";
        contextText += `**Title:** ${incident.title || "N/A"}\n\n`;
        contextText += `**Description:** ${incident.description || "N/A"}\n\n`;
        contextText += `**Severity:** ${((_a = incident.incidentSeverity) === null || _a === void 0 ? void 0 : _a.name) || "N/A"}\n\n`;
        contextText += `**Current State:** ${((_b = incident.currentIncidentState) === null || _b === void 0 ? void 0 : _b.name) || "N/A"}\n\n`;
        contextText += `**Created At:** ${incident.createdAt ? OneUptimeDate.getDateAsFormattedString(incident.createdAt) : "N/A"}\n\n`;
        // Affected monitors
        if (incident.monitors && incident.monitors.length > 0) {
            contextText += "**Affected Monitors:** ";
            contextText += incident.monitors
                .map((m) => {
                return m.name;
            })
                .join(", ");
            contextText += "\n\n";
        }
        // Labels
        if (incident.labels && incident.labels.length > 0) {
            contextText += "**Labels:** ";
            contextText += incident.labels
                .map((l) => {
                return l.name;
            })
                .join(", ");
            contextText += "\n\n";
        }
        // Root cause if available
        if (incident.rootCause) {
            contextText += `**Root Cause:** ${incident.rootCause}\n\n`;
        }
        // State timeline
        if (stateTimeline.length > 0) {
            contextText += "# State Timeline\n\n";
            for (const timeline of stateTimeline) {
                const startTime = timeline.startsAt
                    ? OneUptimeDate.getDateAsFormattedString(timeline.startsAt)
                    : "N/A";
                const stateName = ((_d = (_c = timeline.incidentState) === null || _c === void 0 ? void 0 : _c.name) === null || _d === void 0 ? void 0 : _d.toString()) || "Unknown";
                const createdBy = ((_f = (_e = timeline.createdByUser) === null || _e === void 0 ? void 0 : _e.name) === null || _f === void 0 ? void 0 : _f.toString()) ||
                    ((_h = (_g = timeline.createdByUser) === null || _g === void 0 ? void 0 : _g.email) === null || _h === void 0 ? void 0 : _h.toString()) ||
                    "System";
                contextText += `- **${startTime}**: State changed to **${stateName}** by ${createdBy}\n`;
                if (timeline.rootCause) {
                    contextText += `  - Root cause noted: ${timeline.rootCause}\n`;
                }
            }
            contextText += "\n";
        }
        // Include internal notes for context (for both note types)
        if (internalNotes.length > 0) {
            contextText += "# Internal Notes (Private)\n\n";
            for (const note of internalNotes) {
                const noteTime = note.createdAt
                    ? OneUptimeDate.getDateAsFormattedString(note.createdAt)
                    : "N/A";
                const createdBy = ((_k = (_j = note.createdByUser) === null || _j === void 0 ? void 0 : _j.name) === null || _k === void 0 ? void 0 : _k.toString()) ||
                    ((_m = (_l = note.createdByUser) === null || _l === void 0 ? void 0 : _l.email) === null || _m === void 0 ? void 0 : _m.toString()) ||
                    "Unknown";
                contextText += `**[${noteTime}] ${createdBy}:**\n`;
                contextText += `${note.note || "N/A"}\n\n`;
            }
        }
        // Public notes
        if (publicNotes.length > 0) {
            contextText += "# Public Notes\n\n";
            for (const note of publicNotes) {
                const noteTime = note.postedAt
                    ? OneUptimeDate.getDateAsFormattedString(note.postedAt)
                    : note.createdAt
                        ? OneUptimeDate.getDateAsFormattedString(note.createdAt)
                        : "N/A";
                const createdBy = ((_p = (_o = note.createdByUser) === null || _o === void 0 ? void 0 : _o.name) === null || _p === void 0 ? void 0 : _p.toString()) ||
                    ((_r = (_q = note.createdByUser) === null || _q === void 0 ? void 0 : _q.email) === null || _r === void 0 ? void 0 : _r.toString()) ||
                    "Unknown";
                contextText += `**[${noteTime}] ${createdBy}:**\n`;
                contextText += `${note.note || "N/A"}\n\n`;
            }
        }
        // Workspace messages (Slack/Teams)
        if (workspaceMessages.length > 0) {
            contextText += "# Discussion from Incident Channel\n\n";
            contextText += WorkspaceUtil.formatMessagesAsContext(workspaceMessages, {
                includeTimestamp: true,
                includeUsername: true,
                maxLength: 20000,
            });
            contextText += "\n\n";
        }
        // System prompt for note generation
        let systemPrompt;
        if (noteType === "public") {
            if (template) {
                systemPrompt = `You are an expert incident communicator. Your task is to fill in a public incident note template based on the provided incident data.

CRITICAL INSTRUCTIONS:
- You MUST use ONLY the exact template structure provided below
- Fill in each section of the template with relevant information from the incident data
- Do NOT add any new sections, headers, or content that is not part of the template
- Do NOT add introductions, conclusions, or any text outside the template structure
- Write in a professional, clear, and customer-friendly manner
- Focus on what customers need to know: impact, current status, and next steps
- Avoid technical jargon - keep it understandable for non-technical readers
- Be concise but informative

TEMPLATE TO FILL (use this exact structure):

${template}`;
            }
            else {
                systemPrompt = `You are an expert incident communicator. Your task is to generate a public incident note that will be visible to customers on the status page.

The note should:
1. Be written in a professional, customer-friendly tone
2. Clearly communicate the current status of the incident
3. Explain the impact on users without excessive technical details
4. Provide information about what is being done to resolve the issue
5. Set appropriate expectations about resolution timing if known
6. Be concise but informative

DO NOT include:
- Internal technical details that customers don't need
- Blame or finger-pointing
- Confidential information
- Excessive jargon

Write in markdown format for better readability.`;
            }
        }
        else if (template) {
            // Internal note with template
            systemPrompt = `You are an expert Site Reliability Engineer (SRE). Your task is to fill in an internal incident note template based on the provided incident data.

CRITICAL INSTRUCTIONS:
- You MUST use ONLY the exact template structure provided below
- Fill in each section of the template with relevant information from the incident data
- Do NOT add any new sections, headers, or content that is not part of the template
- Do NOT add introductions, conclusions, or any text outside the template structure
- Be technical and detailed - this is for the internal team
- Include relevant technical details, metrics, and observations

TEMPLATE TO FILL (use this exact structure):

${template}`;
        }
        else {
            // Internal note without template
            systemPrompt = `You are an expert Site Reliability Engineer (SRE). Your task is to generate an internal incident note for the response team.

The note should:
1. Provide technical details about the current situation
2. Document observations, findings, or actions taken
3. Include relevant metrics or error messages if mentioned in the context
4. Be detailed enough to help team members understand the situation
5. Use technical language appropriate for the engineering team

Write in markdown format for better readability. Be thorough and technical.`;
        }
        // Build user message
        const userMessage = template
            ? `Fill in the template above using ONLY the following incident data. Output only the filled template, nothing else:\n\n${contextText}`
            : `Based on the following incident data, please generate ${noteType === "public" ? "a customer-facing public" : "an internal technical"} incident note:\n\n${contextText}`;
        // Build messages array
        const messages = [
            {
                role: "system",
                content: systemPrompt,
            },
            {
                role: "user",
                content: userMessage,
            },
        ];
        return {
            contextText,
            systemPrompt,
            messages,
        };
    }
    static buildGenericAIContext(data) {
        let userContent = data.userPrompt;
        if (data.context) {
            userContent = `${data.userPrompt}\n\nContext:\n${data.context}`;
        }
        const messages = [
            {
                role: "system",
                content: data.systemPrompt,
            },
            {
                role: "user",
                content: userContent,
            },
        ];
        return {
            contextText: data.context || "",
            systemPrompt: data.systemPrompt,
            messages,
        };
    }
    static async getWorkspaceMessagesForIncident(data) {
        const allMessages = [];
        for (const channel of data.workspaceChannels) {
            try {
                // Get auth token for this workspace type
                const projectAuth = await WorkspaceProjectAuthTokenService.getProjectAuth({
                    projectId: data.projectId,
                    workspaceType: channel.workspaceType,
                });
                if (!projectAuth || !projectAuth.authToken) {
                    logger.debug(`No auth token found for workspace type: ${channel.workspaceType}`);
                    continue;
                }
                const messagesParams = {
                    channelId: channel.id,
                    authToken: projectAuth.authToken,
                    projectId: data.projectId,
                    workspaceType: channel.workspaceType,
                };
                if (channel.teamId) {
                    messagesParams.teamId = channel.teamId;
                }
                if (data.limit !== undefined) {
                    messagesParams.limit = data.limit;
                }
                if (data.oldestTimestamp) {
                    messagesParams.oldestTimestamp = data.oldestTimestamp;
                }
                const messages = await WorkspaceUtil.getChannelMessages(messagesParams);
                allMessages.push(...messages);
            }
            catch (error) {
                logger.error(`Error fetching messages from channel ${channel.id}: ${error}`);
                // Continue with other channels even if one fails
            }
        }
        // Sort all messages by timestamp
        allMessages.sort((a, b) => {
            return a.timestamp.getTime() - b.timestamp.getTime();
        });
        return allMessages;
    }
}
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], IncidentAIContextBuilder, "buildIncidentContext", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String]),
    __metadata("design:returntype", Object)
], IncidentAIContextBuilder, "formatIncidentContextForPostmortem", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object, String, String]),
    __metadata("design:returntype", Object)
], IncidentAIContextBuilder, "formatIncidentContextForNote", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Object)
], IncidentAIContextBuilder, "buildGenericAIContext", null);
__decorate([
    CaptureSpan(),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Object]),
    __metadata("design:returntype", Promise)
], IncidentAIContextBuilder, "getWorkspaceMessagesForIncident", null);
//# sourceMappingURL=IncidentAIContextBuilder.js.map