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

const ListPagesInput = z.object({
  limit: z.number().min(1).max(250).default(50).describe('Results per page'),
  page_info: z.string().optional().describe('Cursor for pagination'),
  title: z.string().optional().describe('Filter by title'),
  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)'),
  updated_at_min: z.string().optional().describe('Filter by min update date (ISO 8601)'),
  fields: z.string().optional().describe('Comma-separated list of fields to retrieve'),
});

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

const CreatePageInput = z.object({
  title: z.string().describe('Page title'),
  body_html: z.string().optional().describe('Page content HTML'),
  author: z.string().optional().describe('Page author'),
  published: z.boolean().optional().describe('Published status'),
  metafields: z.array(z.object({
    namespace: z.string().describe('Metafield namespace'),
    key: z.string().describe('Metafield key'),
    value: z.string().describe('Metafield value'),
    type: z.string().describe('Metafield type'),
  })).optional().describe('Page metafields'),
});

const UpdatePageInput = z.object({
  id: z.string().describe('Page ID'),
  title: z.string().optional().describe('Page title'),
  body_html: z.string().optional().describe('Page content HTML'),
  author: z.string().optional().describe('Page author'),
  published: z.boolean().optional().describe('Published status'),
});

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

export default [
    {
      name: 'shopify_list_pages',
      description: 'List pages with pagination, title filtering, and date filtering',
      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' },
          title: { type: 'string', description: 'Filter by title' },
          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)' },
          updated_at_min: { type: 'string', description: 'Filter by min update date (ISO 8601)' },
          fields: { type: 'string', description: 'Comma-separated list of fields to retrieve' },
        },
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = ListPagesInput.parse(input);
        const results = await client.list('/pages.json', { params: validated });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_get_page',
      description: 'Get a specific page by ID with optional field filtering',
      inputSchema: {
        type: 'object' as const,
        properties: {
          id: { type: 'string', description: 'Page ID' },
          fields: { type: 'string', description: 'Comma-separated list of fields to retrieve' },
        },
        required: ['id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = GetPageInput.parse(input);
        const result = await client.get(`/pages/${validated.id}.json`, {
          params: validated.fields ? { fields: validated.fields } : {},
        });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_create_page',
      description: 'Create a new page with optional metafields',
      inputSchema: {
        type: 'object' as const,
        properties: {
          title: { type: 'string', description: 'Page title' },
          body_html: { type: 'string', description: 'Page content HTML' },
          author: { type: 'string', description: 'Page author' },
          published: { type: 'boolean', description: 'Published status' },
          metafields: {
            type: 'array',
            description: 'Page metafields',
            items: {
              type: 'object',
              properties: {
                namespace: { type: 'string', description: 'Metafield namespace' },
                key: { type: 'string', description: 'Metafield key' },
                value: { type: 'string', description: 'Metafield value' },
                type: { type: 'string', description: 'Metafield type' },
              },
            },
          },
        },
        required: ['title'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = CreatePageInput.parse(input);
        const result = await client.create('/pages.json', { page: validated });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_update_page',
      description: 'Update an existing page',
      inputSchema: {
        type: 'object' as const,
        properties: {
          id: { type: 'string', description: 'Page ID' },
          title: { type: 'string', description: 'Page title' },
          body_html: { type: 'string', description: 'Page content HTML' },
          author: { type: 'string', description: 'Page author' },
          published: { type: 'boolean', description: 'Published status' },
        },
        required: ['id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = UpdatePageInput.parse(input);
        const { id, ...updates } = validated;
        const result = await client.update(`/pages/${id}.json`, { page: updates });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_delete_page',
      description: 'Delete a page by ID',
      inputSchema: {
        type: 'object' as const,
        properties: {
          id: { type: 'string', description: 'Page ID' },
        },
        required: ['id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = DeletePageInput.parse(input);
        await client.delete(`/pages/${validated.id}.json`);
        return {
          content: [{ type: 'text' as const, text: `Page ${validated.id} deleted successfully` }],
        };
      },
    },
  ];
