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

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

const GetFulfillmentInput = z.object({
  order_id: z.string().describe('Order ID'),
  fulfillment_id: z.string().describe('Fulfillment ID'),
});

const CreateFulfillmentInput = z.object({
  order_id: z.string().describe('Order ID'),
  location_id: z.string().optional().describe('Location ID for fulfillment'),
  tracking_number: z.string().optional().describe('Tracking number'),
  tracking_company: z.string().optional().describe('Tracking company/carrier'),
  tracking_url: z.string().optional().describe('Tracking URL'),
  notify_customer: z.boolean().optional().describe('Send fulfillment notification to customer'),
  line_items: z.array(z.object({
    id: z.string().describe('Line item ID'),
    quantity: z.number().optional().describe('Quantity to fulfill'),
  })).optional().describe('Line items to fulfill'),
});

const UpdateFulfillmentInput = z.object({
  order_id: z.string().describe('Order ID'),
  fulfillment_id: z.string().describe('Fulfillment ID'),
  tracking_number: z.string().optional().describe('Tracking number'),
  tracking_company: z.string().optional().describe('Tracking company/carrier'),
  tracking_url: z.string().optional().describe('Tracking URL'),
  notify_customer: z.boolean().optional().describe('Send notification to customer'),
});

const CancelFulfillmentInput = z.object({
  order_id: z.string().describe('Order ID'),
  fulfillment_id: z.string().describe('Fulfillment ID'),
});

const ListFulfillmentOrdersInput = z.object({
  order_id: z.string().describe('Order ID'),
});

export default [
    {
      name: 'shopify_list_fulfillments',
      description: 'List fulfillments for a specific order',
      inputSchema: {
        type: 'object' as const,
        properties: {
          order_id: { type: 'string', description: 'Order ID' },
          limit: { type: 'number', description: 'Results per page', default: 50, minimum: 1, maximum: 250 },
          page_info: { type: 'string', description: 'Cursor for pagination' },
        },
        required: ['order_id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = ListFulfillmentsInput.parse(input);
        const { order_id, ...params } = validated;
        const results = await client.list(`/orders/${order_id}/fulfillments.json`, { params });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_get_fulfillment',
      description: 'Get a specific fulfillment by ID',
      inputSchema: {
        type: 'object' as const,
        properties: {
          order_id: { type: 'string', description: 'Order ID' },
          fulfillment_id: { type: 'string', description: 'Fulfillment ID' },
        },
        required: ['order_id', 'fulfillment_id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = GetFulfillmentInput.parse(input);
        const result = await client.get(`/orders/${validated.order_id}/fulfillments/${validated.fulfillment_id}.json`);
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_create_fulfillment',
      description: 'Create a new fulfillment for an order with tracking information',
      inputSchema: {
        type: 'object' as const,
        properties: {
          order_id: { type: 'string', description: 'Order ID' },
          location_id: { type: 'string', description: 'Location ID for fulfillment' },
          tracking_number: { type: 'string', description: 'Tracking number' },
          tracking_company: { type: 'string', description: 'Tracking company/carrier' },
          tracking_url: { type: 'string', description: 'Tracking URL' },
          notify_customer: { type: 'boolean', description: 'Send fulfillment notification to customer' },
          line_items: {
            type: 'array',
            description: 'Line items to fulfill',
            items: {
              type: 'object',
              properties: {
                id: { type: 'string', description: 'Line item ID' },
                quantity: { type: 'number', description: 'Quantity to fulfill' },
              },
            },
          },
        },
        required: ['order_id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = CreateFulfillmentInput.parse(input);
        const { order_id, ...fulfillmentData } = validated;
        const result = await client.create(`/orders/${order_id}/fulfillments.json`, { fulfillment: fulfillmentData });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_update_fulfillment',
      description: 'Update a fulfillment (e.g., update tracking information)',
      inputSchema: {
        type: 'object' as const,
        properties: {
          order_id: { type: 'string', description: 'Order ID' },
          fulfillment_id: { type: 'string', description: 'Fulfillment ID' },
          tracking_number: { type: 'string', description: 'Tracking number' },
          tracking_company: { type: 'string', description: 'Tracking company/carrier' },
          tracking_url: { type: 'string', description: 'Tracking URL' },
          notify_customer: { type: 'boolean', description: 'Send notification to customer' },
        },
        required: ['order_id', 'fulfillment_id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = UpdateFulfillmentInput.parse(input);
        const { order_id, fulfillment_id, ...updates } = validated;
        const result = await client.update(`/orders/${order_id}/fulfillments/${fulfillment_id}.json`, { fulfillment: updates });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_cancel_fulfillment',
      description: 'Cancel a fulfillment',
      inputSchema: {
        type: 'object' as const,
        properties: {
          order_id: { type: 'string', description: 'Order ID' },
          fulfillment_id: { type: 'string', description: 'Fulfillment ID' },
        },
        required: ['order_id', 'fulfillment_id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = CancelFulfillmentInput.parse(input);
        const result = await client.create(`/orders/${validated.order_id}/fulfillments/${validated.fulfillment_id}/cancel.json`, {});
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_list_fulfillment_orders',
      description: 'List fulfillment orders for a specific order',
      inputSchema: {
        type: 'object' as const,
        properties: {
          order_id: { type: 'string', description: 'Order ID' },
        },
        required: ['order_id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = ListFulfillmentOrdersInput.parse(input);
        const results = await client.list(`/orders/${validated.order_id}/fulfillment_orders.json`);
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }],
        };
      },
    },
  ];
