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

const ListCustomCollectionsInput = z.object({
  limit: z.number().min(1).max(250).default(50).describe('Results per page'),
  page_info: z.string().optional().describe('Cursor for pagination'),
  product_id: z.string().optional().describe('Filter by product ID'),
  title: z.string().optional().describe('Filter by title'),
});

const GetCustomCollectionInput = z.object({
  id: z.string().describe('Custom collection ID'),
});

const CreateCustomCollectionInput = z.object({
  title: z.string().describe('Collection title'),
  body_html: z.string().optional().describe('Collection description HTML'),
  sort_order: z.enum(['alpha-asc', 'alpha-desc', 'best-selling', 'created', 'created-desc', 'manual', 'price-asc', 'price-desc']).optional().describe('Sort order'),
  published: z.boolean().optional().describe('Published status'),
  image: z.object({
    src: z.string().describe('Image URL'),
    alt: z.string().optional().describe('Alt text'),
  }).optional().describe('Collection image'),
});

const UpdateCustomCollectionInput = z.object({
  id: z.string().describe('Custom collection ID'),
  title: z.string().optional().describe('Collection title'),
  body_html: z.string().optional().describe('Collection description HTML'),
  sort_order: z.enum(['alpha-asc', 'alpha-desc', 'best-selling', 'created', 'created-desc', 'manual', 'price-asc', 'price-desc']).optional().describe('Sort order'),
  published: z.boolean().optional().describe('Published status'),
});

const DeleteCustomCollectionInput = z.object({
  id: z.string().describe('Custom collection ID'),
});

const AddProductToCollectionInput = z.object({
  collection_id: z.string().describe('Collection ID'),
  product_id: z.string().describe('Product ID'),
});

export default [
    {
      name: 'shopify_list_custom_collections',
      description: 'List custom collections with pagination and 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' },
          product_id: { type: 'string', description: 'Filter by product ID' },
          title: { type: 'string', description: 'Filter by title' },
        },
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = ListCustomCollectionsInput.parse(input);
        const results = await client.list('/custom_collections.json', { params: validated });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_get_custom_collection',
      description: 'Get a specific custom collection by ID',
      inputSchema: {
        type: 'object' as const,
        properties: {
          id: { type: 'string', description: 'Custom collection ID' },
        },
        required: ['id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = GetCustomCollectionInput.parse(input);
        const result = await client.get(`/custom_collections/${validated.id}.json`);
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_create_custom_collection',
      description: 'Create a new custom collection',
      inputSchema: {
        type: 'object' as const,
        properties: {
          title: { type: 'string', description: 'Collection title' },
          body_html: { type: 'string', description: 'Collection description HTML' },
          sort_order: { type: 'string', enum: ['alpha-asc', 'alpha-desc', 'best-selling', 'created', 'created-desc', 'manual', 'price-asc', 'price-desc'], description: 'Sort order' },
          published: { type: 'boolean', description: 'Published status' },
          image: {
            type: 'object',
            description: 'Collection image',
            properties: {
              src: { type: 'string', description: 'Image URL' },
              alt: { type: 'string', description: 'Alt text' },
            },
          },
        },
        required: ['title'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = CreateCustomCollectionInput.parse(input);
        const result = await client.create('/custom_collections.json', { custom_collection: validated });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_update_custom_collection',
      description: 'Update an existing custom collection',
      inputSchema: {
        type: 'object' as const,
        properties: {
          id: { type: 'string', description: 'Custom collection ID' },
          title: { type: 'string', description: 'Collection title' },
          body_html: { type: 'string', description: 'Collection description HTML' },
          sort_order: { type: 'string', enum: ['alpha-asc', 'alpha-desc', 'best-selling', 'created', 'created-desc', 'manual', 'price-asc', 'price-desc'], description: 'Sort order' },
          published: { type: 'boolean', description: 'Published status' },
        },
        required: ['id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = UpdateCustomCollectionInput.parse(input);
        const { id, ...updates } = validated;
        const result = await client.update(`/custom_collections/${id}.json`, { custom_collection: updates });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_delete_custom_collection',
      description: 'Delete a custom collection by ID',
      inputSchema: {
        type: 'object' as const,
        properties: {
          id: { type: 'string', description: 'Custom collection ID' },
        },
        required: ['id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = DeleteCustomCollectionInput.parse(input);
        await client.delete(`/custom_collections/${validated.id}.json`);
        return {
          content: [{ type: 'text' as const, text: `Custom collection ${validated.id} deleted successfully` }],
        };
      },
    },
    {
      name: 'shopify_add_product_to_collection',
      description: 'Add a product to a collection (creates a collect)',
      inputSchema: {
        type: 'object' as const,
        properties: {
          collection_id: { type: 'string', description: 'Collection ID' },
          product_id: { type: 'string', description: 'Product ID' },
        },
        required: ['collection_id', 'product_id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = AddProductToCollectionInput.parse(input);
        const result = await client.create('/collects.json', {
          collect: {
            collection_id: validated.collection_id,
            product_id: validated.product_id,
          },
        });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
  ];
