import type { BigCommerceClient } from '../clients/bigcommerce.js';
import type { ShippingZone, ShippingMethod, TaxClass, PaymentMethod, Currency } from '../types/index.js';

export function registerShippingTools(client: BigCommerceClient) {
  return {
    // Shipping Zones
    bigcommerce_list_shipping_zones: {
      description: 'List all shipping zones',
      parameters: {
        type: 'object' as const,
        properties: {},
      },
      handler: async () => {
        const result = await client.get<{ data: ShippingZone[] }>('/shipping/zones');
        return result.data;
      },
    },

    bigcommerce_get_shipping_zone: {
      description: 'Get a specific shipping zone by ID',
      parameters: {
        type: 'object' as const,
        properties: {
          zone_id: { type: 'number', description: 'Shipping zone ID', required: true },
        },
        required: ['zone_id'],
      },
      handler: async (params: { zone_id: number }) => {
        const result = await client.get<{ data: ShippingZone }>(`/shipping/zones/${params.zone_id}`);
        return result.data;
      },
    },

    bigcommerce_create_shipping_zone: {
      description: 'Create a new shipping zone',
      parameters: {
        type: 'object' as const,
        properties: {
          name: { type: 'string', description: 'Zone name', required: true },
          type: { type: 'string', enum: ['global', 'country', 'state', 'zip'], description: 'Zone type', required: true },
          locations: {
            type: 'array',
            description: 'Locations in this zone',
            items: {
              type: 'object',
              properties: {
                country_iso2: { type: 'string', description: '2-letter country code' },
                state_iso2: { type: 'string', description: '2-letter state code' },
                zip_start: { type: 'string', description: 'Zip code range start' },
                zip_end: { type: 'string', description: 'Zip code range end' },
              },
            },
            required: true,
          },
          enabled: { type: 'boolean', description: 'Enabled status (default: true)' },
        },
        required: ['name', 'type', 'locations'],
      },
      handler: async (params: Partial<ShippingZone>) => {
        const result = await client.post<{ data: ShippingZone }>('/shipping/zones', params);
        return result.data;
      },
    },

    bigcommerce_update_shipping_zone: {
      description: 'Update a shipping zone',
      parameters: {
        type: 'object' as const,
        properties: {
          zone_id: { type: 'number', description: 'Shipping zone ID', required: true },
          name: { type: 'string', description: 'Zone name' },
          enabled: { type: 'boolean', description: 'Enabled status' },
        },
        required: ['zone_id'],
      },
      handler: async (params: { zone_id: number; [key: string]: unknown }) => {
        const { zone_id, ...updateData } = params;
        const result = await client.put<{ data: ShippingZone }>(`/shipping/zones/${zone_id}`, updateData);
        return result.data;
      },
    },

    bigcommerce_delete_shipping_zone: {
      description: 'Delete a shipping zone',
      parameters: {
        type: 'object' as const,
        properties: {
          zone_id: { type: 'number', description: 'Shipping zone ID', required: true },
        },
        required: ['zone_id'],
      },
      handler: async (params: { zone_id: number }) => {
        await client.delete(`/shipping/zones/${params.zone_id}`);
        return { success: true, message: `Shipping zone ${params.zone_id} deleted` };
      },
    },

    // Shipping Methods
    bigcommerce_list_shipping_methods: {
      description: 'List all shipping methods for a zone',
      parameters: {
        type: 'object' as const,
        properties: {
          zone_id: { type: 'number', description: 'Shipping zone ID (omit for all methods)', required: false },
        },
      },
      handler: async (params: { zone_id?: number }) => {
        const endpoint = params.zone_id ? `/shipping/zones/${params.zone_id}/methods` : '/shipping/methods';
        const result = await client.get<{ data: ShippingMethod[] }>(endpoint);
        return result.data;
      },
    },

    bigcommerce_create_shipping_method: {
      description: 'Create a new shipping method',
      parameters: {
        type: 'object' as const,
        properties: {
          zone_id: { type: 'number', description: 'Shipping zone ID', required: true },
          name: { type: 'string', description: 'Method name', required: true },
          type: {
            type: 'string',
            enum: ['perorder', 'peritem', 'weight', 'total', 'auspost', 'canadapost', 'endicia', 'usps', 'fedex', 'ups', 'upsready', 'freightquote'],
            description: 'Method type',
            required: true,
          },
          settings: {
            type: 'object',
            description: 'Method settings (e.g., {rate: 10.00})',
            properties: {
              rate: { type: 'number', description: 'Shipping rate' },
            },
          },
          enabled: { type: 'boolean', description: 'Enabled status (default: true)' },
        },
        required: ['zone_id', 'name', 'type'],
      },
      handler: async (params: { zone_id: number; [key: string]: unknown }) => {
        const { zone_id, ...methodData } = params;
        const result = await client.post<{ data: ShippingMethod }>(`/shipping/zones/${zone_id}/methods`, methodData);
        return result.data;
      },
    },

    bigcommerce_update_shipping_method: {
      description: 'Update a shipping method',
      parameters: {
        type: 'object' as const,
        properties: {
          zone_id: { type: 'number', description: 'Shipping zone ID', required: true },
          method_id: { type: 'number', description: 'Shipping method ID', required: true },
          name: { type: 'string', description: 'Method name' },
          enabled: { type: 'boolean', description: 'Enabled status' },
          settings: { type: 'object', description: 'Method settings' },
        },
        required: ['zone_id', 'method_id'],
      },
      handler: async (params: { zone_id: number; method_id: number; [key: string]: unknown }) => {
        const { zone_id, method_id, ...updateData } = params;
        const result = await client.put<{ data: ShippingMethod }>(`/shipping/zones/${zone_id}/methods/${method_id}`, updateData);
        return result.data;
      },
    },

    bigcommerce_delete_shipping_method: {
      description: 'Delete a shipping method',
      parameters: {
        type: 'object' as const,
        properties: {
          zone_id: { type: 'number', description: 'Shipping zone ID', required: true },
          method_id: { type: 'number', description: 'Shipping method ID', required: true },
        },
        required: ['zone_id', 'method_id'],
      },
      handler: async (params: { zone_id: number; method_id: number }) => {
        await client.delete(`/shipping/zones/${params.zone_id}/methods/${params.method_id}`);
        return { success: true, message: `Shipping method ${params.method_id} deleted` };
      },
    },

    // Tax Classes
    bigcommerce_list_tax_classes: {
      description: 'List all tax classes',
      parameters: {
        type: 'object' as const,
        properties: {},
      },
      handler: async () => {
        const result = await client.get<{ data: TaxClass[] }>('/tax-classes');
        return result.data;
      },
    },

    bigcommerce_get_tax_class: {
      description: 'Get a specific tax class by ID',
      parameters: {
        type: 'object' as const,
        properties: {
          tax_class_id: { type: 'number', description: 'Tax class ID', required: true },
        },
        required: ['tax_class_id'],
      },
      handler: async (params: { tax_class_id: number }) => {
        const result = await client.get<{ data: TaxClass }>(`/tax-classes/${params.tax_class_id}`);
        return result.data;
      },
    },

    // Payment Methods
    bigcommerce_list_payment_methods: {
      description: 'List all accepted payment methods for the store',
      parameters: {
        type: 'object' as const,
        properties: {},
      },
      handler: async () => {
        const result = await client.get<{ data: PaymentMethod[] }>('/payments/methods');
        return result.data;
      },
    },

    // Currencies
    bigcommerce_list_currencies: {
      description: 'List all currencies configured in the store',
      parameters: {
        type: 'object' as const,
        properties: {},
      },
      handler: async () => {
        const result = await client.get<{ data: Currency[] }>('/currencies');
        return result.data;
      },
    },

    bigcommerce_get_currency: {
      description: 'Get a specific currency by ID',
      parameters: {
        type: 'object' as const,
        properties: {
          currency_id: { type: 'number', description: 'Currency ID', required: true },
        },
        required: ['currency_id'],
      },
      handler: async (params: { currency_id: number }) => {
        const result = await client.get<{ data: Currency }>(`/currencies/${params.currency_id}`);
        return result.data;
      },
    },

    bigcommerce_create_currency: {
      description: 'Add a new currency to the store',
      parameters: {
        type: 'object' as const,
        properties: {
          country_iso2: { type: 'string', description: '2-letter country code (e.g., US, CA, GB)', required: true },
          currency_code: { type: 'string', description: '3-letter currency code (e.g., USD, CAD, GBP)', required: true },
          currency_exchange_rate: { type: 'string', description: 'Exchange rate (as string decimal)', required: true },
          auto_update: { type: 'boolean', description: 'Auto-update exchange rate (default: false)' },
          enabled: { type: 'boolean', description: 'Enable currency (default: true)' },
          is_transactional: { type: 'boolean', description: 'Allow transactions in this currency' },
        },
        required: ['country_iso2', 'currency_code', 'currency_exchange_rate'],
      },
      handler: async (params: Partial<Currency>) => {
        const result = await client.post<{ data: Currency }>('/currencies', params);
        return result.data;
      },
    },

    bigcommerce_update_currency: {
      description: 'Update a currency',
      parameters: {
        type: 'object' as const,
        properties: {
          currency_id: { type: 'number', description: 'Currency ID', required: true },
          currency_exchange_rate: { type: 'string', description: 'Exchange rate' },
          auto_update: { type: 'boolean', description: 'Auto-update exchange rate' },
          enabled: { type: 'boolean', description: 'Enable/disable currency' },
        },
        required: ['currency_id'],
      },
      handler: async (params: { currency_id: number; [key: string]: unknown }) => {
        const { currency_id, ...updateData } = params;
        const result = await client.put<{ data: Currency }>(`/currencies/${currency_id}`, updateData);
        return result.data;
      },
    },

    bigcommerce_delete_currency: {
      description: 'Delete a currency',
      parameters: {
        type: 'object' as const,
        properties: {
          currency_id: { type: 'number', description: 'Currency ID', required: true },
        },
        required: ['currency_id'],
      },
      handler: async (params: { currency_id: number }) => {
        await client.delete(`/currencies/${params.currency_id}`);
        return { success: true, message: `Currency ${params.currency_id} deleted` };
      },
    },
  };
}
