/**
 * BigCommerce Coupons Tools
 */

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

export function registerCouponsTools(client: BigCommerceClient) {
  return [
    {
      name: 'bigcommerce_list_coupons',
      description: 'List all coupons from BigCommerce store with optional filters',
      inputSchema: {
        type: 'object',
        properties: {
          code: { type: 'string', description: 'Filter by coupon code' },
          name: { type: 'string', description: 'Filter by coupon name' },
          type: { 
            type: 'string',
            enum: ['per_item_discount', 'per_total_discount', 'shipping_discount', 'free_shipping', 'percentage_discount'],
            description: 'Filter by coupon type' 
          },
        },
      },
      handler: async (args: any) => {
        const coupons = await client.get(`/coupons?${new URLSearchParams(args).toString()}`, "v2");
        return { content: [{ type: 'text', text: JSON.stringify(coupons, null, 2) }] };
      },
    },
    {
      name: 'bigcommerce_get_coupon',
      description: 'Get detailed information about a specific coupon',
      inputSchema: {
        type: 'object',
        properties: {
          coupon_id: { type: 'number', description: 'Coupon ID' },
        },
        required: ['coupon_id'],
      },
      handler: async (args: any) => {
        const coupon = await client.get(`/coupons/${args.coupon_id}`, "v2");
        return { content: [{ type: 'text', text: JSON.stringify(coupon, null, 2) }] };
      },
    },
    {
      name: 'bigcommerce_create_coupon',
      description: 'Create a new coupon in BigCommerce',
      inputSchema: {
        type: 'object',
        properties: {
          name: { type: 'string', description: 'Coupon name (internal)' },
          code: { type: 'string', description: 'Coupon code (what customers enter)' },
          type: { 
            type: 'string',
            enum: ['per_item_discount', 'per_total_discount', 'shipping_discount', 'free_shipping', 'percentage_discount'],
            description: 'Coupon type' 
          },
          amount: { type: 'number', description: 'Discount amount (dollars or percentage)' },
          min_purchase: { type: 'number', description: 'Minimum purchase amount required' },
          expires: { type: 'string', description: 'Expiration date (RFC 2822 or ISO 8601)' },
          enabled: { type: 'boolean', description: 'Is coupon enabled' },
          max_uses: { type: 'number', description: 'Maximum total uses' },
          max_uses_per_customer: { type: 'number', description: 'Maximum uses per customer' },
        },
        required: ['name', 'code', 'type', 'amount'],
      },
      handler: async (args: any) => {
        const coupon = await client.post(`/coupons`, args, "v2");
        return { content: [{ type: 'text', text: JSON.stringify(coupon, null, 2) }] };
      },
    },
    {
      name: 'bigcommerce_update_coupon',
      description: 'Update an existing coupon',
      inputSchema: {
        type: 'object',
        properties: {
          coupon_id: { type: 'number', description: 'Coupon ID' },
          name: { type: 'string', description: 'Coupon name (internal)' },
          code: { type: 'string', description: 'Coupon code' },
          type: { 
            type: 'string',
            enum: ['per_item_discount', 'per_total_discount', 'shipping_discount', 'free_shipping', 'percentage_discount'],
            description: 'Coupon type' 
          },
          amount: { type: 'number', description: 'Discount amount' },
          min_purchase: { type: 'number', description: 'Minimum purchase amount required' },
          expires: { type: 'string', description: 'Expiration date' },
          enabled: { type: 'boolean', description: 'Is coupon enabled' },
          max_uses: { type: 'number', description: 'Maximum total uses' },
          max_uses_per_customer: { type: 'number', description: 'Maximum uses per customer' },
        },
        required: ['coupon_id'],
      },
      handler: async (args: any) => {
        const { coupon_id, ...updateData } = args;
        const coupon = await client.put(`/coupons/${coupon_id}`, updateData, "v2");
        return { content: [{ type: 'text', text: JSON.stringify(coupon, null, 2) }] };
      },
    },
    {
      name: 'bigcommerce_delete_coupon',
      description: 'Delete a coupon from BigCommerce',
      inputSchema: {
        type: 'object',
        properties: {
          coupon_id: { type: 'number', description: 'Coupon ID to delete' },
        },
        required: ['coupon_id'],
      },
      handler: async (args: any) => {
        await client.delete(`/coupons/${args.coupon_id}`, "v2");
        return { content: [{ type: 'text', text: `Coupon ${args.coupon_id} deleted successfully` }] };
      },
    },
  ];
}
