/**
 * Staff Management Tools
 */

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

export const staffTools = {
  touchbistro_list_staff: {
    description: 'List all staff members with roles, active status, and assigned sections.',
    parameters: z.object({
      role: z
        .enum(['owner', 'manager', 'server', 'bartender', 'host', 'chef', 'cook', 'busser', 'cashier'])
        .optional()
        .describe('Filter by role'),
      active: z.boolean().optional().describe('Filter by active status'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const filter: any = {};
      if (params.role) filter.role = params.role;
      if (params.active !== undefined) filter.active = params.active;

      const response = await client.getStaff({ filter });

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

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

  touchbistro_get_staff_member: {
    description: 'Get detailed information about a specific staff member.',
    parameters: z.object({
      staffId: z.string().describe('Staff member ID'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.getStaffMember(params.staffId);

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

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

  touchbistro_create_staff_member: {
    description: 'Create a new staff member with role, permissions, and wage information.',
    parameters: z.object({
      employeeNumber: z.string().describe('Employee number'),
      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'),
      role: z
        .enum(['owner', 'manager', 'server', 'bartender', 'host', 'chef', 'cook', 'busser', 'cashier'])
        .describe('Staff role'),
      pinCode: z.string().describe('POS PIN code'),
      hireDate: z.string().describe('Hire date (ISO format)'),
      wage: z.number().optional().describe('Wage amount'),
      wageType: z.enum(['hourly', 'salary', 'tipped_hourly']).optional().describe('Wage type'),
      assignedSections: z.array(z.string()).optional().describe('Assigned section IDs'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.createStaffMember(params);

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

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

  touchbistro_update_staff_member: {
    description: 'Update staff member details, role, or wage information.',
    parameters: z.object({
      staffId: z.string().describe('Staff member 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'),
      role: z
        .enum(['owner', 'manager', 'server', 'bartender', 'host', 'chef', 'cook', 'busser', 'cashier'])
        .optional()
        .describe('Staff role'),
      wage: z.number().optional().describe('Wage amount'),
      wageType: z.enum(['hourly', 'salary', 'tipped_hourly']).optional().describe('Wage type'),
      assignedSections: z.array(z.string()).optional().describe('Assigned section IDs'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const { staffId, ...updates } = params;
      const response = await client.updateStaffMember(staffId, updates);

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

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

  touchbistro_deactivate_staff_member: {
    description: 'Deactivate a staff member (termination/resignation).',
    parameters: z.object({
      staffId: z.string().describe('Staff member ID'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.deactivateStaffMember(params.staffId);

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

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

  touchbistro_clock_in: {
    description: 'Clock in a staff member to start their shift.',
    parameters: z.object({
      staffId: z.string().describe('Staff member ID'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.clockIn(params.staffId);

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

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

  touchbistro_clock_out: {
    description: 'Clock out a staff member to end their shift.',
    parameters: z.object({
      staffId: z.string().describe('Staff member ID'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.clockOut(params.staffId);

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

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

  touchbistro_get_timesheets: {
    description: 'Get staff timesheets with hours, wages, and tips for a date range.',
    parameters: z.object({
      staffId: z.string().optional().describe('Filter by staff member ID'),
      dateFrom: z.string().optional().describe('Start date (ISO format)'),
      dateTo: z.string().optional().describe('End date (ISO format)'),
    }),
    handler: async (params: any, client: TouchBistroApiClient) => {
      const response = await client.getTimeSheets(params);

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

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