import { CloverClient } from '../clients/clover.js';
import { CloverCustomer, CloverAddress, CloverEmailAddress, CloverPhoneNumber } from '../types/index.js';

export function createCustomersTools(client: CloverClient) {
  return {
    clover_list_customers: {
      description: 'List customers',
      inputSchema: {
        type: 'object',
        properties: {
          filter: {
            type: 'string',
            description: 'Filter expression',
          },
          expand: {
            type: 'string',
            description: 'Comma-separated fields to expand (addresses, emailAddresses, phoneNumbers, cards)',
          },
          limit: {
            type: 'number',
            description: 'Maximum number of customers to return',
          },
        },
      },
      handler: async (args: any) => {
        const customers = await client.fetchPaginated<CloverCustomer>(
          '/customers',
          { filter: args.filter, expand: args.expand },
          args.limit
        );
        return { customers, count: customers.length };
      },
    },

    clover_get_customer: {
      description: 'Get a specific customer by ID',
      inputSchema: {
        type: 'object',
        properties: {
          customerId: { type: 'string', description: 'Customer ID' },
          expand: {
            type: 'string',
            description: 'Comma-separated fields to expand',
          },
        },
        required: ['customerId'],
      },
      handler: async (args: any) => {
        return await client.get<CloverCustomer>(`/customers/${args.customerId}`, {
          expand: args.expand,
        });
      },
    },

    clover_create_customer: {
      description: 'Create a new customer',
      inputSchema: {
        type: 'object',
        properties: {
          firstName: { type: 'string', description: 'First name' },
          lastName: { type: 'string', description: 'Last name' },
          marketingAllowed: {
            type: 'boolean',
            description: 'Marketing opt-in',
          },
        },
        required: ['firstName', 'lastName'],
      },
      handler: async (args: any) => {
        return await client.post<CloverCustomer>('/customers', {
          firstName: args.firstName,
          lastName: args.lastName,
          marketingAllowed: args.marketingAllowed,
        });
      },
    },

    clover_update_customer: {
      description: 'Update an existing customer',
      inputSchema: {
        type: 'object',
        properties: {
          customerId: { type: 'string', description: 'Customer ID' },
          firstName: { type: 'string', description: 'First name' },
          lastName: { type: 'string', description: 'Last name' },
          marketingAllowed: {
            type: 'boolean',
            description: 'Marketing opt-in',
          },
        },
        required: ['customerId'],
      },
      handler: async (args: any) => {
        const { customerId, ...updateData } = args;
        return await client.post<CloverCustomer>(`/customers/${customerId}`, updateData);
      },
    },

    clover_delete_customer: {
      description: 'Delete a customer',
      inputSchema: {
        type: 'object',
        properties: {
          customerId: { type: 'string', description: 'Customer ID' },
        },
        required: ['customerId'],
      },
      handler: async (args: any) => {
        await client.delete(`/customers/${args.customerId}`);
        return { success: true, customerId: args.customerId };
      },
    },

    clover_search_customers: {
      description: 'Search customers by name, phone, or email',
      inputSchema: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'Search query (name, phone, or email)',
          },
        },
        required: ['query'],
      },
      handler: async (args: any) => {
        const customers = await client.fetchPaginated<CloverCustomer>(
          '/customers',
          { filter: `firstName~'${args.query}' OR lastName~'${args.query}'` }
        );
        return { customers, count: customers.length };
      },
    },

    clover_list_customer_addresses: {
      description: 'List addresses for a customer',
      inputSchema: {
        type: 'object',
        properties: {
          customerId: { type: 'string', description: 'Customer ID' },
        },
        required: ['customerId'],
      },
      handler: async (args: any) => {
        return await client.get(`/customers/${args.customerId}/addresses`);
      },
    },

    clover_add_customer_address: {
      description: 'Add an address to a customer',
      inputSchema: {
        type: 'object',
        properties: {
          customerId: { type: 'string', description: 'Customer ID' },
          address1: { type: 'string', description: 'Address line 1' },
          address2: { type: 'string', description: 'Address line 2' },
          city: { type: 'string', description: 'City' },
          state: { type: 'string', description: 'State/Province' },
          zip: { type: 'string', description: 'ZIP/Postal code' },
          country: { type: 'string', description: 'Country code' },
        },
        required: ['customerId', 'address1', 'city', 'state', 'zip'],
      },
      handler: async (args: any) => {
        const { customerId, ...addressData } = args;
        return await client.post<CloverAddress>(
          `/customers/${customerId}/addresses`,
          addressData
        );
      },
    },

    clover_list_customer_cards: {
      description: 'List payment cards for a customer',
      inputSchema: {
        type: 'object',
        properties: {
          customerId: { type: 'string', description: 'Customer ID' },
        },
        required: ['customerId'],
      },
      handler: async (args: any) => {
        return await client.get(`/customers/${args.customerId}/cards`);
      },
    },
  };
}
