import { z } from 'zod';
import { ShopifyClient } from '../clients/shopify.js';

const ListInventoryItemsInput = z.object({
  ids: z.string().describe('Comma-separated list of inventory item IDs'),
  limit: z.number().min(1).max(250).default(50).describe('Results per page'),
  page_info: z.string().optional().describe('Cursor for pagination'),
});

const GetInventoryItemInput = z.object({
  id: z.string().describe('Inventory item ID'),
});

const ListInventoryLevelsInput = z.object({
  inventory_item_ids: z.string().optional().describe('Comma-separated inventory item IDs'),
  location_ids: z.string().optional().describe('Comma-separated location IDs'),
  limit: z.number().min(1).max(250).default(50).describe('Results per page'),
  page_info: z.string().optional().describe('Cursor for pagination'),
});

const SetInventoryLevelInput = z.object({
  inventory_item_id: z.string().describe('Inventory item ID'),
  location_id: z.string().describe('Location ID'),
  available: z.number().describe('New available inventory quantity'),
  disconnect_if_necessary: z.boolean().optional().describe('Disconnect from location if necessary'),
});

const AdjustInventoryLevelInput = z.object({
  inventory_item_id: z.string().describe('Inventory item ID'),
  location_id: z.string().describe('Location ID'),
  available_adjustment: z.number().describe('Inventory adjustment (positive or negative)'),
});

const ListLocationsInput = z.object({
  limit: z.number().min(1).max(250).default(50).describe('Results per page'),
  page_info: z.string().optional().describe('Cursor for pagination'),
});

export default [
    {
      name: 'shopify_list_inventory_items',
      description: 'List inventory items by IDs',
      inputSchema: {
        type: 'object' as const,
        properties: {
          ids: { type: 'string', description: 'Comma-separated list of inventory item IDs' },
          limit: { type: 'number', description: 'Results per page', default: 50, minimum: 1, maximum: 250 },
          page_info: { type: 'string', description: 'Cursor for pagination' },
        },
        required: ['ids'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = ListInventoryItemsInput.parse(input);
        const results = await client.list('/inventory_items.json', { params: validated });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_get_inventory_item',
      description: 'Get a specific inventory item by ID',
      inputSchema: {
        type: 'object' as const,
        properties: {
          id: { type: 'string', description: 'Inventory item ID' },
        },
        required: ['id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = GetInventoryItemInput.parse(input);
        const result = await client.get(`/inventory_items/${validated.id}.json`);
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_list_inventory_levels',
      description: 'List inventory levels filtered by inventory items or locations',
      inputSchema: {
        type: 'object' as const,
        properties: {
          inventory_item_ids: { type: 'string', description: 'Comma-separated inventory item IDs' },
          location_ids: { type: 'string', description: 'Comma-separated location IDs' },
          limit: { type: 'number', description: 'Results per page', default: 50, minimum: 1, maximum: 250 },
          page_info: { type: 'string', description: 'Cursor for pagination' },
        },
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = ListInventoryLevelsInput.parse(input);
        const results = await client.list('/inventory_levels.json', { params: validated });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_set_inventory_level',
      description: 'Set the inventory level for an item at a specific location (overwrites current value)',
      inputSchema: {
        type: 'object' as const,
        properties: {
          inventory_item_id: { type: 'string', description: 'Inventory item ID' },
          location_id: { type: 'string', description: 'Location ID' },
          available: { type: 'number', description: 'New available inventory quantity' },
          disconnect_if_necessary: { type: 'boolean', description: 'Disconnect from location if necessary' },
        },
        required: ['inventory_item_id', 'location_id', 'available'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = SetInventoryLevelInput.parse(input);
        const result = await client.create('/inventory_levels/set.json', validated);
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_adjust_inventory_level',
      description: 'Adjust inventory level by a delta (positive or negative)',
      inputSchema: {
        type: 'object' as const,
        properties: {
          inventory_item_id: { type: 'string', description: 'Inventory item ID' },
          location_id: { type: 'string', description: 'Location ID' },
          available_adjustment: { type: 'number', description: 'Inventory adjustment (positive or negative)' },
        },
        required: ['inventory_item_id', 'location_id', 'available_adjustment'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = AdjustInventoryLevelInput.parse(input);
        const result = await client.create('/inventory_levels/adjust.json', validated);
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_list_locations',
      description: 'List all inventory locations',
      inputSchema: {
        type: 'object' as const,
        properties: {
          limit: { type: 'number', description: 'Results per page', default: 50, minimum: 1, maximum: 250 },
          page_info: { type: 'string', description: 'Cursor for pagination' },
        },
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = ListLocationsInput.parse(input);
        const results = await client.list('/locations.json', { params: validated });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }],
        };
      },
    },
  ];
