/**
 * Reservation Management Tools
 */

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

export const reservationTools = {
  touchbistro_list_reservations: {
    description: 'List reservations with filters for date range and status.',
    parameters: z.object({
      dateFrom: z.string().optional().describe('Start date (ISO format)'),
      dateTo: z.string().optional().describe('End date (ISO format)'),
      status: z
        .enum(['pending', 'confirmed', 'seated', 'completed', 'no_show', 'cancelled'])
        .optional()
        .describe('Filter by status'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.getReservations(params);

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

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

  touchbistro_get_reservation: {
    description: 'Get detailed information about a specific reservation.',
    parameters: z.object({
      reservationId: z.string().describe('Reservation ID'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.getReservation(params.reservationId);

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

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

  touchbistro_create_reservation: {
    description: 'Create a new reservation for a customer.',
    parameters: z.object({
      customerId: z.string().optional().describe('Customer ID (if existing customer)'),
      customerName: z.string().describe('Customer name'),
      customerPhone: z.string().optional().describe('Customer phone number'),
      customerEmail: z.string().optional().describe('Customer email'),
      partySize: z.number().describe('Number of guests'),
      date: z.string().describe('Reservation date (ISO format)'),
      time: z.string().describe('Reservation time (HH:mm format)'),
      duration: z.number().optional().describe('Estimated duration in minutes'),
      tableId: z.string().optional().describe('Specific table ID'),
      sectionId: z.string().optional().describe('Preferred section ID'),
      notes: z.string().optional().describe('Reservation notes'),
      specialRequests: z.string().optional().describe('Special requests'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.createReservation(params);

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

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

  touchbistro_update_reservation: {
    description: 'Update reservation details (time, party size, table, etc.).',
    parameters: z.object({
      reservationId: z.string().describe('Reservation ID'),
      partySize: z.number().optional().describe('Updated party size'),
      date: z.string().optional().describe('Updated date (ISO format)'),
      time: z.string().optional().describe('Updated time (HH:mm format)'),
      duration: z.number().optional().describe('Updated duration in minutes'),
      tableId: z.string().optional().describe('Updated table ID'),
      notes: z.string().optional().describe('Updated notes'),
      specialRequests: z.string().optional().describe('Updated special requests'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const { reservationId, ...updates } = params;
      const response = await client.updateReservation(reservationId, updates);

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

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

  touchbistro_confirm_reservation: {
    description: 'Confirm a pending reservation.',
    parameters: z.object({
      reservationId: z.string().describe('Reservation ID'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.confirmReservation(params.reservationId);

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

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

  touchbistro_seat_reservation: {
    description: 'Seat a reservation at a table.',
    parameters: z.object({
      reservationId: z.string().describe('Reservation ID'),
      tableId: z.string().describe('Table ID to seat at'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.seatReservation(params.reservationId, params.tableId);

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

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

  touchbistro_cancel_reservation: {
    description: 'Cancel a reservation.',
    parameters: z.object({
      reservationId: z.string().describe('Reservation ID'),
      reason: z.string().optional().describe('Cancellation reason'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.cancelReservation(params.reservationId, params.reason);

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

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

  touchbistro_mark_no_show: {
    description: 'Mark a reservation as no-show.',
    parameters: z.object({
      reservationId: z.string().describe('Reservation ID'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.markNoShow(params.reservationId);

      if (!response.success) {
        throw new Error(response.error?.message || 'Failed to mark as no-show');
      }

      return {
        content: [
          {
            type: 'text',
            text: `Reservation marked as no-show!\n\n${JSON.stringify(response.data, null, 2)}`,
          },
        ],
      };
    },
  },
};
