import { CloverClient } from '../clients/clover.js';
import { CloverLineItem } from '../types/index.js';

export function createLineItemsTools(client: CloverClient) {
  return {
    clover_get_line_item: {
      description: 'Get a specific line item from an order',
      inputSchema: {
        type: 'object',
        properties: {
          orderId: { type: 'string', description: 'Order ID' },
          lineItemId: { type: 'string', description: 'Line item ID' },
          expand: { type: 'string', description: 'Comma-separated fields to expand' },
        },
        required: ['orderId', 'lineItemId'],
      },
      handler: async (args: any) => {
        return await client.get<CloverLineItem>(
          `/orders/${args.orderId}/line_items/${args.lineItemId}`,
          { expand: args.expand }
        );
      },
    },

    clover_update_line_item: {
      description: 'Update a line item in an order',
      inputSchema: {
        type: 'object',
        properties: {
          orderId: { type: 'string', description: 'Order ID' },
          lineItemId: { type: 'string', description: 'Line item ID' },
          name: { type: 'string', description: 'Line item name' },
          price: { type: 'number', description: 'Price in cents' },
          unitQty: { type: 'number', description: 'Unit quantity' },
          note: { type: 'string', description: 'Line item note' },
        },
        required: ['orderId', 'lineItemId'],
      },
      handler: async (args: any) => {
        const { orderId, lineItemId, ...updateData } = args;
        return await client.post<CloverLineItem>(
          `/orders/${orderId}/line_items/${lineItemId}`,
          updateData
        );
      },
    },

    clover_exchange_line_item: {
      description: 'Exchange a line item (mark as exchanged)',
      inputSchema: {
        type: 'object',
        properties: {
          orderId: { type: 'string', description: 'Order ID' },
          lineItemId: { type: 'string', description: 'Line item ID' },
        },
        required: ['orderId', 'lineItemId'],
      },
      handler: async (args: any) => {
        return await client.post(
          `/orders/${args.orderId}/line_items/${args.lineItemId}/exchange`,
          {}
        );
      },
    },

    clover_add_line_item_modification: {
      description: 'Add a modification to a line item',
      inputSchema: {
        type: 'object',
        properties: {
          orderId: { type: 'string', description: 'Order ID' },
          lineItemId: { type: 'string', description: 'Line item ID' },
          modifierId: { type: 'string', description: 'Modifier ID' },
          name: { type: 'string', description: 'Modification name (optional override)' },
          amount: { type: 'number', description: 'Amount in cents (optional override)' },
        },
        required: ['orderId', 'lineItemId', 'modifierId'],
      },
      handler: async (args: any) => {
        return await client.post(
          `/orders/${args.orderId}/line_items/${args.lineItemId}/modifications`,
          {
            modifier: { id: args.modifierId },
            name: args.name,
            amount: args.amount,
          }
        );
      },
    },

    clover_remove_line_item_modification: {
      description: 'Remove a modification from a line item',
      inputSchema: {
        type: 'object',
        properties: {
          orderId: { type: 'string', description: 'Order ID' },
          lineItemId: { type: 'string', description: 'Line item ID' },
          modificationId: { type: 'string', description: 'Modification ID' },
        },
        required: ['orderId', 'lineItemId', 'modificationId'],
      },
      handler: async (args: any) => {
        await client.delete(
          `/orders/${args.orderId}/line_items/${args.lineItemId}/modifications/${args.modificationId}`
        );
        return { success: true, modificationId: args.modificationId };
      },
    },

    clover_add_line_item_discount: {
      description: 'Add a discount to a line item',
      inputSchema: {
        type: 'object',
        properties: {
          orderId: { type: 'string', description: 'Order ID' },
          lineItemId: { type: 'string', description: 'Line item ID' },
          discountId: { type: 'string', description: 'Discount ID' },
        },
        required: ['orderId', 'lineItemId', 'discountId'],
      },
      handler: async (args: any) => {
        return await client.post(
          `/orders/${args.orderId}/line_items/${args.lineItemId}/discounts`,
          { discount: { id: args.discountId } }
        );
      },
    },

    clover_remove_line_item_discount: {
      description: 'Remove a discount from a line item',
      inputSchema: {
        type: 'object',
        properties: {
          orderId: { type: 'string', description: 'Order ID' },
          lineItemId: { type: 'string', description: 'Line item ID' },
          discountId: { type: 'string', description: 'Discount ID' },
        },
        required: ['orderId', 'lineItemId', 'discountId'],
      },
      handler: async (args: any) => {
        await client.delete(
          `/orders/${args.orderId}/line_items/${args.lineItemId}/discounts/${args.discountId}`
        );
        return { success: true, discountId: args.discountId };
      },
    },
  };
}
