import { CloverClient } from '../clients/clover.js';
import { CloverCategory, PaginatedResponse } from '../types/index.js';

export function createCategoriesTools(client: CloverClient) {
  return {
    clover_list_categories: {
      description: 'List all inventory categories',
      inputSchema: {
        type: 'object',
        properties: {
          expand: {
            type: 'string',
            description: 'Comma-separated fields to expand (e.g., "items")',
          },
          limit: {
            type: 'number',
            description: 'Maximum number of categories to return',
          },
        },
      },
      handler: async (args: any) => {
        const categories = await client.fetchPaginated<CloverCategory>(
          '/categories',
          { expand: args.expand },
          args.limit
        );
        return { categories, count: categories.length };
      },
    },

    clover_get_category: {
      description: 'Get a specific category by ID',
      inputSchema: {
        type: 'object',
        properties: {
          categoryId: { type: 'string', description: 'Category ID' },
          expand: {
            type: 'string',
            description: 'Comma-separated fields to expand',
          },
        },
        required: ['categoryId'],
      },
      handler: async (args: any) => {
        return await client.get<CloverCategory>(`/categories/${args.categoryId}`, {
          expand: args.expand,
        });
      },
    },

    clover_create_category: {
      description: 'Create a new inventory category',
      inputSchema: {
        type: 'object',
        properties: {
          name: { type: 'string', description: 'Category name' },
          sortOrder: {
            type: 'number',
            description: 'Sort order (lower numbers appear first)',
          },
        },
        required: ['name'],
      },
      handler: async (args: any) => {
        return await client.post<CloverCategory>('/categories', {
          name: args.name,
          sortOrder: args.sortOrder,
        });
      },
    },

    clover_update_category: {
      description: 'Update an existing category',
      inputSchema: {
        type: 'object',
        properties: {
          categoryId: { type: 'string', description: 'Category ID' },
          name: { type: 'string', description: 'Category name' },
          sortOrder: { type: 'number', description: 'Sort order' },
        },
        required: ['categoryId'],
      },
      handler: async (args: any) => {
        const { categoryId, ...updateData } = args;
        return await client.post<CloverCategory>(`/categories/${categoryId}`, updateData);
      },
    },

    clover_delete_category: {
      description: 'Delete a category',
      inputSchema: {
        type: 'object',
        properties: {
          categoryId: { type: 'string', description: 'Category ID' },
        },
        required: ['categoryId'],
      },
      handler: async (args: any) => {
        await client.delete(`/categories/${args.categoryId}`);
        return { success: true, categoryId: args.categoryId };
      },
    },

    clover_add_item_to_category: {
      description: 'Add an item to a category',
      inputSchema: {
        type: 'object',
        properties: {
          categoryId: { type: 'string', description: 'Category ID' },
          itemId: { type: 'string', description: 'Item ID' },
        },
        required: ['categoryId', 'itemId'],
      },
      handler: async (args: any) => {
        return await client.post(
          `/categories/${args.categoryId}/items`,
          { id: args.itemId }
        );
      },
    },

    clover_remove_item_from_category: {
      description: 'Remove an item from a category',
      inputSchema: {
        type: 'object',
        properties: {
          categoryId: { type: 'string', description: 'Category ID' },
          itemId: { type: 'string', description: 'Item ID' },
        },
        required: ['categoryId', 'itemId'],
      },
      handler: async (args: any) => {
        await client.delete(`/categories/${args.categoryId}/items/${args.itemId}`);
        return { success: true, itemId: args.itemId };
      },
    },

    clover_list_category_items: {
      description: 'List all items in a category',
      inputSchema: {
        type: 'object',
        properties: {
          categoryId: { type: 'string', description: 'Category ID' },
          expand: { type: 'string', description: 'Comma-separated fields to expand' },
        },
        required: ['categoryId'],
      },
      handler: async (args: any) => {
        return await client.get<PaginatedResponse<any>>(
          `/categories/${args.categoryId}/items`,
          { expand: args.expand }
        );
      },
    },
  };
}
