import ObjectID from "../../../Types/ObjectID";
import Incident from "../../../Models/DatabaseModels/Incident";
import IncidentStateTimeline from "../../../Models/DatabaseModels/IncidentStateTimeline";
import IncidentInternalNote from "../../../Models/DatabaseModels/IncidentInternalNote";
import IncidentPublicNote from "../../../Models/DatabaseModels/IncidentPublicNote";
import IncidentService from "../../Services/IncidentService";
import IncidentStateTimelineService from "../../Services/IncidentStateTimelineService";
import IncidentInternalNoteService from "../../Services/IncidentInternalNoteService";
import IncidentPublicNoteService from "../../Services/IncidentPublicNoteService";
import WorkspaceUtil, { WorkspaceChannelMessage } from "../Workspace/Workspace";
import WorkspaceProjectAuthTokenService from "../../Services/WorkspaceProjectAuthTokenService";
import WorkspaceProjectAuthToken from "../../../Models/DatabaseModels/WorkspaceProjectAuthToken";
import logger from "../Logger";
import CaptureSpan from "../Telemetry/CaptureSpan";
import OneUptimeDate from "../../../Types/Date";
import SortOrder from "../../../Types/BaseDatabase/SortOrder";
import { LLMMessage } from "../LLM/LLMService";
import NotificationRuleWorkspaceChannel from "../../../Types/Workspace/NotificationRules/NotificationRuleWorkspaceChannel";
import WorkspaceType from "../../../Types/Workspace/WorkspaceType";

export interface IncidentContextData {
  incident: Incident;
  stateTimeline: Array<IncidentStateTimeline>;
  internalNotes: Array<IncidentInternalNote>;
  publicNotes: Array<IncidentPublicNote>;
  workspaceMessages: Array<WorkspaceChannelMessage>;
}

export interface AIGenerationContext {
  contextText: string;
  systemPrompt: string;
  messages: Array<LLMMessage>;
}

export default class IncidentAIContextBuilder {
  @CaptureSpan()
  public static async buildIncidentContext(data: {
    incidentId: ObjectID;
    includeWorkspaceMessages?: boolean;
    workspaceMessageLimit?: number;
  }): Promise<IncidentContextData> {
    const incident: Incident | null = 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: Array<IncidentStateTimeline> =
      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: Array<IncidentInternalNote> =
      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: Array<IncidentPublicNote> =
      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: Array<WorkspaceChannelMessage> = [];

    const workspaceChannels:
      | Array<NotificationRuleWorkspaceChannel>
      | undefined = incident.postUpdatesToWorkspaceChannels as
      | Array<NotificationRuleWorkspaceChannel>
      | undefined;

    if (
      data.includeWorkspaceMessages &&
      workspaceChannels &&
      workspaceChannels.length > 0 &&
      incident.projectId
    ) {
      try {
        const fetchParams: {
          projectId: ObjectID;
          workspaceChannels: Array<NotificationRuleWorkspaceChannel>;
          limit?: number;
          oldestTimestamp?: Date;
        } = {
          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,
    };
  }

  @CaptureSpan()
  public static formatIncidentContextForPostmortem(
    contextData: IncidentContextData,
    template?: string,
  ): AIGenerationContext {
    const {
      incident,
      stateTimeline,
      internalNotes,
      publicNotes,
      workspaceMessages,
    } = contextData;

    let contextText: string = "";

    // 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:** ${incident.incidentSeverity?.name || "N/A"}\n\n`;
    contextText += `**Current State:** ${incident.currentIncidentState?.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: { name?: string }) => {
          return m.name;
        })
        .join(", ");
      contextText += "\n\n";
    }

    // Labels
    if (incident.labels && incident.labels.length > 0) {
      contextText += "**Labels:** ";
      contextText += incident.labels
        .map((l: { name?: string }) => {
          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: string = timeline.startsAt
          ? OneUptimeDate.getDateAsFormattedString(timeline.startsAt)
          : "N/A";
        const stateName: string =
          timeline.incidentState?.name?.toString() || "Unknown";
        const createdBy: string =
          timeline.createdByUser?.name?.toString() ||
          timeline.createdByUser?.email?.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: string = note.createdAt
          ? OneUptimeDate.getDateAsFormattedString(note.createdAt)
          : "N/A";
        const createdBy: string =
          note.createdByUser?.name?.toString() ||
          note.createdByUser?.email?.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: string = note.postedAt
          ? OneUptimeDate.getDateAsFormattedString(note.postedAt)
          : note.createdAt
            ? OneUptimeDate.getDateAsFormattedString(note.createdAt)
            : "N/A";
        const createdBy: string =
          note.createdByUser?.name?.toString() ||
          note.createdByUser?.email?.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: string;

    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: string = 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: Array<LLMMessage> = [
      {
        role: "system",
        content: systemPrompt,
      },
      {
        role: "user",
        content: userMessage,
      },
    ];

    return {
      contextText,
      systemPrompt,
      messages,
    };
  }

  @CaptureSpan()
  public static formatIncidentContextForNote(
    contextData: IncidentContextData,
    noteType: "public" | "internal",
    template?: string,
  ): AIGenerationContext {
    const {
      incident,
      stateTimeline,
      internalNotes,
      publicNotes,
      workspaceMessages,
    } = contextData;

    let contextText: string = "";

    // 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:** ${incident.incidentSeverity?.name || "N/A"}\n\n`;
    contextText += `**Current State:** ${incident.currentIncidentState?.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: { name?: string }) => {
          return m.name;
        })
        .join(", ");
      contextText += "\n\n";
    }

    // Labels
    if (incident.labels && incident.labels.length > 0) {
      contextText += "**Labels:** ";
      contextText += incident.labels
        .map((l: { name?: string }) => {
          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: string = timeline.startsAt
          ? OneUptimeDate.getDateAsFormattedString(timeline.startsAt)
          : "N/A";
        const stateName: string =
          timeline.incidentState?.name?.toString() || "Unknown";
        const createdBy: string =
          timeline.createdByUser?.name?.toString() ||
          timeline.createdByUser?.email?.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: string = note.createdAt
          ? OneUptimeDate.getDateAsFormattedString(note.createdAt)
          : "N/A";
        const createdBy: string =
          note.createdByUser?.name?.toString() ||
          note.createdByUser?.email?.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: string = note.postedAt
          ? OneUptimeDate.getDateAsFormattedString(note.postedAt)
          : note.createdAt
            ? OneUptimeDate.getDateAsFormattedString(note.createdAt)
            : "N/A";
        const createdBy: string =
          note.createdByUser?.name?.toString() ||
          note.createdByUser?.email?.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: string;

    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: string = 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: Array<LLMMessage> = [
      {
        role: "system",
        content: systemPrompt,
      },
      {
        role: "user",
        content: userMessage,
      },
    ];

    return {
      contextText,
      systemPrompt,
      messages,
    };
  }

  @CaptureSpan()
  public static buildGenericAIContext(data: {
    systemPrompt: string;
    userPrompt: string;
    context?: string;
  }): AIGenerationContext {
    let userContent: string = data.userPrompt;

    if (data.context) {
      userContent = `${data.userPrompt}\n\nContext:\n${data.context}`;
    }

    const messages: Array<LLMMessage> = [
      {
        role: "system",
        content: data.systemPrompt,
      },
      {
        role: "user",
        content: userContent,
      },
    ];

    return {
      contextText: data.context || "",
      systemPrompt: data.systemPrompt,
      messages,
    };
  }

  @CaptureSpan()
  private static async getWorkspaceMessagesForIncident(data: {
    projectId: ObjectID;
    workspaceChannels: Array<NotificationRuleWorkspaceChannel>;
    limit?: number;
    oldestTimestamp?: Date;
  }): Promise<Array<WorkspaceChannelMessage>> {
    const allMessages: Array<WorkspaceChannelMessage> = [];

    for (const channel of data.workspaceChannels) {
      try {
        // Get auth token for this workspace type
        const projectAuth: WorkspaceProjectAuthToken | null =
          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: string;
          authToken: string;
          projectId: ObjectID;
          workspaceType: WorkspaceType;
          teamId?: string;
          limit?: number;
          oldestTimestamp?: Date;
        } = {
          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: Array<WorkspaceChannelMessage> =
          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: WorkspaceChannelMessage, b: WorkspaceChannelMessage) => {
        return a.timestamp.getTime() - b.timestamp.getTime();
      },
    );

    return allMessages;
  }
}
