import { z } from 'zod';
import type { RipplingClient } from '../clients/rippling.js';

export function createEmployeesTools(client: RipplingClient) {
  return {
    rippling_list_employees: {
      description: 'List all employees with optional filters',
      inputSchema: z.object({
        status: z.enum(['ACTIVE', 'INACTIVE', 'TERMINATED']).optional().describe('Filter by employment status'),
        departmentId: z.string().optional().describe('Filter by department ID'),
        cursor: z.string().optional().describe('Pagination cursor'),
        limit: z.number().min(1).max(500).default(100).optional().describe('Number of results per page'),
      }),
      handler: async (args: any) => {
        const result = await client.listEmployees(args);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    rippling_get_employee: {
      description: 'Get detailed information about a specific employee',
      inputSchema: z.object({
        id: z.string().describe('Employee ID'),
      }),
      handler: async (args: any) => {
        const result = await client.getEmployee(args.id);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    rippling_create_employee: {
      description: 'Create a new employee record',
      inputSchema: z.object({
        firstName: z.string().describe('First name'),
        lastName: z.string().describe('Last name'),
        email: z.string().email().describe('Work email address'),
        personalEmail: z.string().email().optional().describe('Personal email'),
        phoneNumber: z.string().optional().describe('Phone number'),
        title: z.string().optional().describe('Job title'),
        department: z.string().optional().describe('Department ID'),
        manager: z.string().optional().describe('Manager employee ID'),
        startDate: z.string().optional().describe('Start date (YYYY-MM-DD)'),
        employmentType: z.enum(['FULL_TIME', 'PART_TIME', 'CONTRACT', 'INTERN']).optional().describe('Employment type'),
        workLocationId: z.string().optional().describe('Work location ID'),
        compensation: z.object({
          amount: z.number().optional(),
          currency: z.string().default('USD').optional(),
          frequency: z.enum(['HOURLY', 'ANNUALLY', 'MONTHLY']).optional(),
        }).optional().describe('Compensation details'),
        customFields: z.record(z.any()).optional().describe('Custom field values'),
      }),
      handler: async (args: any) => {
        const result = await client.createEmployee(args);
        return {
          content: [
            {
              type: 'text',
              text: `Employee created successfully:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },

    rippling_update_employee: {
      description: 'Update employee information',
      inputSchema: z.object({
        id: z.string().describe('Employee ID'),
        firstName: z.string().optional().describe('First name'),
        lastName: z.string().optional().describe('Last name'),
        email: z.string().email().optional().describe('Work email'),
        phoneNumber: z.string().optional().describe('Phone number'),
        title: z.string().optional().describe('Job title'),
        department: z.string().optional().describe('Department ID'),
        manager: z.string().optional().describe('Manager employee ID'),
        workLocationId: z.string().optional().describe('Work location ID'),
        compensation: z.object({
          amount: z.number().optional(),
          currency: z.string().optional(),
          frequency: z.enum(['HOURLY', 'ANNUALLY', 'MONTHLY']).optional(),
          effectiveDate: z.string().optional(),
        }).optional().describe('Compensation details'),
        customFields: z.record(z.any()).optional().describe('Custom field values'),
      }),
      handler: async (args: any) => {
        const { id, ...data } = args;
        const result = await client.updateEmployee(id, data);
        return {
          content: [
            {
              type: 'text',
              text: `Employee updated successfully:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },

    rippling_terminate_employee: {
      description: 'Terminate an employee',
      inputSchema: z.object({
        id: z.string().describe('Employee ID'),
        terminationDate: z.string().describe('Termination date (YYYY-MM-DD)'),
        reason: z.string().optional().describe('Termination reason'),
      }),
      handler: async (args: any) => {
        const { id, ...data } = args;
        const result = await client.terminateEmployee(id, data);
        return {
          content: [
            {
              type: 'text',
              text: `Employee terminated successfully:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },

    rippling_list_employee_custom_fields: {
      description: 'List all custom fields configured for employees',
      inputSchema: z.object({}),
      handler: async () => {
        const result = await client.listEmployeeCustomFields();
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    rippling_get_org_chart: {
      description: 'Get the organizational chart structure',
      inputSchema: z.object({}),
      handler: async () => {
        const result = await client.getOrgChart();
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },
  };
}
