/**
 * BigCommerce Categories Tools
 */

import { BigCommerceClient } from '../clients/bigcommerce.js';

export function registerCategoriesTools(client: BigCommerceClient) {
  return [
    {
      name: 'bigcommerce_list_categories',
      description: 'List all categories from BigCommerce store with optional filters',
      inputSchema: {
        type: 'object',
        properties: {
          name: { type: 'string', description: 'Filter by category name' },
          parent_id: { type: 'number', description: 'Filter by parent category ID' },
          is_visible: { type: 'boolean', description: 'Filter by visibility' },
          page_title: { type: 'string', description: 'Filter by page title' },
        },
      },
      handler: async (args: any) => {
        const categories = await client.get(`/catalog/categories?${new URLSearchParams(args).toString()}`);
        return { content: [{ type: 'text', text: JSON.stringify(categories, null, 2) }] };
      },
    },
    {
      name: 'bigcommerce_get_category',
      description: 'Get detailed information about a specific category',
      inputSchema: {
        type: 'object',
        properties: {
          category_id: { type: 'number', description: 'Category ID' },
        },
        required: ['category_id'],
      },
      handler: async (args: any) => {
        const category = await client.get(`/catalog/categories/${args.category_id}`);
        return { content: [{ type: 'text', text: JSON.stringify(category, null, 2) }] };
      },
    },
    {
      name: 'bigcommerce_create_category',
      description: 'Create a new category in BigCommerce',
      inputSchema: {
        type: 'object',
        properties: {
          parent_id: { type: 'number', description: 'Parent category ID (0 for root)' },
          name: { type: 'string', description: 'Category name' },
          description: { type: 'string', description: 'Category description' },
          sort_order: { type: 'number', description: 'Sort order for display' },
          page_title: { type: 'string', description: 'Page title for SEO' },
          meta_keywords: { 
            type: 'array', 
            items: { type: 'string' },
            description: 'Meta keywords for SEO' 
          },
          meta_description: { type: 'string', description: 'Meta description for SEO' },
          is_visible: { type: 'boolean', description: 'Is category visible' },
          search_keywords: { type: 'string', description: 'Search keywords' },
          default_product_sort: { 
            type: 'string',
            enum: ['use_store_settings', 'featured', 'newest', 'bestselling', 'alphaasc', 'alphadesc', 'avgcustomerreview', 'priceasc', 'pricedesc'],
            description: 'Default product sort order'
          },
          image_url: { type: 'string', description: 'Category image URL' },
        },
        required: ['parent_id', 'name'],
      },
      handler: async (args: any) => {
        const category = await client.post(`/catalog/categories`, args);
        return { content: [{ type: 'text', text: JSON.stringify(category, null, 2) }] };
      },
    },
    {
      name: 'bigcommerce_update_category',
      description: 'Update an existing category',
      inputSchema: {
        type: 'object',
        properties: {
          category_id: { type: 'number', description: 'Category ID' },
          parent_id: { type: 'number', description: 'Parent category ID' },
          name: { type: 'string', description: 'Category name' },
          description: { type: 'string', description: 'Category description' },
          sort_order: { type: 'number', description: 'Sort order for display' },
          page_title: { type: 'string', description: 'Page title for SEO' },
          meta_keywords: { 
            type: 'array', 
            items: { type: 'string' },
            description: 'Meta keywords for SEO' 
          },
          meta_description: { type: 'string', description: 'Meta description for SEO' },
          is_visible: { type: 'boolean', description: 'Is category visible' },
          search_keywords: { type: 'string', description: 'Search keywords' },
          default_product_sort: { 
            type: 'string',
            description: 'Default product sort order'
          },
          image_url: { type: 'string', description: 'Category image URL' },
        },
        required: ['category_id'],
      },
      handler: async (args: any) => {
        const { category_id, ...updateData } = args;
        const category = await client.put(`/catalog/categories/${category_id}`, updateData);
        return { content: [{ type: 'text', text: JSON.stringify(category, null, 2) }] };
      },
    },
    {
      name: 'bigcommerce_delete_category',
      description: 'Delete a category from BigCommerce',
      inputSchema: {
        type: 'object',
        properties: {
          category_id: { type: 'number', description: 'Category ID to delete' },
        },
        required: ['category_id'],
      },
      handler: async (args: any) => {
        await client.delete(`/catalog/categories/${args.category_id}`);
        return { content: [{ type: 'text', text: `Category ${args.category_id} deleted successfully` }] };
      },
    },
    {
      name: 'bigcommerce_get_category_tree',
      description: 'Get the complete category tree structure',
      inputSchema: {
        type: 'object',
        properties: {},
      },
      handler: async (args: any) => {
        const tree = await client.get(`/catalog/categories/tree`);
        return { content: [{ type: 'text', text: JSON.stringify(tree, null, 2) }] };
      },
    },
  ];
}
