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

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

const ListEventsInput = z.object({
  limit: z.number().min(1).max(250).default(50).describe('Results per page'),
  page_info: z.string().optional().describe('Cursor for pagination'),
  created_at_min: z.string().optional().describe('Filter by min creation date (ISO 8601)'),
  created_at_max: z.string().optional().describe('Filter by max creation date (ISO 8601)'),
  verb: z.string().optional().describe('Filter by event verb (e.g., "create", "update", "delete")'),
  filter: z.string().optional().describe('Filter events by resource (e.g., "Product", "Order")'),
});

const GetEventInput = z.object({
  id: z.string().describe('Event ID'),
});

export default [
    {
      name: 'shopify_get_shop',
      description: 'Get shop information (name, domain, currency, plan, etc.)',
      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 = GetShopInput.parse(input);
        const result = await client.get('/shop.json', { params: validated });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_list_events',
      description: 'List events (audit log of changes to shop resources) with filtering by date, verb, and resource type',
      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' },
          created_at_min: { type: 'string', description: 'Filter by min creation date (ISO 8601)' },
          created_at_max: { type: 'string', description: 'Filter by max creation date (ISO 8601)' },
          verb: { type: 'string', description: 'Filter by event verb (e.g., "create", "update", "delete")' },
          filter: { type: 'string', description: 'Filter events by resource (e.g., "Product", "Order")' },
        },
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = ListEventsInput.parse(input);
        const results = await client.list('/events.json', { params: validated });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_get_event',
      description: 'Get a specific event by ID',
      inputSchema: {
        type: 'object' as const,
        properties: {
          id: { type: 'string', description: 'Event ID' },
        },
        required: ['id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = GetEventInput.parse(input);
        const result = await client.get(`/events/${validated.id}.json`);
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
  ];
