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

export function createTimeTools(client: RipplingClient) {
  return {
    rippling_list_time_entries: {
      description: 'List time entries (clock in/out records)',
      inputSchema: z.object({
        employeeId: z.string().optional().describe('Filter by employee ID'),
        startDate: z.string().optional().describe('Filter by start date (YYYY-MM-DD)'),
        endDate: z.string().optional().describe('Filter by end date (YYYY-MM-DD)'),
        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.listTimeEntries(args);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    rippling_create_time_entry: {
      description: 'Create a new time entry',
      inputSchema: z.object({
        employeeId: z.string().describe('Employee ID'),
        date: z.string().describe('Date (YYYY-MM-DD)'),
        clockIn: z.string().optional().describe('Clock in time (ISO 8601)'),
        clockOut: z.string().optional().describe('Clock out time (ISO 8601)'),
        hours: z.number().optional().describe('Total hours worked'),
        breakMinutes: z.number().optional().describe('Break duration in minutes'),
        notes: z.string().optional().describe('Additional notes'),
      }),
      handler: async (args: any) => {
        const result = await client.createTimeEntry(args);
        return {
          content: [
            {
              type: 'text',
              text: `Time entry created successfully:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },

    rippling_update_time_entry: {
      description: 'Update an existing time entry',
      inputSchema: z.object({
        id: z.string().describe('Time entry ID'),
        clockIn: z.string().optional().describe('Clock in time (ISO 8601)'),
        clockOut: z.string().optional().describe('Clock out time (ISO 8601)'),
        hours: z.number().optional().describe('Total hours worked'),
        breakMinutes: z.number().optional().describe('Break duration in minutes'),
        notes: z.string().optional().describe('Additional notes'),
      }),
      handler: async (args: any) => {
        const { id, ...data } = args;
        const result = await client.updateTimeEntry(id, data);
        return {
          content: [
            {
              type: 'text',
              text: `Time entry updated successfully:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },

    rippling_delete_time_entry: {
      description: 'Delete a time entry',
      inputSchema: z.object({
        id: z.string().describe('Time entry ID'),
      }),
      handler: async (args: any) => {
        const result = await client.deleteTimeEntry(args.id);
        return {
          content: [
            {
              type: 'text',
              text: `Time entry deleted successfully:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },

    rippling_get_timesheet: {
      description: 'Get a timesheet for a specific period',
      inputSchema: z.object({
        id: z.string().describe('Timesheet ID'),
      }),
      handler: async (args: any) => {
        const result = await client.getTimesheet(args.id);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    rippling_approve_timesheet: {
      description: 'Approve a timesheet',
      inputSchema: z.object({
        id: z.string().describe('Timesheet ID'),
      }),
      handler: async (args: any) => {
        const result = await client.approveTimesheet(args.id);
        return {
          content: [
            {
              type: 'text',
              text: `Timesheet approved successfully:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },

    rippling_list_time_off_requests: {
      description: 'List time off (PTO/vacation) requests',
      inputSchema: z.object({
        employeeId: z.string().optional().describe('Filter by employee ID'),
        status: z.enum(['PENDING', 'APPROVED', 'DENIED', 'CANCELLED']).optional().describe('Filter by status'),
        startDate: z.string().optional().describe('Filter by start date (YYYY-MM-DD)'),
        endDate: z.string().optional().describe('Filter by end date (YYYY-MM-DD)'),
        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.listTimeOffRequests(args);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    rippling_create_time_off_request: {
      description: 'Create a new time off request',
      inputSchema: z.object({
        employeeId: z.string().describe('Employee ID'),
        type: z.string().describe('Time off type (e.g., "Vacation", "Sick Leave", "Personal")'),
        startDate: z.string().describe('Start date (YYYY-MM-DD)'),
        endDate: z.string().describe('End date (YYYY-MM-DD)'),
        days: z.number().optional().describe('Number of days'),
        hours: z.number().optional().describe('Number of hours'),
        reason: z.string().optional().describe('Reason for time off'),
      }),
      handler: async (args: any) => {
        const result = await client.createTimeOffRequest(args);
        return {
          content: [
            {
              type: 'text',
              text: `Time off request created successfully:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },

    rippling_approve_time_off_request: {
      description: 'Approve a time off request',
      inputSchema: z.object({
        id: z.string().describe('Time off request ID'),
      }),
      handler: async (args: any) => {
        const result = await client.approveTimeOffRequest(args.id);
        return {
          content: [
            {
              type: 'text',
              text: `Time off request approved successfully:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },

    rippling_deny_time_off_request: {
      description: 'Deny a time off request',
      inputSchema: z.object({
        id: z.string().describe('Time off request ID'),
        reason: z.string().optional().describe('Reason for denial'),
      }),
      handler: async (args: any) => {
        const { id, reason } = args;
        const result = await client.denyTimeOffRequest(id, reason);
        return {
          content: [
            {
              type: 'text',
              text: `Time off request denied:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },
  };
}
