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

export function createInventoryTools(client: CloverClient) {
  return {
    clover_list_items: {
      description: 'List inventory items',
      inputSchema: {
        type: 'object',
        properties: {
          filter: {
            type: 'string',
            description: 'Filter expression (e.g., "hidden=false")',
          },
          expand: {
            type: 'string',
            description: 'Comma-separated fields to expand (categories, tags, modifierGroups)',
          },
          limit: {
            type: 'number',
            description: 'Maximum number of items to return',
          },
        },
      },
      handler: async (args: any) => {
        const items = await client.fetchPaginated<CloverItem>(
          '/items',
          { filter: args.filter, expand: args.expand },
          args.limit
        );
        return { items, count: items.length };
      },
    },

    clover_get_item: {
      description: 'Get a specific inventory item by ID',
      inputSchema: {
        type: 'object',
        properties: {
          itemId: { type: 'string', description: 'Item ID' },
          expand: {
            type: 'string',
            description: 'Comma-separated fields to expand',
          },
        },
        required: ['itemId'],
      },
      handler: async (args: any) => {
        return await client.get<CloverItem>(`/items/${args.itemId}`, {
          expand: args.expand,
        });
      },
    },

    clover_create_item: {
      description: 'Create a new inventory item',
      inputSchema: {
        type: 'object',
        properties: {
          name: { type: 'string', description: 'Item name' },
          price: { type: 'number', description: 'Price in cents' },
          priceType: {
            type: 'string',
            enum: ['FIXED', 'PER_UNIT', 'VARIABLE'],
            description: 'Price type (default: FIXED)',
          },
          sku: { type: 'string', description: 'SKU code' },
          code: { type: 'string', description: 'Item code' },
          cost: { type: 'number', description: 'Cost in cents' },
          isRevenue: {
            type: 'boolean',
            description: 'Is this a revenue item (default: true)',
          },
          stockCount: { type: 'number', description: 'Initial stock count' },
        },
        required: ['name', 'price'],
      },
      handler: async (args: any) => {
        return await client.post<CloverItem>('/items', {
          name: args.name,
          price: args.price,
          priceType: args.priceType || 'FIXED',
          sku: args.sku,
          code: args.code,
          cost: args.cost,
          isRevenue: args.isRevenue !== false,
          stockCount: args.stockCount,
        });
      },
    },

    clover_update_item: {
      description: 'Update an existing inventory item',
      inputSchema: {
        type: 'object',
        properties: {
          itemId: { type: 'string', description: 'Item ID' },
          name: { type: 'string', description: 'Item name' },
          price: { type: 'number', description: 'Price in cents' },
          sku: { type: 'string', description: 'SKU code' },
          code: { type: 'string', description: 'Item code' },
          cost: { type: 'number', description: 'Cost in cents' },
          hidden: { type: 'boolean', description: 'Hide item from menus' },
        },
        required: ['itemId'],
      },
      handler: async (args: any) => {
        const { itemId, ...updateData } = args;
        return await client.post<CloverItem>(`/items/${itemId}`, updateData);
      },
    },

    clover_delete_item: {
      description: 'Delete an inventory item',
      inputSchema: {
        type: 'object',
        properties: {
          itemId: { type: 'string', description: 'Item ID' },
        },
        required: ['itemId'],
      },
      handler: async (args: any) => {
        await client.delete(`/items/${args.itemId}`);
        return { success: true, itemId: args.itemId };
      },
    },

    clover_list_categories: {
      description: 'List inventory categories',
      inputSchema: {
        type: 'object',
        properties: {
          expand: {
            type: 'string',
            description: 'Comma-separated fields to expand (items)',
          },
        },
      },
      handler: async (args: any) => {
        const categories = await client.fetchPaginated<CloverCategory>(
          '/categories',
          { expand: args.expand }
        );
        return { categories, count: categories.length };
      },
    },

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

    clover_list_modifier_groups: {
      description: 'List modifier groups',
      inputSchema: {
        type: 'object',
        properties: {
          expand: {
            type: 'string',
            description: 'Comma-separated fields to expand (modifiers)',
          },
        },
      },
      handler: async (args: any) => {
        const groups = await client.fetchPaginated<CloverModifierGroup>(
          '/modifier_groups',
          { expand: args.expand }
        );
        return { modifierGroups: groups, count: groups.length };
      },
    },

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

    clover_list_item_stocks: {
      description: 'List stock levels for all items',
      inputSchema: {
        type: 'object',
        properties: {
          itemId: {
            type: 'string',
            description: 'Optional: filter by specific item ID',
          },
        },
      },
      handler: async (args: any) => {
        const endpoint = args.itemId
          ? `/items/${args.itemId}/stock`
          : '/item_stocks';
        return await client.get(endpoint);
      },
    },

    clover_update_item_stock: {
      description: 'Update stock count for an item',
      inputSchema: {
        type: 'object',
        properties: {
          itemId: { type: 'string', description: 'Item ID' },
          quantity: { type: 'number', description: 'New stock quantity' },
        },
        required: ['itemId', 'quantity'],
      },
      handler: async (args: any) => {
        return await client.post(`/items/${args.itemId}/stock`, {
          quantity: args.quantity,
        });
      },
    },
  };
}
