/**
 * HubSpot Blog Tools
 * Tools for managing blog posts and blog authors
 */

import type { HubSpotClient } from '../clients/hubspot.js';

export function getTools(client: HubSpotClient) {
  return [
    // Blog Posts
    {
      name: 'hubspot_list_blog_posts',
      description: 'List blog posts',
      inputSchema: {
        type: 'object',
        properties: {
          limit: {
            type: 'number',
            description: 'Maximum number of posts to return',
            default: 20,
          },
          offset: {
            type: 'number',
            description: 'Offset for pagination',
            default: 0,
          },
          state: {
            type: 'string',
            description: 'Filter by state (DRAFT, PUBLISHED, SCHEDULED)',
          },
        },
      },
      handler: async (args: any) => {
        const params: any = {
          limit: args.limit || 20,
          offset: args.offset || 0,
        };
        if (args.state) params.state = args.state;

        const response = await client.apiRequest<any>({
          method: 'GET',
          url: '/cms/v3/blogs/posts',
          params,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_get_blog_post',
      description: 'Get a specific blog post by ID',
      inputSchema: {
        type: 'object',
        properties: {
          postId: {
            type: 'string',
            description: 'Blog post ID',
          },
        },
        required: ['postId'],
      },
      handler: async (args: any) => {
        const post = await client.apiRequest<any>({
          method: 'GET',
          url: `/cms/v3/blogs/posts/${args.postId}`,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(post, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_create_blog_post',
      description: 'Create a new blog post',
      inputSchema: {
        type: 'object',
        properties: {
          name: {
            type: 'string',
            description: 'Post title/name',
          },
          slug: {
            type: 'string',
            description: 'URL slug',
          },
          contentGroupId: {
            type: 'string',
            description: 'Blog ID',
          },
          htmlTitle: {
            type: 'string',
            description: 'HTML meta title',
          },
          postBody: {
            type: 'string',
            description: 'Post body content (HTML)',
          },
          metaDescription: {
            type: 'string',
            description: 'Meta description',
          },
        },
        required: ['name'],
      },
      handler: async (args: any) => {
        const post = await client.apiRequest<any>({
          method: 'POST',
          url: '/cms/v3/blogs/posts',
          data: {
            name: args.name,
            slug: args.slug,
            contentGroupId: args.contentGroupId,
            htmlTitle: args.htmlTitle,
            postBody: args.postBody,
            metaDescription: args.metaDescription,
          },
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  postId: post.id,
                  post,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_update_blog_post',
      description: 'Update an existing blog post',
      inputSchema: {
        type: 'object',
        properties: {
          postId: {
            type: 'string',
            description: 'Blog post ID',
          },
          name: {
            type: 'string',
            description: 'Updated post title',
          },
          postBody: {
            type: 'string',
            description: 'Updated post body',
          },
          metaDescription: {
            type: 'string',
            description: 'Updated meta description',
          },
        },
        required: ['postId'],
      },
      handler: async (args: any) => {
        const updates: any = {};
        if (args.name) updates.name = args.name;
        if (args.postBody) updates.postBody = args.postBody;
        if (args.metaDescription) updates.metaDescription = args.metaDescription;

        const post = await client.apiRequest<any>({
          method: 'PATCH',
          url: `/cms/v3/blogs/posts/${args.postId}`,
          data: updates,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  post,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_delete_blog_post',
      description: 'Archive a blog post',
      inputSchema: {
        type: 'object',
        properties: {
          postId: {
            type: 'string',
            description: 'Blog post ID to archive',
          },
        },
        required: ['postId'],
      },
      handler: async (args: any) => {
        await client.apiRequest({
          method: 'DELETE',
          url: `/cms/v3/blogs/posts/${args.postId}`,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  message: `Blog post ${args.postId} archived successfully`,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_publish_blog_post',
      description: 'Publish a blog post (schedule or publish immediately)',
      inputSchema: {
        type: 'object',
        properties: {
          postId: {
            type: 'string',
            description: 'Blog post ID',
          },
          publishDate: {
            type: 'string',
            description: 'Publish date/time (ISO format). If not provided, publishes immediately.',
          },
        },
        required: ['postId'],
      },
      handler: async (args: any) => {
        const data: any = {
          state: 'PUBLISHED',
        };
        if (args.publishDate) {
          data.state = 'SCHEDULED';
          data.publishDate = args.publishDate;
        }

        const post = await client.apiRequest<any>({
          method: 'PATCH',
          url: `/cms/v3/blogs/posts/${args.postId}`,
          data,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  success: true,
                  message: args.publishDate
                    ? `Post scheduled for ${args.publishDate}`
                    : 'Post published successfully',
                  post,
                },
                null,
                2
              ),
            },
          ],
        };
      },
    },

    // Blog Authors
    {
      name: 'hubspot_list_blog_authors',
      description: 'List all blog authors',
      inputSchema: {
        type: 'object',
        properties: {
          limit: {
            type: 'number',
            description: 'Maximum number of authors to return',
            default: 20,
          },
        },
      },
      handler: async (args: any) => {
        const response = await client.apiRequest<any>({
          method: 'GET',
          url: '/cms/v3/blogs/authors',
          params: {
            limit: args.limit || 20,
          },
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'hubspot_get_blog_author',
      description: 'Get a specific blog author by ID',
      inputSchema: {
        type: 'object',
        properties: {
          authorId: {
            type: 'string',
            description: 'Author ID',
          },
        },
        required: ['authorId'],
      },
      handler: async (args: any) => {
        const author = await client.apiRequest<any>({
          method: 'GET',
          url: `/cms/v3/blogs/authors/${args.authorId}`,
        });

        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(author, null, 2),
            },
          ],
        };
      },
    },
  ];
}
