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

export function createEmployeesTools(client: CloverClient) {
  return {
    clover_list_employees: {
      description: 'List employees',
      inputSchema: {
        type: 'object',
        properties: {
          filter: {
            type: 'string',
            description: 'Filter expression',
          },
          expand: {
            type: 'string',
            description: 'Comma-separated fields to expand (shifts, roles)',
          },
          limit: {
            type: 'number',
            description: 'Maximum number of employees to return',
          },
        },
      },
      handler: async (args: any) => {
        const employees = await client.fetchPaginated<CloverEmployee>(
          '/employees',
          { filter: args.filter, expand: args.expand },
          args.limit
        );
        return { employees, count: employees.length };
      },
    },

    clover_get_employee: {
      description: 'Get a specific employee by ID',
      inputSchema: {
        type: 'object',
        properties: {
          employeeId: { type: 'string', description: 'Employee ID' },
          expand: {
            type: 'string',
            description: 'Comma-separated fields to expand',
          },
        },
        required: ['employeeId'],
      },
      handler: async (args: any) => {
        return await client.get<CloverEmployee>(`/employees/${args.employeeId}`, {
          expand: args.expand,
        });
      },
    },

    clover_create_employee: {
      description: 'Create a new employee',
      inputSchema: {
        type: 'object',
        properties: {
          name: { type: 'string', description: 'Employee name' },
          nickname: { type: 'string', description: 'Nickname' },
          email: { type: 'string', description: 'Email address' },
          pin: { type: 'string', description: 'PIN code (4-10 digits)' },
          role: { type: 'string', description: 'Employee role' },
        },
        required: ['name', 'role'],
      },
      handler: async (args: any) => {
        return await client.post<CloverEmployee>('/employees', {
          name: args.name,
          nickname: args.nickname,
          email: args.email,
          pin: args.pin,
          role: args.role,
        });
      },
    },

    clover_update_employee: {
      description: 'Update an existing employee',
      inputSchema: {
        type: 'object',
        properties: {
          employeeId: { type: 'string', description: 'Employee ID' },
          name: { type: 'string', description: 'Employee name' },
          nickname: { type: 'string', description: 'Nickname' },
          email: { type: 'string', description: 'Email address' },
          pin: { type: 'string', description: 'PIN code' },
          role: { type: 'string', description: 'Employee role' },
        },
        required: ['employeeId'],
      },
      handler: async (args: any) => {
        const { employeeId, ...updateData } = args;
        return await client.post<CloverEmployee>(`/employees/${employeeId}`, updateData);
      },
    },

    clover_delete_employee: {
      description: 'Delete an employee',
      inputSchema: {
        type: 'object',
        properties: {
          employeeId: { type: 'string', description: 'Employee ID' },
        },
        required: ['employeeId'],
      },
      handler: async (args: any) => {
        await client.delete(`/employees/${args.employeeId}`);
        return { success: true, employeeId: args.employeeId };
      },
    },

    clover_list_employee_roles: {
      description: 'List all employee roles',
      inputSchema: {
        type: 'object',
        properties: {},
      },
      handler: async () => {
        return await client.get('/roles');
      },
    },

    clover_list_employee_shifts: {
      description: 'List shifts for an employee',
      inputSchema: {
        type: 'object',
        properties: {
          employeeId: { type: 'string', description: 'Employee ID' },
          filter: {
            type: 'string',
            description: 'Filter expression (e.g., "inTime>1234567890")',
          },
        },
        required: ['employeeId'],
      },
      handler: async (args: any) => {
        return await client.get(`/employees/${args.employeeId}/shifts`, {
          filter: args.filter,
        });
      },
    },

    clover_create_shift: {
      description: 'Create a shift for an employee',
      inputSchema: {
        type: 'object',
        properties: {
          employeeId: { type: 'string', description: 'Employee ID' },
          inTime: {
            type: 'number',
            description: 'Clock-in timestamp (Unix ms)',
          },
          outTime: {
            type: 'number',
            description: 'Clock-out timestamp (Unix ms, optional)',
          },
        },
        required: ['employeeId', 'inTime'],
      },
      handler: async (args: any) => {
        const { employeeId, ...shiftData } = args;
        return await client.post<CloverShift>(
          `/employees/${employeeId}/shifts`,
          shiftData
        );
      },
    },

    clover_clock_in_employee: {
      description: 'Clock in an employee (start a shift)',
      inputSchema: {
        type: 'object',
        properties: {
          employeeId: { type: 'string', description: 'Employee ID' },
        },
        required: ['employeeId'],
      },
      handler: async (args: any) => {
        return await client.post<CloverShift>(
          `/employees/${args.employeeId}/shifts`,
          { inTime: Date.now() }
        );
      },
    },

    clover_clock_out_employee: {
      description: 'Clock out an employee (end a shift)',
      inputSchema: {
        type: 'object',
        properties: {
          employeeId: { type: 'string', description: 'Employee ID' },
          shiftId: { type: 'string', description: 'Shift ID' },
        },
        required: ['employeeId', 'shiftId'],
      },
      handler: async (args: any) => {
        return await client.post(
          `/employees/${args.employeeId}/shifts/${args.shiftId}`,
          { outTime: Date.now() }
        );
      },
    },
  };
}
