/**
 * Team Tools for Monday.com MCP Server
 * Tools for managing teams: list, get
 */

import { z } from "zod";
import { Tool } from "@modelcontextprotocol/sdk/types.js";
import { MondayClient } from "../clients/monday.js";

// Zod Schemas
const ListTeamsSchema = z.object({
  limit: z.number().min(1).max(100).optional().describe("Number of teams to return"),
  page: z.number().min(1).optional().describe("Page number for pagination"),
});

const GetTeamSchema = z.object({
  team_id: z.string().describe("Team ID"),
});

/**
 * Get all team tools
 */
export function getTools(_client: MondayClient): Tool[] {
  return [
    {
      name: "monday_list_teams",
      description: "List all teams in the account with their members.",
      inputSchema: {
        type: "object",
        properties: {
          limit: { type: "number", description: "Number of teams to return (default: 50, max: 100)" },
          page: { type: "number", description: "Page number for pagination" },
        },
      },
    },
    {
      name: "monday_get_team",
      description: "Get a single team by ID with full member details.",
      inputSchema: {
        type: "object",
        properties: {
          team_id: { type: "string", description: "Team ID" },
        },
        required: ["team_id"],
      },
    },
  ];
}

/**
 * Execute team tool
 */
export async function executeTeamTool(
  client: MondayClient,
  toolName: string,
  args: any
): Promise<any> {
  switch (toolName) {
    case "monday_list_teams": {
      const params = ListTeamsSchema.parse(args);
      let queryArgs: string[] = [];
      
      if (params.limit !== undefined) queryArgs.push(`limit: ${params.limit}`);
      if (params.page !== undefined) queryArgs.push(`page: ${params.page}`);

      const argsStr = queryArgs.length > 0 ? `(${queryArgs.join(", ")})` : "";

      const query = `
        query {
          teams${argsStr} {
            id
            name
            picture_url
            users {
              id
              name
              email
              photo_thumb
            }
          }
        }
      `;
      const result = await (client as any).query(query);
      return result.data.teams || [];
    }

    case "monday_get_team": {
      const params = GetTeamSchema.parse(args);
      const query = `
        query {
          teams(ids: [${params.team_id}]) {
            id
            name
            picture_url
            users {
              id
              name
              email
              url
              photo_thumb
              photo_original
              is_guest
              enabled
              title
              phone
              location
            }
          }
        }
      `;
      const result = await (client as any).query(query);
      if (!result.data.teams || result.data.teams.length === 0) {
        throw new Error(`Team ${params.team_id} not found`);
      }
      return result.data.teams[0];
    }

    default:
      throw new Error(`Unknown team tool: ${toolName}`);
  }
}
