import type { CallToolResult, McpServer } from "@modelcontextprotocol/server";
import { buildV3AuditLog } from "@/app/api/v3/lib/audit";
import { buildWorkflowApiContext, workflowsHandlers } from "@/app/api/v3/workflows/lib/context";
import { MCP_API_ROUTE } from "@/modules/mcp/constants";
import { type TMcpToolContext, getMcpAuthentication, getMcpRequestId, getMcpToolAuthInfo } from "../auth";
import { responseToMcpToolResult } from "../errors";
import { registerMcpTool } from "./guard-permissions";
import { isRecord, redactToolResultData, redactUnlessCallerHolds } from "./redacted-result";
import { runMcpMutation } from "./run-mcp-mutation";
import { RESPONSES_READ, withoutRunRespondentData } from "./workflow-run-redaction";
import {
  type TMcpCreateWorkflowInput,
  type TMcpDuplicateWorkflowInput,
  type TMcpGetWorkflowInput,
  type TMcpGetWorkflowRunInput,
  type TMcpListWorkflowRunsInput,
  type TMcpListWorkflowsInput,
  type TMcpPatchWorkflowInput,
  type TMcpTestWorkflowInput,
  type TMcpWorkflowIdInput,
  ZMcpCreateWorkflowInput,
  ZMcpDuplicateWorkflowInput,
  ZMcpGetWorkflowInput,
  ZMcpGetWorkflowRunInput,
  ZMcpListWorkflowRunsInput,
  ZMcpListWorkflowsInput,
  ZMcpPatchWorkflowInput,
  ZMcpTestWorkflowInput,
  ZMcpWorkflowIdInput,
} from "./workflow-schemas";
import { registerWorkflowTestRequestTools } from "./workflow-test-request";
import { registerWorkflowTestRunTools } from "./workflow-test-run";
import { registerWorkflowWebhookTools } from "./workflow-webhook";

type WorkflowApiContext = ReturnType<typeof buildWorkflowApiContext>;

export function buildListWorkflowsSearchParams(input: TMcpListWorkflowsInput): URLSearchParams {
  const searchParams = new URLSearchParams();

  searchParams.set("workspaceId", input.workspaceId);
  searchParams.set("limit", String(input.limit ?? 20));

  if (input.cursor) {
    searchParams.set("cursor", input.cursor);
  }

  if (input.sortBy) {
    searchParams.set("sortBy", input.sortBy);
  }

  if (input.filter?.name?.contains) {
    searchParams.set("filter[name][contains]", input.filter.name.contains);
  }

  input.filter?.status?.in?.forEach((status) => {
    searchParams.append("filter[status][in]", status);
  });

  return searchParams;
}

export function buildListWorkflowRunsSearchParams(input: TMcpListWorkflowRunsInput): URLSearchParams {
  const searchParams = new URLSearchParams();

  searchParams.set("workspaceId", input.workspaceId);
  searchParams.set("limit", String(input.limit ?? 20));

  if (input.cursor) {
    searchParams.set("cursor", input.cursor);
  }

  if (input.workflowId) {
    searchParams.set("workflowId", input.workflowId);
  }

  if (input.responseId) {
    searchParams.set("responseId", input.responseId);
  }

  input.filter?.status?.in?.forEach((status) => {
    searchParams.append("filter[status][in]", status);
  });

  if (input.filter?.isDryRun !== undefined) {
    searchParams.set("filter[isDryRun][eq]", String(input.filter.isDryRun));
  }

  return searchParams;
}

/**
 * The framework-agnostic workflow list handlers read query params off a `Request` (via
 * `new URL(req.url).searchParams`). MCP has no incoming HTTP request, so build a synthetic one that
 * is never dispatched — the handler only reads the search params, so the scheme/host are inert
 * placeholders (an absolute base is required solely to satisfy `new URL`).
 */
function buildWorkflowsListRequest(searchParams: URLSearchParams): Request {
  const url = new URL(MCP_API_ROUTE, "https://mcp.internal");
  url.search = searchParams.toString();
  return new Request(url);
}

/**
 * The create/patch/duplicate handlers read + validate a JSON body off `req.text()`. Build a
 * synthetic (never-dispatched) POST carrying the body; an empty object is sent for optional-body
 * operations so the handler never sees an empty string.
 */
function buildWorkflowsBodyRequest(body: unknown): Request {
  return new Request(new URL(MCP_API_ROUTE, "https://mcp.internal"), {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify(body ?? {}),
  });
}

/**
 * Run a workflow mutation with the same audit lifecycle the v3 route wrapper provides: build the
 * audit log, inject it into the context (the handlers populate it via ctx.recordAudit post-mutation),
 * mark success/failure, and always queue it. Mirrors the survey mutation tools.
 *
 * Two different contexts meet in this file, so they are named apart: `mcpCtx` is the MCP SDK's handler
 * context (it carries the verified token), while `ctx` stays the v3 workflow API context that
 * `workflowsHandlers` takes as a named property.
 */
async function runWorkflowMutation(
  mcpCtx: TMcpToolContext,
  action: Parameters<typeof buildV3AuditLog>[1],
  run: (ctx: WorkflowApiContext) => Promise<Response>
): Promise<CallToolResult> {
  // The shared runner owns the audit lifecycle; workflows only differ in building their API context
  // (which threads the same auditLog through) before delegating to the caller's operation.
  const result = await runMcpMutation(
    mcpCtx,
    { action, resource: "workflow" },
    ({ authentication, requestId, auditLog }) =>
      run(buildWorkflowApiContext(authentication, requestId, MCP_API_ROUTE, auditLog ?? undefined))
  );
  return redactUnlessCallerHolds(getMcpToolAuthInfo(mcpCtx), RESPONSES_READ, result, withoutLastRunError);
}

export const withoutRespondentData = (result: CallToolResult): CallToolResult =>
  redactToolResultData(result, withoutRunRespondentData);

/**
 * A `list_workflow_runs` result without each run's error text, which can quote what a failed step sent
 * (see {@link withoutRespondentData}), for a caller without Responses: Read. A run row carries no other
 * respondent-derived field. An error result passes through.
 */
const withoutRunError = (run: unknown): unknown => (isRecord(run) ? { ...run, error: null } : run);

export const withoutRespondentDataInRunList = (result: CallToolResult): CallToolResult =>
  redactToolResultData(result, withoutRunError);

/** A workflow with its embedded `lastRun.error` set to `null`, when it carries a run; anything else as is. */
const withoutLastRunError = (workflow: unknown): unknown => {
  if (!isRecord(workflow) || !isRecord(workflow.lastRun)) return workflow;
  return { ...workflow, lastRun: { ...workflow.lastRun, error: null } };
};

/**
 * A workflow result (`list_workflows`, `get_workflow`, or a mutation's) without each workflow's
 * `lastRun.error`, which can quote what a failed step sent (see {@link withoutRespondentData}), for a
 * caller without Responses: Read. A workflow carries no other respondent-derived field. An error result
 * passes through.
 */
export const withoutRespondentDataInWorkflows = (result: CallToolResult): CallToolResult =>
  redactToolResultData(result, withoutLastRunError);

export function registerWorkflowTools(server: McpServer): void {
  registerMcpTool(
    server,
    "list_workflows",
    {
      title: "List workflows",
      description:
        "List workflows in a Formbricks workspace using the v3 Workflows API contract. Without Responses: Read in the user's grant, each workflow's lastRun.error is null: it can quote respondents' answers.",
      inputSchema: ZMcpListWorkflowsInput,
      annotations: {
        readOnlyHint: true,
        destructiveHint: false,
        idempotentHint: true,
        openWorldHint: true,
      },
    },
    [{ permission: "workflows", level: "read" }],
    async (input: TMcpListWorkflowsInput, mcpCtx) => {
      const authInfo = getMcpToolAuthInfo(mcpCtx);
      const requestId = getMcpRequestId(authInfo);
      const ctx = buildWorkflowApiContext(getMcpAuthentication(authInfo), requestId, MCP_API_ROUTE);
      const response = await workflowsHandlers.list({
        req: buildWorkflowsListRequest(buildListWorkflowsSearchParams(input)),
        ctx,
      });
      const result = await responseToMcpToolResult(response, requestId);

      return redactUnlessCallerHolds(authInfo, RESPONSES_READ, result, withoutLastRunError);
    }
  );

  registerMcpTool(
    server,
    "get_workflow",
    {
      title: "Get workflow",
      description:
        "Get one Formbricks workflow using the v3 Workflows API contract. Without Responses: Read in the user's grant, lastRun.error is null: it can quote respondents' answers.",
      inputSchema: ZMcpGetWorkflowInput,
      annotations: {
        readOnlyHint: true,
        destructiveHint: false,
        idempotentHint: true,
        openWorldHint: true,
      },
    },
    [{ permission: "workflows", level: "read" }],
    async (input: TMcpGetWorkflowInput, mcpCtx) => {
      const authInfo = getMcpToolAuthInfo(mcpCtx);
      const requestId = getMcpRequestId(authInfo);
      const ctx = buildWorkflowApiContext(getMcpAuthentication(authInfo), requestId, MCP_API_ROUTE);
      const response = await workflowsHandlers.get({ ctx, params: { workflowId: input.workflowId } });
      const result = await responseToMcpToolResult(response, requestId);

      return redactUnlessCallerHolds(authInfo, RESPONSES_READ, result, withoutLastRunError);
    }
  );

  registerMcpTool(
    server,
    "list_workflow_runs",
    {
      title: "List workflow runs",
      description:
        "List workflow runs for a Formbricks workspace (newest first) using the v3 Workflows API contract. Without Responses: Read in the user's grant, each run's error is null: it can quote respondents' answers.",
      inputSchema: ZMcpListWorkflowRunsInput,
      annotations: {
        readOnlyHint: true,
        destructiveHint: false,
        idempotentHint: true,
        openWorldHint: true,
      },
    },
    [{ permission: "workflowRuns", level: "read" }],
    async (input: TMcpListWorkflowRunsInput, mcpCtx) => {
      const authInfo = getMcpToolAuthInfo(mcpCtx);
      const requestId = getMcpRequestId(authInfo);
      const ctx = buildWorkflowApiContext(getMcpAuthentication(authInfo), requestId, MCP_API_ROUTE);
      const response = await workflowsHandlers.listRuns({
        req: buildWorkflowsListRequest(buildListWorkflowRunsSearchParams(input)),
        ctx,
      });
      const result = await responseToMcpToolResult(response, requestId);

      return redactUnlessCallerHolds(authInfo, RESPONSES_READ, result, withoutRunError);
    }
  );

  registerMcpTool(
    server,
    "get_workflow_run",
    {
      title: "Get workflow run",
      description:
        "Get one Formbricks workflow run with its ordered step logs using the v3 Workflows API contract. A run whose content was deleted after the retention period has `contentDeletedAt` set: its status and timing stay, its payload and step data are empty. Without Responses: Read in the user's grant, triggerPayload, data, steps, error and each log's input, output and error are null: they carry respondents' answers.",
      inputSchema: ZMcpGetWorkflowRunInput,
      annotations: {
        readOnlyHint: true,
        destructiveHint: false,
        idempotentHint: true,
        openWorldHint: true,
      },
    },
    // Workflow runs: Read alone, so a client upgraded with `workflows:read` keeps the tool (spec story
    // 40). The fields that carry respondents' answers are left out without Responses: Read.
    [{ permission: "workflowRuns", level: "read" }],
    async (input: TMcpGetWorkflowRunInput, mcpCtx) => {
      const authInfo = getMcpToolAuthInfo(mcpCtx);
      const requestId = getMcpRequestId(authInfo);
      const ctx = buildWorkflowApiContext(getMcpAuthentication(authInfo), requestId, MCP_API_ROUTE);
      const response = await workflowsHandlers.getRun({ ctx, params: { runId: input.runId } });
      const result = await responseToMcpToolResult(response, requestId);

      return redactUnlessCallerHolds(authInfo, RESPONSES_READ, result, withoutRunRespondentData);
    }
  );

  registerMcpTool(
    server,
    "test_workflow",
    {
      title: "Test workflow (dry-run)",
      description: [
        "Dry-run a Formbricks workflow using the v3 Workflows API contract:",
        "validate its live definition would execute, resolve the trigger's survey + ending cards, confirm every literal send_email recipient is an organization member,",
        "and check every send_email survey link points to a link survey in the workspace.",
        "No run is persisted and no side effects occur; the response reports { ok, problems, warnings }, where warnings (such as survey link Embedded Data keys the survey drops) never make ok false.",
      ].join(" "),
      annotations: {
        // Dry-run: validates the definition and resolves its trigger references. Nothing is
        // executed and no run is created, so the handler gates at "read" to match the
        // workflows:read scope declared below.
        readOnlyHint: true,
        destructiveHint: false,
        idempotentHint: true,
        openWorldHint: true,
      },
      inputSchema: ZMcpTestWorkflowInput,
    },
    [{ permission: "workflows", level: "read" }],
    async (input: TMcpTestWorkflowInput, mcpCtx) => {
      const authInfo = getMcpToolAuthInfo(mcpCtx);
      const requestId = getMcpRequestId(authInfo);
      const ctx = buildWorkflowApiContext(getMcpAuthentication(authInfo), requestId, MCP_API_ROUTE);
      const response = await workflowsHandlers.testWorkflow({
        ctx,
        params: { workflowId: input.workflowId },
      });

      return await responseToMcpToolResult(response, requestId);
    }
  );

  registerMcpTool(
    server,
    "create_workflow",
    {
      title: "Create workflow",
      description: "Create a Formbricks workflow (always as a draft) using the v3 Workflows API contract.",
      inputSchema: ZMcpCreateWorkflowInput,
      audit: { action: "created", targetType: "workflow" },
      annotations: {
        readOnlyHint: false,
        destructiveHint: false,
        idempotentHint: false,
        openWorldHint: true,
      },
    },
    [{ permission: "workflows", level: "write" }],
    async (input: TMcpCreateWorkflowInput, mcpCtx) =>
      runWorkflowMutation(mcpCtx, "created", (ctx) =>
        workflowsHandlers.create({ req: buildWorkflowsBodyRequest(input), ctx })
      )
  );

  registerMcpTool(
    server,
    "patch_workflow",
    {
      title: "Patch workflow",
      description: [
        "Update a Formbricks workflow using the v3 Workflows API patch contract.",
        "Provided top-level fields replace that whole subtree; definition edits are only accepted while draft or disabled.",
      ].join(" "),
      inputSchema: ZMcpPatchWorkflowInput,
      audit: { action: "updated", targetType: "workflow", targetIdArg: "workflowId" },
      annotations: {
        readOnlyHint: false,
        destructiveHint: true,
        idempotentHint: false,
        openWorldHint: true,
      },
    },
    [{ permission: "workflows", level: "write" }],
    async (input: TMcpPatchWorkflowInput, mcpCtx) =>
      runWorkflowMutation(mcpCtx, "updated", (ctx) =>
        workflowsHandlers.patch({
          req: buildWorkflowsBodyRequest(input.data),
          ctx,
          params: { workflowId: input.workflowId },
        })
      )
  );

  registerMcpTool(
    server,
    "duplicate_workflow",
    {
      title: "Duplicate workflow",
      description:
        "Duplicate a Formbricks workflow as a new draft (empty run + version history) using the v3 Workflows API contract.",
      inputSchema: ZMcpDuplicateWorkflowInput,
      audit: { action: "created", targetType: "workflow" },
      annotations: {
        readOnlyHint: false,
        destructiveHint: false,
        idempotentHint: false,
        openWorldHint: true,
      },
    },
    [{ permission: "workflows", level: "write" }],
    async (input: TMcpDuplicateWorkflowInput, mcpCtx) =>
      runWorkflowMutation(mcpCtx, "created", (ctx) =>
        workflowsHandlers.duplicate({
          req: buildWorkflowsBodyRequest(input.name ? { name: input.name } : {}),
          ctx,
          params: { workflowId: input.workflowId },
        })
      )
  );

  registerMcpTool(
    server,
    "delete_workflow",
    {
      title: "Delete workflow",
      description: "Delete a Formbricks workflow using the v3 Workflows API contract.",
      inputSchema: ZMcpWorkflowIdInput,
      audit: { action: "deleted", targetType: "workflow", targetIdArg: "workflowId" },
      annotations: {
        readOnlyHint: false,
        destructiveHint: true,
        idempotentHint: false,
        openWorldHint: true,
      },
    },
    [{ permission: "workflows", level: "write" }],
    async (input: TMcpWorkflowIdInput, mcpCtx) =>
      runWorkflowMutation(mcpCtx, "deleted", (ctx) =>
        workflowsHandlers.delete({ ctx, params: { workflowId: input.workflowId } })
      )
  );

  registerMcpTool(
    server,
    "enable_workflow",
    {
      title: "Enable workflow",
      description: [
        "Enable a Formbricks workflow using the v3 Workflows API contract:",
        "validate executability, snapshot an immutable version, and make it live.",
        "Once live it runs on matching survey responses and can send emails.",
      ].join(" "),
      inputSchema: ZMcpWorkflowIdInput,
      audit: { action: "updated", targetType: "workflow", targetIdArg: "workflowId" },
      annotations: {
        readOnlyHint: false,
        // Enabling activates a live, email-sending workflow — high impact.
        destructiveHint: true,
        idempotentHint: false,
        openWorldHint: true,
      },
    },
    [{ permission: "workflows", level: "write" }],
    async (input: TMcpWorkflowIdInput, mcpCtx) =>
      runWorkflowMutation(mcpCtx, "updated", (ctx) =>
        workflowsHandlers.enable({ ctx, params: { workflowId: input.workflowId } })
      )
  );

  registerMcpTool(
    server,
    "disable_workflow",
    {
      title: "Disable workflow",
      description:
        "Disable a live Formbricks workflow (stops future runs) using the v3 Workflows API contract.",
      inputSchema: ZMcpWorkflowIdInput,
      audit: { action: "updated", targetType: "workflow", targetIdArg: "workflowId" },
      annotations: {
        readOnlyHint: false,
        destructiveHint: false,
        idempotentHint: false,
        openWorldHint: true,
      },
    },
    [{ permission: "workflows", level: "write" }],
    async (input: TMcpWorkflowIdInput, mcpCtx) =>
      runWorkflowMutation(mcpCtx, "updated", (ctx) =>
        workflowsHandlers.disable({ ctx, params: { workflowId: input.workflowId } })
      )
  );

  registerMcpTool(
    server,
    "archive_workflow",
    {
      title: "Archive workflow",
      description: "Archive a Formbricks workflow using the v3 Workflows API contract.",
      inputSchema: ZMcpWorkflowIdInput,
      audit: { action: "updated", targetType: "workflow", targetIdArg: "workflowId" },
      annotations: {
        readOnlyHint: false,
        // Archiving soft-deletes and excludes the workflow from default reads.
        destructiveHint: true,
        idempotentHint: false,
        openWorldHint: true,
      },
    },
    [{ permission: "workflows", level: "write" }],
    async (input: TMcpWorkflowIdInput, mcpCtx) =>
      runWorkflowMutation(mcpCtx, "updated", (ctx) =>
        workflowsHandlers.archive({ ctx, params: { workflowId: input.workflowId } })
      )
  );

  registerMcpTool(
    server,
    "unarchive_workflow",
    {
      title: "Unarchive workflow",
      description: "Unarchive a Formbricks workflow (back to draft) using the v3 Workflows API contract.",
      inputSchema: ZMcpWorkflowIdInput,
      audit: { action: "updated", targetType: "workflow", targetIdArg: "workflowId" },
      annotations: {
        readOnlyHint: false,
        destructiveHint: false,
        idempotentHint: false,
        openWorldHint: true,
      },
    },
    [{ permission: "workflows", level: "write" }],
    async (input: TMcpWorkflowIdInput, mcpCtx) =>
      runWorkflowMutation(mcpCtx, "updated", (ctx) =>
        workflowsHandlers.unarchive({ ctx, params: { workflowId: input.workflowId } })
      )
  );

  // Part 2 tools (ENG-7171): Secret URL, sample, Send test request, Test run.
  registerWorkflowWebhookTools(server);
  registerWorkflowTestRequestTools(server);
  registerWorkflowTestRunTools(server);
}
