import type { BigCommerceClient } from '../clients/bigcommerce.js';
import type { StoreInformation, Channel, Site } from '../types/index.js';

export function registerStoreTools(client: BigCommerceClient) {
  return {
    // Store Information
    bigcommerce_get_store_information: {
      description: 'Get store information and settings (name, domain, timezone, currency, etc.)',
      parameters: {
        type: 'object' as const,
        properties: {},
      },
      handler: async () => {
        const result = await client.get<{ data: StoreInformation }>('/store', 'v2');
        return result.data;
      },
    },

    // Channels
    bigcommerce_list_channels: {
      description: 'List all sales channels',
      parameters: {
        type: 'object' as const,
        properties: {
          type: { type: 'string', description: 'Filter by channel type (storefront, pos, marketplace)' },
          platform: { type: 'string', description: 'Filter by platform (bigcommerce, facebook, amazon, etc.)' },
          status: { type: 'string', enum: ['active', 'inactive', 'archived'], description: 'Filter by status' },
          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.type) queryParams.type = params.type as string;
        if (params.platform) queryParams.platform = params.platform as string;
        if (params.status) queryParams.status = params.status as string;

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

        const result = await client.getPage<Channel>('/channels', page, limit, queryParams);
        return { channels: result.data, meta: result.meta };
      },
    },

    bigcommerce_get_channel: {
      description: 'Get a specific channel by ID',
      parameters: {
        type: 'object' as const,
        properties: {
          channel_id: { type: 'number', description: 'Channel ID', required: true },
        },
        required: ['channel_id'],
      },
      handler: async (params: { channel_id: number }) => {
        const result = await client.get<{ data: Channel }>(`/channels/${params.channel_id}`);
        return result.data;
      },
    },

    bigcommerce_create_channel: {
      description: 'Create a new channel',
      parameters: {
        type: 'object' as const,
        properties: {
          name: { type: 'string', description: 'Channel name', required: true },
          type: { type: 'string', description: 'Channel type (storefront, pos, marketplace, marketing)', required: true },
          platform: { type: 'string', description: 'Platform identifier', required: true },
          external_id: { type: 'string', description: 'External platform ID' },
          status: { type: 'string', enum: ['active', 'inactive'], description: 'Channel status (default: active)' },
        },
        required: ['name', 'type', 'platform'],
      },
      handler: async (params: Partial<Channel>) => {
        const result = await client.post<{ data: Channel }>('/channels', params);
        return result.data;
      },
    },

    bigcommerce_update_channel: {
      description: 'Update a channel',
      parameters: {
        type: 'object' as const,
        properties: {
          channel_id: { type: 'number', description: 'Channel ID', required: true },
          name: { type: 'string', description: 'Channel name' },
          status: { type: 'string', enum: ['active', 'inactive'], description: 'Channel status' },
          is_visible: { type: 'boolean', description: 'Channel visibility' },
        },
        required: ['channel_id'],
      },
      handler: async (params: { channel_id: number; [key: string]: unknown }) => {
        const { channel_id, ...updateData } = params;
        const result = await client.put<{ data: Channel }>(`/channels/${channel_id}`, updateData);
        return result.data;
      },
    },

    // Sites (for multi-site/headless)
    bigcommerce_list_sites: {
      description: 'List all sites associated with channels',
      parameters: {
        type: 'object' as const,
        properties: {
          channel_id: { type: 'number', description: 'Filter by channel ID' },
        },
      },
      handler: async (params: { channel_id?: number }) => {
        const queryParams = params.channel_id ? { channel_id: params.channel_id } : {};
        const result = await client.get<{ data: Site[] }>('/channels/sites', undefined);
        return result.data;
      },
    },

    bigcommerce_get_site: {
      description: 'Get a specific site by ID',
      parameters: {
        type: 'object' as const,
        properties: {
          site_id: { type: 'number', description: 'Site ID', required: true },
        },
        required: ['site_id'],
      },
      handler: async (params: { site_id: number }) => {
        const result = await client.get<{ data: Site }>(`/channels/sites/${params.site_id}`);
        return result.data;
      },
    },

    bigcommerce_create_site: {
      description: 'Create a new site for a channel',
      parameters: {
        type: 'object' as const,
        properties: {
          channel_id: { type: 'number', description: 'Channel ID', required: true },
          url: { type: 'string', description: 'Site URL', required: true },
        },
        required: ['channel_id', 'url'],
      },
      handler: async (params: Partial<Site>) => {
        const result = await client.post<{ data: Site }>('/channels/sites', params);
        return result.data;
      },
    },

    bigcommerce_update_site: {
      description: 'Update a site',
      parameters: {
        type: 'object' as const,
        properties: {
          site_id: { type: 'number', description: 'Site ID', required: true },
          url: { type: 'string', description: 'Site URL' },
        },
        required: ['site_id'],
      },
      handler: async (params: { site_id: number; [key: string]: unknown }) => {
        const { site_id, ...updateData } = params;
        const result = await client.put<{ data: Site }>(`/channels/sites/${site_id}`, updateData);
        return result.data;
      },
    },

    bigcommerce_delete_site: {
      description: 'Delete a site',
      parameters: {
        type: 'object' as const,
        properties: {
          site_id: { type: 'number', description: 'Site ID', required: true },
        },
        required: ['site_id'],
      },
      handler: async (params: { site_id: number }) => {
        await client.delete(`/channels/sites/${params.site_id}`);
        return { success: true, message: `Site ${params.site_id} deleted` };
      },
    },

    // Time Zone
    bigcommerce_get_time_zone: {
      description: 'Get store time zone information',
      parameters: {
        type: 'object' as const,
        properties: {},
      },
      handler: async () => {
        const result = await client.get<{ data: unknown }>('/time', 'v2');
        return result.data;
      },
    },
  };
}
