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

const ListBlogsInput = z.object({
  limit: z.number().min(1).max(250).default(50).describe('Results per page'),
  page_info: z.string().optional().describe('Cursor for pagination'),
  fields: z.string().optional().describe('Comma-separated list of fields to retrieve'),
});

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

const ListArticlesInput = z.object({
  blog_id: z.string().describe('Blog ID'),
  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)'),
  published_at_min: z.string().optional().describe('Filter by min published date (ISO 8601)'),
  published_at_max: z.string().optional().describe('Filter by max published date (ISO 8601)'),
  fields: z.string().optional().describe('Comma-separated list of fields to retrieve'),
});

const GetArticleInput = z.object({
  blog_id: z.string().describe('Blog ID'),
  article_id: z.string().describe('Article ID'),
  fields: z.string().optional().describe('Comma-separated list of fields to retrieve'),
});

const CreateArticleInput = z.object({
  blog_id: z.string().describe('Blog ID'),
  title: z.string().describe('Article title'),
  body_html: z.string().optional().describe('Article content HTML'),
  author: z.string().optional().describe('Article author'),
  tags: z.string().optional().describe('Comma-separated tags'),
  published: z.boolean().optional().describe('Published status'),
  published_at: z.string().optional().describe('Published date/time (ISO 8601)'),
  image: z.object({
    src: z.string().describe('Image URL'),
    alt: z.string().optional().describe('Alt text'),
  }).optional().describe('Article featured image'),
});

const UpdateArticleInput = z.object({
  blog_id: z.string().describe('Blog ID'),
  article_id: z.string().describe('Article ID'),
  title: z.string().optional().describe('Article title'),
  body_html: z.string().optional().describe('Article content HTML'),
  author: z.string().optional().describe('Article author'),
  tags: z.string().optional().describe('Comma-separated tags'),
  published: z.boolean().optional().describe('Published status'),
});

const DeleteArticleInput = z.object({
  blog_id: z.string().describe('Blog ID'),
  article_id: z.string().describe('Article ID'),
});

export default [
    {
      name: 'shopify_list_blogs',
      description: 'List all blogs',
      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' },
          fields: { type: 'string', description: 'Comma-separated list of fields to retrieve' },
        },
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = ListBlogsInput.parse(input);
        const results = await client.list('/blogs.json', { params: validated });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_get_blog',
      description: 'Get a specific blog by ID',
      inputSchema: {
        type: 'object' as const,
        properties: {
          id: { type: 'string', description: 'Blog ID' },
          fields: { type: 'string', description: 'Comma-separated list of fields to retrieve' },
        },
        required: ['id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = GetBlogInput.parse(input);
        const result = await client.get(`/blogs/${validated.id}.json`, {
          params: validated.fields ? { fields: validated.fields } : {},
        });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_list_articles',
      description: 'List articles in a blog with date filtering and pagination',
      inputSchema: {
        type: 'object' as const,
        properties: {
          blog_id: { type: 'string', description: 'Blog ID' },
          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)' },
          published_at_min: { type: 'string', description: 'Filter by min published date (ISO 8601)' },
          published_at_max: { type: 'string', description: 'Filter by max published date (ISO 8601)' },
          fields: { type: 'string', description: 'Comma-separated list of fields to retrieve' },
        },
        required: ['blog_id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = ListArticlesInput.parse(input);
        const { blog_id, ...params } = validated;
        const results = await client.list(`/blogs/${blog_id}/articles.json`, { params });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(results, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_get_article',
      description: 'Get a specific article by ID',
      inputSchema: {
        type: 'object' as const,
        properties: {
          blog_id: { type: 'string', description: 'Blog ID' },
          article_id: { type: 'string', description: 'Article ID' },
          fields: { type: 'string', description: 'Comma-separated list of fields to retrieve' },
        },
        required: ['blog_id', 'article_id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = GetArticleInput.parse(input);
        const result = await client.get(`/blogs/${validated.blog_id}/articles/${validated.article_id}.json`, {
          params: validated.fields ? { fields: validated.fields } : {},
        });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_create_article',
      description: 'Create a new article in a blog',
      inputSchema: {
        type: 'object' as const,
        properties: {
          blog_id: { type: 'string', description: 'Blog ID' },
          title: { type: 'string', description: 'Article title' },
          body_html: { type: 'string', description: 'Article content HTML' },
          author: { type: 'string', description: 'Article author' },
          tags: { type: 'string', description: 'Comma-separated tags' },
          published: { type: 'boolean', description: 'Published status' },
          published_at: { type: 'string', description: 'Published date/time (ISO 8601)' },
          image: {
            type: 'object',
            description: 'Article featured image',
            properties: {
              src: { type: 'string', description: 'Image URL' },
              alt: { type: 'string', description: 'Alt text' },
            },
          },
        },
        required: ['blog_id', 'title'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = CreateArticleInput.parse(input);
        const { blog_id, ...articleData } = validated;
        const result = await client.create(`/blogs/${blog_id}/articles.json`, { article: articleData });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_update_article',
      description: 'Update an existing article',
      inputSchema: {
        type: 'object' as const,
        properties: {
          blog_id: { type: 'string', description: 'Blog ID' },
          article_id: { type: 'string', description: 'Article ID' },
          title: { type: 'string', description: 'Article title' },
          body_html: { type: 'string', description: 'Article content HTML' },
          author: { type: 'string', description: 'Article author' },
          tags: { type: 'string', description: 'Comma-separated tags' },
          published: { type: 'boolean', description: 'Published status' },
        },
        required: ['blog_id', 'article_id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = UpdateArticleInput.parse(input);
        const { blog_id, article_id, ...updates } = validated;
        const result = await client.update(`/blogs/${blog_id}/articles/${article_id}.json`, { article: updates });
        return {
          content: [{ type: 'text' as const, text: JSON.stringify(result, null, 2) }],
        };
      },
    },
    {
      name: 'shopify_delete_article',
      description: 'Delete an article by ID',
      inputSchema: {
        type: 'object' as const,
        properties: {
          blog_id: { type: 'string', description: 'Blog ID' },
          article_id: { type: 'string', description: 'Article ID' },
        },
        required: ['blog_id', 'article_id'],
      },
      handler: async (input: unknown, client: ShopifyClient) => {
        const validated = DeleteArticleInput.parse(input);
        await client.delete(`/blogs/${validated.blog_id}/articles/${validated.article_id}.json`);
        return {
          content: [{ type: 'text' as const, text: `Article ${validated.article_id} deleted successfully` }],
        };
      },
    },
  ];
