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

const ListShippingZonesInput = z.object({
  fields: z.string().optional().describe('Comma-separated list of fields to retrieve'),
});

const ListCarrierServicesInput = z.object({
  limit: z.number().min(1).max(250).default(50).describe('Results per page'),
});

const GetCarrierServiceInput = z.object({
  id: z.string().describe('Carrier service ID'),
});

const CreateCarrierServiceInput = z.object({
  name: z.string().describe('Carrier service name'),
  callback_url: z.string().describe('URL for rate calculation callback'),
  service_discovery: z.boolean().describe('Whether to discover services automatically'),
  carrier_service_type: z.enum(['api', 'legacy']).optional().describe('Type of carrier service'),
  format: z.enum(['json', 'xml']).optional().describe('Data format'),
});

const UpdateCarrierServiceInput = z.object({
  id: z.string().describe('Carrier service ID'),
  name: z.string().optional().describe('Carrier service name'),
  callback_url: z.string().optional().describe('URL for rate calculation callback'),
  service_discovery: z.boolean().optional().describe('Whether to discover services automatically'),
});

const DeleteCarrierServiceInput = z.object({
  id: z.string().describe('Carrier service ID'),
});

export default [
    {
      name: 'shopify_list_shipping_zones',
      description: 'List all shipping zones',
      inputSchema: {
        type: 'object' as const,
        properties: {
          fields: { type: 'string', description: 'Comma-separated list of fields to retrieve' },
        },
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = ListShippingZonesInput.parse(input);
        const results = await client.list('/shipping_zones.json', { params: validated });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_list_carrier_services',
      description: 'List all carrier services (third-party shipping rate calculators)',
      inputSchema: {
        type: 'object' as const,
        properties: {
          limit: { type: 'number', description: 'Results per page', default: 50, minimum: 1, maximum: 250 },
        },
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = ListCarrierServicesInput.parse(input);
        const results = await client.list('/carrier_services.json', { params: validated });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_get_carrier_service',
      description: 'Get a specific carrier service by ID',
      inputSchema: {
        type: 'object' as const,
        properties: {
          id: { type: 'string', description: 'Carrier service ID' },
        },
        required: ['id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = GetCarrierServiceInput.parse(input);
        const result = await client.get(`/carrier_services/${validated.id}.json`);
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_create_carrier_service',
      description: 'Create a new carrier service',
      inputSchema: {
        type: 'object' as const,
        properties: {
          name: { type: 'string', description: 'Carrier service name' },
          callback_url: { type: 'string', description: 'URL for rate calculation callback' },
          service_discovery: { type: 'boolean', description: 'Whether to discover services automatically' },
          carrier_service_type: { type: 'string', enum: ['api', 'legacy'], description: 'Type of carrier service' },
          format: { type: 'string', enum: ['json', 'xml'], description: 'Data format' },
        },
        required: ['name', 'callback_url', 'service_discovery'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = CreateCarrierServiceInput.parse(input);
        const result = await client.create('/carrier_services.json', { carrier_service: validated });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_update_carrier_service',
      description: 'Update an existing carrier service',
      inputSchema: {
        type: 'object' as const,
        properties: {
          id: { type: 'string', description: 'Carrier service ID' },
          name: { type: 'string', description: 'Carrier service name' },
          callback_url: { type: 'string', description: 'URL for rate calculation callback' },
          service_discovery: { type: 'boolean', description: 'Whether to discover services automatically' },
        },
        required: ['id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = UpdateCarrierServiceInput.parse(input);
        const { id, ...updates } = validated;
        const result = await client.update(`/carrier_services/${id}.json`, { carrier_service: updates });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_delete_carrier_service',
      description: 'Delete a carrier service by ID',
      inputSchema: {
        type: 'object' as const,
        properties: {
          id: { type: 'string', description: 'Carrier service ID' },
        },
        required: ['id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = DeleteCarrierServiceInput.parse(input);
        await client.delete(`/carrier_services/${validated.id}.json`);
        return {
          content: [{ type: 'text' as const, text: `Carrier service ${validated.id} deleted successfully` }],
        };
      },
    },
  ];
