// Organizations Tools

import { CalendlyClient } from '../clients/calendly.js';

export function createOrganizationsTools(client: CalendlyClient) {
  return {
    calendly_get_organization: {
      description: 'Get organization details by UUID',
      parameters: {
        type: 'object',
        properties: {
          uuid: {
            type: 'string',
            description: 'Organization UUID',
          },
        },
        required: ['uuid'],
      },
      handler: async (args: any) => {
        const result = await client.getOrganization(args.uuid);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    calendly_list_organization_members: {
      description: 'List members of an organization',
      parameters: {
        type: 'object',
        properties: {
          organization_uri: {
            type: 'string',
            description: 'Organization URI',
          },
          email: {
            type: 'string',
            description: 'Filter by email address',
          },
          count: {
            type: 'number',
            description: 'Number of results per page',
            default: 20,
          },
          page_token: {
            type: 'string',
            description: 'Pagination token',
          },
        },
        required: ['organization_uri'],
      },
      handler: async (args: any) => {
        const result = await client.listOrganizationMemberships(args.organization_uri, {
          email: args.email,
          count: args.count,
          page_token: args.page_token,
        });
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    calendly_list_organization_invitations: {
      description: 'List pending invitations for an organization',
      parameters: {
        type: 'object',
        properties: {
          organization_uri: {
            type: 'string',
            description: 'Organization URI',
          },
          email: {
            type: 'string',
            description: 'Filter by email address',
          },
          status: {
            type: 'string',
            enum: ['pending', 'accepted', 'declined', 'revoked'],
            description: 'Filter by invitation status',
          },
          count: {
            type: 'number',
            description: 'Number of results per page',
            default: 20,
          },
          page_token: {
            type: 'string',
            description: 'Pagination token',
          },
        },
        required: ['organization_uri'],
      },
      handler: async (args: any) => {
        const result = await client.listOrganizationInvitations(args.organization_uri, {
          email: args.email,
          status: args.status,
          count: args.count,
          page_token: args.page_token,
        });
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    calendly_invite_user: {
      description: 'Invite a user to an organization by email',
      parameters: {
        type: 'object',
        properties: {
          organization_uri: {
            type: 'string',
            description: 'Organization URI',
          },
          email: {
            type: 'string',
            description: 'Email address to invite',
          },
        },
        required: ['organization_uri', 'email'],
      },
      handler: async (args: any) => {
        const result = await client.inviteUserToOrganization(
          args.organization_uri,
          args.email
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    calendly_revoke_invitation: {
      description: 'Revoke a pending organization invitation',
      parameters: {
        type: 'object',
        properties: {
          uuid: {
            type: 'string',
            description: 'Invitation UUID to revoke',
          },
        },
        required: ['uuid'],
      },
      handler: async (args: any) => {
        const result = await client.revokeOrganizationInvitation(args.uuid);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    calendly_remove_organization_member: {
      description: 'Remove a member from an organization',
      parameters: {
        type: 'object',
        properties: {
          uuid: {
            type: 'string',
            description: 'Organization membership UUID to remove',
          },
        },
        required: ['uuid'],
      },
      handler: async (args: any) => {
        await client.removeOrganizationMembership(args.uuid);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify({ success: true, message: 'Member removed' }),
            },
          ],
        };
      },
    },
  };
}
