import type { BigCommerceClient } from '../clients/bigcommerce.js';
import type { Script, Page } from '../types/index.js';

export function registerContentTools(client: BigCommerceClient) {
  return {
    // Scripts (for injecting custom JS/CSS)
    bigcommerce_list_scripts: {
      description: 'List all storefront scripts',
      parameters: {
        type: 'object' as const,
        properties: {
          page: { type: 'number', description: 'Page number (default: 1)' },
          limit: { type: 'number', description: 'Items per page (default: 50)' },
        },
      },
      handler: async (params: { page?: number; limit?: number }) => {
        const page = params.page || 1;
        const limit = params.limit || 50;
        const result = await client.getPage<Script>('/content/scripts', page, limit);
        return { scripts: result.data, meta: result.meta };
      },
    },

    bigcommerce_get_script: {
      description: 'Get a specific script by UUID',
      parameters: {
        type: 'object' as const,
        properties: {
          script_uuid: { type: 'string', description: 'Script UUID', required: true },
        },
        required: ['script_uuid'],
      },
      handler: async (params: { script_uuid: string }) => {
        const result = await client.get<{ data: Script }>(`/content/scripts/${params.script_uuid}`);
        return result.data;
      },
    },

    bigcommerce_create_script: {
      description: 'Create a new storefront script (inject JS/CSS)',
      parameters: {
        type: 'object' as const,
        properties: {
          name: { type: 'string', description: 'Script name (internal)', required: true },
          description: { type: 'string', description: 'Script description' },
          html: { type: 'string', description: 'Inline script/style HTML (use either html or src, not both)' },
          src: { type: 'string', description: 'External script/style URL (use either html or src, not both)' },
          load_method: { type: 'string', enum: ['default', 'async', 'defer'], description: 'Load method (default: default)' },
          location: { type: 'string', enum: ['head', 'footer'], description: 'Injection location (default: head)', required: true },
          visibility: { type: 'string', enum: ['all_pages', 'storefront', 'checkout', 'order_confirmation'], description: 'Where to show (default: all_pages)', required: true },
          kind: { type: 'string', enum: ['src', 'script_tag'], description: 'Script kind (default: script_tag)', required: true },
          consent_category: { type: 'string', enum: ['essential', 'functional', 'analytics', 'targeting'], description: 'GDPR consent category', required: true },
          enabled: { type: 'boolean', description: 'Enabled status (default: true)' },
        },
        required: ['name', 'location', 'visibility', 'kind', 'consent_category'],
      },
      handler: async (params: Partial<Script>) => {
        const result = await client.post<{ data: Script }>('/content/scripts', params);
        return result.data;
      },
    },

    bigcommerce_update_script: {
      description: 'Update a script',
      parameters: {
        type: 'object' as const,
        properties: {
          script_uuid: { type: 'string', description: 'Script UUID', required: true },
          name: { type: 'string', description: 'Script name' },
          description: { type: 'string', description: 'Script description' },
          html: { type: 'string', description: 'Inline script/style HTML' },
          src: { type: 'string', description: 'External script/style URL' },
          enabled: { type: 'boolean', description: 'Enabled status' },
        },
        required: ['script_uuid'],
      },
      handler: async (params: { script_uuid: string; [key: string]: unknown }) => {
        const { script_uuid, ...updateData } = params;
        const result = await client.put<{ data: Script }>(`/content/scripts/${script_uuid}`, updateData);
        return result.data;
      },
    },

    bigcommerce_delete_script: {
      description: 'Delete a script',
      parameters: {
        type: 'object' as const,
        properties: {
          script_uuid: { type: 'string', description: 'Script UUID', required: true },
        },
        required: ['script_uuid'],
      },
      handler: async (params: { script_uuid: string }) => {
        await client.delete(`/content/scripts/${params.script_uuid}`);
        return { success: true, message: `Script ${params.script_uuid} deleted` };
      },
    },

    // Pages (CMS content pages)
    bigcommerce_list_pages: {
      description: 'List all content pages',
      parameters: {
        type: 'object' as const,
        properties: {
          channel_id: { type: 'number', description: 'Filter by channel ID' },
          name: { type: 'string', description: 'Filter by page name (partial match)' },
          is_visible: { type: 'boolean', description: 'Filter by visibility' },
          page: { type: 'number', description: 'Page number (default: 1)' },
          limit: { type: 'number', description: 'Items per page (default: 50)' },
        },
      },
      handler: async (params: Record<string, unknown>) => {
        const queryParams: Record<string, string | number> = {};
        if (params.channel_id) queryParams.channel_id = params.channel_id as number;
        if (params.name) queryParams['name:like'] = params.name as string;
        if (params.is_visible !== undefined) queryParams.is_visible = params.is_visible ? 'true' : 'false';

        const page = (params.page as number) || 1;
        const limit = (params.limit as number) || 50;

        const result = await client.getPage<Page>('/pages', page, limit, queryParams, 'v2');
        return result;
      },
    },

    bigcommerce_get_page: {
      description: 'Get a specific page by ID',
      parameters: {
        type: 'object' as const,
        properties: {
          page_id: { type: 'number', description: 'Page ID', required: true },
        },
        required: ['page_id'],
      },
      handler: async (params: { page_id: number }) => {
        const result = await client.get<Page>(`/pages/${params.page_id}`, 'v2');
        return result;
      },
    },

    bigcommerce_create_page: {
      description: 'Create a new content page',
      parameters: {
        type: 'object' as const,
        properties: {
          name: { type: 'string', description: 'Page name', required: true },
          type: { type: 'string', enum: ['page', 'raw', 'contact_form', 'feed', 'link', 'blog'], description: 'Page type (default: page)', required: true },
          body: { type: 'string', description: 'Page HTML content (for type=page)' },
          is_visible: { type: 'boolean', description: 'Visibility (default: true)' },
          parent_id: { type: 'number', description: 'Parent page ID (for nested pages)' },
          sort_order: { type: 'number', description: 'Sort order' },
          meta_title: { type: 'string', description: 'SEO meta title' },
          meta_description: { type: 'string', description: 'SEO meta description' },
          is_homepage: { type: 'boolean', description: 'Set as homepage' },
          is_customers_only: { type: 'boolean', description: 'Restrict to logged-in customers' },
        },
        required: ['name', 'type'],
      },
      handler: async (params: Partial<Page>) => {
        const result = await client.post<Page>('/pages', params, 'v2');
        return result;
      },
    },

    bigcommerce_update_page: {
      description: 'Update a content page',
      parameters: {
        type: 'object' as const,
        properties: {
          page_id: { type: 'number', description: 'Page ID', required: true },
          name: { type: 'string', description: 'Page name' },
          body: { type: 'string', description: 'Page HTML content' },
          is_visible: { type: 'boolean', description: 'Visibility' },
          sort_order: { type: 'number', description: 'Sort order' },
        },
        required: ['page_id'],
      },
      handler: async (params: { page_id: number; [key: string]: unknown }) => {
        const { page_id, ...updateData } = params;
        const result = await client.put<Page>(`/pages/${page_id}`, updateData, 'v2');
        return result;
      },
    },

    bigcommerce_delete_page: {
      description: 'Delete a content page',
      parameters: {
        type: 'object' as const,
        properties: {
          page_id: { type: 'number', description: 'Page ID', required: true },
        },
        required: ['page_id'],
      },
      handler: async (params: { page_id: number }) => {
        await client.delete(`/pages/${params.page_id}`, 'v2');
        return { success: true, message: `Page ${params.page_id} deleted` };
      },
    },
  };
}
