import { CloverClient } from '../clients/clover.js';
import { CloverShift } from '../types/index.js';

export function createShiftsTools(client: CloverClient) {
  return {
    clover_list_shifts: {
      description: 'List all shifts',
      inputSchema: {
        type: 'object',
        properties: {
          filter: {
            type: 'string',
            description: 'Filter expression (e.g., "employee.id=ABC123")',
          },
          expand: {
            type: 'string',
            description: 'Comma-separated fields to expand',
          },
          limit: {
            type: 'number',
            description: 'Maximum number of shifts to return',
          },
        },
      },
      handler: async (args: any) => {
        const shifts = await client.fetchPaginated<CloverShift>(
          '/shifts',
          { filter: args.filter, expand: args.expand },
          args.limit
        );
        return { shifts, count: shifts.length };
      },
    },

    clover_get_shift: {
      description: 'Get a specific shift by ID',
      inputSchema: {
        type: 'object',
        properties: {
          shiftId: { type: 'string', description: 'Shift ID' },
          expand: { type: 'string', description: 'Comma-separated fields to expand' },
        },
        required: ['shiftId'],
      },
      handler: async (args: any) => {
        return await client.get<CloverShift>(`/shifts/${args.shiftId}`, {
          expand: args.expand,
        });
      },
    },

    clover_create_shift: {
      description: 'Create a new shift (clock in)',
      inputSchema: {
        type: 'object',
        properties: {
          employeeId: { type: 'string', description: 'Employee ID' },
          inTime: {
            type: 'number',
            description: 'Clock in time (Unix timestamp in ms), defaults to now',
          },
          serverBanking: {
            type: 'boolean',
            description: 'Whether server banking is enabled',
          },
        },
        required: ['employeeId'],
      },
      handler: async (args: any) => {
        return await client.post<CloverShift>('/shifts', {
          employee: { id: args.employeeId },
          inTime: args.inTime || Date.now(),
          serverBanking: args.serverBanking,
        });
      },
    },

    clover_update_shift: {
      description: 'Update an existing shift',
      inputSchema: {
        type: 'object',
        properties: {
          shiftId: { type: 'string', description: 'Shift ID' },
          outTime: {
            type: 'number',
            description: 'Clock out time (Unix timestamp in ms)',
          },
          cashTipsCollected: {
            type: 'number',
            description: 'Cash tips collected in cents',
          },
          serverBanking: { type: 'boolean', description: 'Server banking flag' },
        },
        required: ['shiftId'],
      },
      handler: async (args: any) => {
        const { shiftId, ...updateData } = args;
        return await client.post<CloverShift>(`/shifts/${shiftId}`, updateData);
      },
    },

    clover_clock_out: {
      description: 'Clock out an employee (end their shift)',
      inputSchema: {
        type: 'object',
        properties: {
          shiftId: { type: 'string', description: 'Shift ID' },
          outTime: {
            type: 'number',
            description: 'Clock out time (Unix timestamp in ms), defaults to now',
          },
        },
        required: ['shiftId'],
      },
      handler: async (args: any) => {
        return await client.post<CloverShift>(`/shifts/${args.shiftId}`, {
          outTime: args.outTime || Date.now(),
        });
      },
    },

    clover_delete_shift: {
      description: 'Delete a shift',
      inputSchema: {
        type: 'object',
        properties: {
          shiftId: { type: 'string', description: 'Shift ID' },
        },
        required: ['shiftId'],
      },
      handler: async (args: any) => {
        await client.delete(`/shifts/${args.shiftId}`);
        return { success: true, shiftId: args.shiftId };
      },
    },

    clover_list_employee_shifts: {
      description: 'List all shifts for a specific employee',
      inputSchema: {
        type: 'object',
        properties: {
          employeeId: { type: 'string', description: 'Employee ID' },
          startTime: {
            type: 'number',
            description: 'Filter shifts starting after this time',
          },
          endTime: {
            type: 'number',
            description: 'Filter shifts ending before this time',
          },
        },
        required: ['employeeId'],
      },
      handler: async (args: any) => {
        let filter = `employee.id=${args.employeeId}`;
        if (args.startTime) {
          filter += `&inTime>=${args.startTime}`;
        }
        if (args.endTime) {
          filter += `&outTime<=${args.endTime}`;
        }

        const shifts = await client.fetchPaginated<CloverShift>('/shifts', { filter });
        return { shifts, count: shifts.length };
      },
    },

    clover_get_active_shifts: {
      description: 'Get all currently active (clocked in) shifts',
      inputSchema: {
        type: 'object',
        properties: {},
      },
      handler: async () => {
        // Shifts without outTime are active
        const allShifts = await client.fetchPaginated<CloverShift>('/shifts', {});
        const activeShifts = allShifts.filter(shift => !shift.outTime);
        return { shifts: activeShifts, count: activeShifts.length };
      },
    },
  };
}
