/**
 * VIRTUAL_MCP_PINNED_VIEWS_UPDATE Tool
 *
 * Update the pinned views for a virtual MCP's sidebar
 */

import { z } from "zod";
import { defineTool } from "../../core/define-tool";
import {
  getUserId,
  requireAuth,
  requireOrganization,
} from "../../core/studio-context";
import { VirtualMCPEntitySchema } from "./schema";

const pinnedViewSchema = z.object({
  connectionId: z.string(),
  toolName: z.string(),
  label: z.string(),
  icon: z.string().nullable().optional(),
});

export const VIRTUAL_MCP_PINNED_VIEWS_UPDATE = defineTool({
  name: "VIRTUAL_MCP_PINNED_VIEWS_UPDATE" as const,
  description:
    "Update the pinned sidebar views for a virtual MCP. Replaces all current pins.",
  annotations: {
    title: "Update Pinned Views",
    readOnlyHint: false,
    destructiveHint: true,
    idempotentHint: true,
    openWorldHint: false,
  },
  inputSchema: z.object({
    virtualMcpId: z.string().describe("Virtual MCP ID"),
    pinnedViews: z
      .array(pinnedViewSchema)
      .describe("Pinned views to set for the virtual MCP sidebar"),
    layout: z
      .object({
        defaultMainView: z
          .object({
            type: z.string(),
            id: z.string().optional(),
            toolName: z.string().optional(),
          })
          .nullable()
          .optional(),
        chatDefaultOpen: z.boolean().nullable().optional(),
      })
      .optional(),
  }),

  outputSchema: z.object({
    item: VirtualMCPEntitySchema.nullable(),
  }),

  handler: async (input, ctx) => {
    requireAuth(ctx);
    const organization = requireOrganization(ctx);
    await ctx.access.check();

    const { virtualMcpId, pinnedViews, layout } = input;
    const userId = getUserId(ctx);

    const virtualMcp = await ctx.storage.virtualMcps.findById(virtualMcpId);
    if (!virtualMcp) {
      throw new Error(`Virtual MCP not found: ${virtualMcpId}`);
    }
    if (virtualMcp.organization_id !== organization.id) {
      throw new Error(`Virtual MCP not found: ${virtualMcpId}`);
    }

    const currentUI =
      (virtualMcp.metadata?.ui as Record<string, unknown>) ?? {};

    const updatedUI = {
      ...currentUI,
      pinnedViews: pinnedViews.length > 0 ? pinnedViews : null,
      layout: layout ?? currentUI.layout ?? null,
    };

    const updated = await ctx.storage.virtualMcps.update(
      virtualMcpId,
      userId ?? "system",
      {
        metadata: { ...virtualMcp.metadata, ui: updatedUI },
      },
    );

    return {
      item: updated,
    };
  },
});
