import type { BigCommerceClient } from '../clients/bigcommerce.js';
import type { Promotion, GiftCertificate, Wishlist } from '../types/index.js';

export function registerPromotionTools(client: BigCommerceClient) {
  return {
    // Promotions
    bigcommerce_list_promotions: {
      description: 'List all promotions (discounts, coupons, automatic promotions)',
      parameters: {
        type: 'object' as const,
        properties: {
          status: { type: 'string', enum: ['enabled', 'disabled', 'invalid'], description: 'Filter by status' },
          redemption_type: { type: 'string', description: 'Filter by redemption type (coupon, automatic)' },
          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.status) queryParams.status = params.status as string;
        if (params.redemption_type) queryParams.redemption_type = params.redemption_type as string;

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

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

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

    bigcommerce_create_promotion: {
      description: 'Create a new promotion',
      parameters: {
        type: 'object' as const,
        properties: {
          name: { type: 'string', description: 'Promotion name', required: true },
          redemption_type: { type: 'string', enum: ['coupon', 'automatic'], description: 'Redemption type', required: true },
          status: { type: 'string', enum: ['enabled', 'disabled'], description: 'Promotion status (default: enabled)' },
          start_date: { type: 'string', description: 'Start date (ISO 8601)', required: true },
          end_date: { type: 'string', description: 'End date (ISO 8601)' },
          max_uses: { type: 'number', description: 'Maximum total uses' },
          stop: { type: 'boolean', description: 'Stop processing other promotions if this applies' },
        },
        required: ['name', 'redemption_type', 'start_date'],
      },
      handler: async (params: Partial<Promotion>) => {
        const result = await client.post<{ data: Promotion }>('/promotions', params);
        return result.data;
      },
    },

    bigcommerce_update_promotion: {
      description: 'Update a promotion',
      parameters: {
        type: 'object' as const,
        properties: {
          promotion_id: { type: 'number', description: 'Promotion ID', required: true },
          name: { type: 'string', description: 'Promotion name' },
          status: { type: 'string', enum: ['enabled', 'disabled'], description: 'Promotion status' },
          start_date: { type: 'string', description: 'Start date (ISO 8601)' },
          end_date: { type: 'string', description: 'End date (ISO 8601)' },
        },
        required: ['promotion_id'],
      },
      handler: async (params: { promotion_id: number; [key: string]: unknown }) => {
        const { promotion_id, ...updateData } = params;
        const result = await client.put<{ data: Promotion }>(`/promotions/${promotion_id}`, updateData);
        return result.data;
      },
    },

    bigcommerce_delete_promotion: {
      description: 'Delete a promotion',
      parameters: {
        type: 'object' as const,
        properties: {
          promotion_id: { type: 'number', description: 'Promotion ID', required: true },
        },
        required: ['promotion_id'],
      },
      handler: async (params: { promotion_id: number }) => {
        await client.delete(`/promotions/${params.promotion_id}`);
        return { success: true, message: `Promotion ${params.promotion_id} deleted` };
      },
    },

    // Coupons (V2 API for legacy support)
    bigcommerce_list_coupons: {
      description: 'List all coupon codes',
      parameters: {
        type: 'object' as const,
        properties: {
          code: { type: 'string', description: 'Filter by coupon code' },
          type: { type: 'string', enum: ['per_item_discount', 'percentage_discount', 'per_total_discount', 'shipping_discount', 'free_shipping'], description: 'Filter by coupon type' },
          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.code) queryParams.code = params.code as string;
        if (params.type) queryParams.type = params.type as string;

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

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

    bigcommerce_get_coupon: {
      description: 'Get a specific coupon by ID',
      parameters: {
        type: 'object' as const,
        properties: {
          coupon_id: { type: 'number', description: 'Coupon ID', required: true },
        },
        required: ['coupon_id'],
      },
      handler: async (params: { coupon_id: number }) => {
        const result = await client.get<unknown>(`/coupons/${params.coupon_id}`, 'v2');
        return result;
      },
    },

    bigcommerce_create_coupon: {
      description: 'Create a new coupon code',
      parameters: {
        type: 'object' as const,
        properties: {
          name: { type: 'string', description: 'Coupon name (internal)', required: true },
          code: { type: 'string', description: 'Coupon code (what customer enters)', required: true },
          type: { type: 'string', enum: ['per_item_discount', 'percentage_discount', 'per_total_discount', 'shipping_discount', 'free_shipping'], description: 'Coupon type', required: true },
          amount: { type: 'string', description: 'Discount amount', required: true },
          enabled: { type: 'boolean', description: 'Enabled status (default: true)' },
          min_purchase: { type: 'string', description: 'Minimum purchase amount' },
          max_uses: { type: 'number', description: 'Maximum total uses' },
          max_uses_per_customer: { type: 'number', description: 'Maximum uses per customer' },
          expires: { type: 'string', description: 'Expiration date (ISO 8601)' },
        },
        required: ['name', 'code', 'type', 'amount'],
      },
      handler: async (params: unknown) => {
        const result = await client.post<unknown>('/coupons', params, 'v2');
        return result;
      },
    },

    bigcommerce_update_coupon: {
      description: 'Update a coupon',
      parameters: {
        type: 'object' as const,
        properties: {
          coupon_id: { type: 'number', description: 'Coupon ID', required: true },
          name: { type: 'string', description: 'Coupon name' },
          enabled: { type: 'boolean', description: 'Enabled status' },
          max_uses: { type: 'number', description: 'Maximum total uses' },
        },
        required: ['coupon_id'],
      },
      handler: async (params: { coupon_id: number; [key: string]: unknown }) => {
        const { coupon_id, ...updateData } = params;
        const result = await client.put<unknown>(`/coupons/${coupon_id}`, updateData, 'v2');
        return result;
      },
    },

    bigcommerce_delete_coupon: {
      description: 'Delete a coupon',
      parameters: {
        type: 'object' as const,
        properties: {
          coupon_id: { type: 'number', description: 'Coupon ID', required: true },
        },
        required: ['coupon_id'],
      },
      handler: async (params: { coupon_id: number }) => {
        await client.delete(`/coupons/${params.coupon_id}`, 'v2');
        return { success: true, message: `Coupon ${params.coupon_id} deleted` };
      },
    },

    // Gift Certificates
    bigcommerce_list_gift_certificates: {
      description: 'List all gift certificates',
      parameters: {
        type: 'object' as const,
        properties: {
          code: { type: 'string', description: 'Filter by gift certificate code' },
          to_email: { type: 'string', description: 'Filter by recipient email' },
          status: { type: 'string', enum: ['active', 'pending', 'disabled', 'expired'], 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.code) queryParams.code = params.code as string;
        if (params.to_email) queryParams.to_email = params.to_email 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<GiftCertificate>('/gift-certificates', page, limit, queryParams);
        return { gift_certificates: result.data, meta: result.meta };
      },
    },

    bigcommerce_get_gift_certificate: {
      description: 'Get a specific gift certificate by ID',
      parameters: {
        type: 'object' as const,
        properties: {
          gift_certificate_id: { type: 'number', description: 'Gift certificate ID', required: true },
        },
        required: ['gift_certificate_id'],
      },
      handler: async (params: { gift_certificate_id: number }) => {
        const result = await client.get<{ data: GiftCertificate }>(`/gift-certificates/${params.gift_certificate_id}`);
        return result.data;
      },
    },

    bigcommerce_create_gift_certificate: {
      description: 'Create a new gift certificate',
      parameters: {
        type: 'object' as const,
        properties: {
          to_name: { type: 'string', description: 'Recipient name', required: true },
          to_email: { type: 'string', description: 'Recipient email', required: true },
          from_name: { type: 'string', description: 'Sender name', required: true },
          from_email: { type: 'string', description: 'Sender email', required: true },
          amount: { type: 'string', description: 'Gift certificate amount', required: true },
          message: { type: 'string', description: 'Personal message' },
          template: { type: 'string', description: 'Template name (Birthday, Boy, Celebration, Christmas, General, Girl)' },
        },
        required: ['to_name', 'to_email', 'from_name', 'from_email', 'amount'],
      },
      handler: async (params: Partial<GiftCertificate>) => {
        const result = await client.post<{ data: GiftCertificate }>('/gift-certificates', params);
        return result.data;
      },
    },

    bigcommerce_update_gift_certificate: {
      description: 'Update a gift certificate',
      parameters: {
        type: 'object' as const,
        properties: {
          gift_certificate_id: { type: 'number', description: 'Gift certificate ID', required: true },
          status: { type: 'string', enum: ['active', 'pending', 'disabled', 'expired'], description: 'Status' },
        },
        required: ['gift_certificate_id'],
      },
      handler: async (params: { gift_certificate_id: number; [key: string]: unknown }) => {
        const { gift_certificate_id, ...updateData } = params;
        const result = await client.put<{ data: GiftCertificate }>(`/gift-certificates/${gift_certificate_id}`, updateData);
        return result.data;
      },
    },

    bigcommerce_delete_gift_certificate: {
      description: 'Delete a gift certificate',
      parameters: {
        type: 'object' as const,
        properties: {
          gift_certificate_id: { type: 'number', description: 'Gift certificate ID', required: true },
        },
        required: ['gift_certificate_id'],
      },
      handler: async (params: { gift_certificate_id: number }) => {
        await client.delete(`/gift-certificates/${params.gift_certificate_id}`);
        return { success: true, message: `Gift certificate ${params.gift_certificate_id} deleted` };
      },
    },

    // Wishlists
    bigcommerce_list_wishlists: {
      description: 'List all customer wishlists',
      parameters: {
        type: 'object' as const,
        properties: {
          customer_id: { type: 'number', description: 'Filter by customer ID' },
          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.customer_id) queryParams.customer_id = params.customer_id as number;

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

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

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

    bigcommerce_add_wishlist_item: {
      description: 'Add an item to a wishlist',
      parameters: {
        type: 'object' as const,
        properties: {
          wishlist_id: { type: 'number', description: 'Wishlist ID', required: true },
          product_id: { type: 'number', description: 'Product ID', required: true },
          variant_id: { type: 'number', description: 'Variant ID (if applicable)' },
        },
        required: ['wishlist_id', 'product_id'],
      },
      handler: async (params: { wishlist_id: number; product_id: number; variant_id?: number }) => {
        const { wishlist_id, ...itemData } = params;
        const result = await client.post<{ data: Wishlist }>(`/wishlists/${wishlist_id}/items`, { items: [itemData] });
        return result.data;
      },
    },

    bigcommerce_delete_wishlist_item: {
      description: 'Delete an item from a wishlist',
      parameters: {
        type: 'object' as const,
        properties: {
          wishlist_id: { type: 'number', description: 'Wishlist ID', required: true },
          item_id: { type: 'number', description: 'Wishlist item ID', required: true },
        },
        required: ['wishlist_id', 'item_id'],
      },
      handler: async (params: { wishlist_id: number; item_id: number }) => {
        await client.delete(`/wishlists/${params.wishlist_id}/items/${params.item_id}`);
        return { success: true, message: `Item ${params.item_id} removed from wishlist` };
      },
    },
  };
}
