import { CloverClient } from '../clients/clover.js';
import { CloverModifier, CloverModifierGroup } from '../types/index.js';

export function createModifiersTools(client: CloverClient) {
  return {
    clover_list_modifier_groups: {
      description: 'List all modifier groups',
      inputSchema: {
        type: 'object',
        properties: {
          expand: {
            type: 'string',
            description: 'Comma-separated fields to expand (e.g., "modifiers")',
          },
          limit: {
            type: 'number',
            description: 'Maximum number of groups to return',
          },
        },
      },
      handler: async (args: any) => {
        const groups = await client.fetchPaginated<CloverModifierGroup>(
          '/modifier_groups',
          { expand: args.expand },
          args.limit
        );
        return { modifierGroups: groups, count: groups.length };
      },
    },

    clover_get_modifier_group: {
      description: 'Get a specific modifier group',
      inputSchema: {
        type: 'object',
        properties: {
          groupId: { type: 'string', description: 'Modifier group ID' },
          expand: { type: 'string', description: 'Comma-separated fields to expand' },
        },
        required: ['groupId'],
      },
      handler: async (args: any) => {
        return await client.get<CloverModifierGroup>(
          `/modifier_groups/${args.groupId}`,
          { expand: args.expand }
        );
      },
    },

    clover_create_modifier_group: {
      description: 'Create a new modifier group',
      inputSchema: {
        type: 'object',
        properties: {
          name: { type: 'string', description: 'Modifier group name' },
          showByDefault: {
            type: 'boolean',
            description: 'Whether to show this group by default',
          },
        },
        required: ['name'],
      },
      handler: async (args: any) => {
        return await client.post<CloverModifierGroup>('/modifier_groups', {
          name: args.name,
          showByDefault: args.showByDefault,
        });
      },
    },

    clover_update_modifier_group: {
      description: 'Update an existing modifier group',
      inputSchema: {
        type: 'object',
        properties: {
          groupId: { type: 'string', description: 'Modifier group ID' },
          name: { type: 'string', description: 'Modifier group name' },
          showByDefault: { type: 'boolean', description: 'Show by default flag' },
        },
        required: ['groupId'],
      },
      handler: async (args: any) => {
        const { groupId, ...updateData } = args;
        return await client.post<CloverModifierGroup>(
          `/modifier_groups/${groupId}`,
          updateData
        );
      },
    },

    clover_delete_modifier_group: {
      description: 'Delete a modifier group',
      inputSchema: {
        type: 'object',
        properties: {
          groupId: { type: 'string', description: 'Modifier group ID' },
        },
        required: ['groupId'],
      },
      handler: async (args: any) => {
        await client.delete(`/modifier_groups/${args.groupId}`);
        return { success: true, groupId: args.groupId };
      },
    },

    clover_list_modifiers: {
      description: 'List all modifiers in a group',
      inputSchema: {
        type: 'object',
        properties: {
          groupId: { type: 'string', description: 'Modifier group ID' },
          limit: { type: 'number', description: 'Maximum number to return' },
        },
        required: ['groupId'],
      },
      handler: async (args: any) => {
        const modifiers = await client.fetchPaginated<CloverModifier>(
          `/modifier_groups/${args.groupId}/modifiers`,
          {},
          args.limit
        );
        return { modifiers, count: modifiers.length };
      },
    },

    clover_get_modifier: {
      description: 'Get a specific modifier',
      inputSchema: {
        type: 'object',
        properties: {
          groupId: { type: 'string', description: 'Modifier group ID' },
          modifierId: { type: 'string', description: 'Modifier ID' },
        },
        required: ['groupId', 'modifierId'],
      },
      handler: async (args: any) => {
        return await client.get<CloverModifier>(
          `/modifier_groups/${args.groupId}/modifiers/${args.modifierId}`
        );
      },
    },

    clover_create_modifier: {
      description: 'Create a new modifier in a group',
      inputSchema: {
        type: 'object',
        properties: {
          groupId: { type: 'string', description: 'Modifier group ID' },
          name: { type: 'string', description: 'Modifier name' },
          price: { type: 'number', description: 'Modifier price in cents' },
        },
        required: ['groupId', 'name', 'price'],
      },
      handler: async (args: any) => {
        return await client.post<CloverModifier>(
          `/modifier_groups/${args.groupId}/modifiers`,
          {
            name: args.name,
            price: args.price,
          }
        );
      },
    },

    clover_update_modifier: {
      description: 'Update an existing modifier',
      inputSchema: {
        type: 'object',
        properties: {
          groupId: { type: 'string', description: 'Modifier group ID' },
          modifierId: { type: 'string', description: 'Modifier ID' },
          name: { type: 'string', description: 'Modifier name' },
          price: { type: 'number', description: 'Modifier price in cents' },
        },
        required: ['groupId', 'modifierId'],
      },
      handler: async (args: any) => {
        const { groupId, modifierId, ...updateData } = args;
        return await client.post<CloverModifier>(
          `/modifier_groups/${groupId}/modifiers/${modifierId}`,
          updateData
        );
      },
    },

    clover_delete_modifier: {
      description: 'Delete a modifier',
      inputSchema: {
        type: 'object',
        properties: {
          groupId: { type: 'string', description: 'Modifier group ID' },
          modifierId: { type: 'string', description: 'Modifier ID' },
        },
        required: ['groupId', 'modifierId'],
      },
      handler: async (args: any) => {
        await client.delete(
          `/modifier_groups/${args.groupId}/modifiers/${args.modifierId}`
        );
        return { success: true, modifierId: args.modifierId };
      },
    },
  };
}
