/**
 * Customer Management Tools
 */

import { z } from 'zod';
import type { TouchBistroApiClient } from '../lib/api-client.js';

export const customerTools = {
  touchbistro_list_customers: {
    description: 'List all customers with loyalty points, visit history, and preferences.',
    parameters: z.object({
      search: z.string().optional().describe('Search by name, email, or phone'),
      page: z.number().optional().describe('Page number'),
      pageSize: z.number().optional().describe('Items per page'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const filter: any = {};
      if (params.search) filter.search = params.search;

      const response = await client.getCustomers({
        page: params.page,
        pageSize: params.pageSize,
        filter,
      });

      if (!response.success) {
        throw new Error(response.error?.message || 'Failed to fetch customers');
      }

      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(response.data, null, 2),
          },
        ],
      };
    },
  },

  touchbistro_get_customer: {
    description: 'Get detailed customer information including loyalty, visit history, and preferences.',
    parameters: z.object({
      customerId: z.string().describe('Customer ID'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.getCustomer(params.customerId);

      if (!response.success) {
        throw new Error(response.error?.message || 'Failed to fetch customer');
      }

      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(response.data, null, 2),
          },
        ],
      };
    },
  },

  touchbistro_create_customer: {
    description: 'Create a new customer profile with contact information and preferences.',
    parameters: z.object({
      firstName: z.string().describe('First name'),
      lastName: z.string().describe('Last name'),
      email: z.string().optional().describe('Email address'),
      phone: z.string().optional().describe('Phone number'),
      birthday: z.string().optional().describe('Birthday (ISO date format)'),
      notes: z.string().optional().describe('Customer notes'),
      dietaryRestrictions: z.array(z.string()).optional().describe('Dietary restrictions'),
      allergies: z.array(z.string()).optional().describe('Allergies'),
      tags: z.array(z.string()).optional().describe('Customer tags/labels'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.createCustomer(params);

      if (!response.success) {
        throw new Error(response.error?.message || 'Failed to create customer');
      }

      return {
        content: [
          {
            type: 'text',
            text: `Customer created successfully!\n\n${JSON.stringify(response.data, null, 2)}`,
          },
        ],
      };
    },
  },

  touchbistro_update_customer: {
    description: 'Update customer information, preferences, or notes.',
    parameters: z.object({
      customerId: z.string().describe('Customer ID'),
      firstName: z.string().optional().describe('First name'),
      lastName: z.string().optional().describe('Last name'),
      email: z.string().optional().describe('Email address'),
      phone: z.string().optional().describe('Phone number'),
      birthday: z.string().optional().describe('Birthday (ISO date format)'),
      notes: z.string().optional().describe('Customer notes'),
      dietaryRestrictions: z.array(z.string()).optional().describe('Dietary restrictions'),
      allergies: z.array(z.string()).optional().describe('Allergies'),
      tags: z.array(z.string()).optional().describe('Customer tags/labels'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const { customerId, ...updates } = params;
      const response = await client.updateCustomer(customerId, updates);

      if (!response.success) {
        throw new Error(response.error?.message || 'Failed to update customer');
      }

      return {
        content: [
          {
            type: 'text',
            text: `Customer updated!\n\n${JSON.stringify(response.data, null, 2)}`,
          },
        ],
      };
    },
  },

  touchbistro_search_customers: {
    description: 'Search customers by name, email, phone, or tags.',
    parameters: z.object({
      query: z.string().describe('Search query'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.searchCustomers(params.query);

      if (!response.success) {
        throw new Error(response.error?.message || 'Failed to search customers');
      }

      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(response.data, null, 2),
          },
        ],
      };
    },
  },

  touchbistro_get_loyalty_transactions: {
    description: 'Get customer loyalty point transaction history.',
    parameters: z.object({
      customerId: z.string().describe('Customer ID'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.getLoyaltyTransactions(params.customerId);

      if (!response.success) {
        throw new Error(response.error?.message || 'Failed to fetch loyalty transactions');
      }

      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(response.data, null, 2),
          },
        ],
      };
    },
  },

  touchbistro_add_loyalty_points: {
    description: 'Add loyalty points to a customer account.',
    parameters: z.object({
      customerId: z.string().describe('Customer ID'),
      points: z.number().describe('Points to add'),
      description: z.string().describe('Description/reason for points'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.addLoyaltyPoints(params.customerId, params.points, params.description);

      if (!response.success) {
        throw new Error(response.error?.message || 'Failed to add loyalty points');
      }

      return {
        content: [
          {
            type: 'text',
            text: `${params.points} loyalty points added!\n\n${JSON.stringify(response.data, null, 2)}`,
          },
        ],
      };
    },
  },

  touchbistro_redeem_loyalty_points: {
    description: 'Redeem loyalty points from a customer account.',
    parameters: z.object({
      customerId: z.string().describe('Customer ID'),
      points: z.number().describe('Points to redeem'),
      description: z.string().describe('Description of redemption'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.redeemLoyaltyPoints(params.customerId, params.points, params.description);

      if (!response.success) {
        throw new Error(response.error?.message || 'Failed to redeem loyalty points');
      }

      return {
        content: [
          {
            type: 'text',
            text: `${params.points} loyalty points redeemed!\n\n${JSON.stringify(response.data, null, 2)}`,
          },
        ],
      };
    },
  },
};
