import type { GustoClient } from '../clients/gusto.js';
import type { TimeOffPolicy, TimeOffBalance, TimeOffRequest } from '../types/index.js';

export function createTimeOffTools(client: GustoClient) {
  return {
    // List time off policies for a company
    list_time_off_policies: async (args: { company_id: string }) => {
      const policies = await client.get<TimeOffPolicy[]>(
        `/companies/${args.company_id}/time_off_policies`
      );
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(policies, null, 2),
          },
        ],
      };
    },

    // Get time off balances for an employee
    get_time_off_balances: async (args: { employee_id: string }) => {
      const balances = await client.get<TimeOffBalance[]>(
        `/employees/${args.employee_id}/time_off_balances`
      );
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(balances, null, 2),
          },
        ],
      };
    },

    // List time off requests for an employee
    list_time_off_requests: async (args: { employee_id: string }) => {
      const requests = await client.get<TimeOffRequest[]>(
        `/employees/${args.employee_id}/time_off_requests`
      );
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(requests, null, 2),
          },
        ],
      };
    },

    // Create a time off request
    create_time_off_request: async (args: {
      employee_id: string;
      policy_id: string;
      start_date: string;
      end_date: string;
      hours?: string;
      note?: string;
    }) => {
      const request = await client.post<TimeOffRequest>(
        `/employees/${args.employee_id}/time_off_requests`,
        {
          policy_id: args.policy_id,
          start_date: args.start_date,
          end_date: args.end_date,
          hours: args.hours,
          note: args.note,
        }
      );
      return {
        content: [
          {
            type: 'text',
            text: `Time off request created successfully:\n${JSON.stringify(request, null, 2)}`,
          },
        ],
      };
    },
  };
}

export const timeOffToolDefinitions = [
  {
    name: 'list_time_off_policies',
    description: 'List all time off policies for a company',
    inputSchema: {
      type: 'object',
      properties: {
        company_id: { type: 'string', description: 'Company ID' },
      },
      required: ['company_id'],
    },
  },
  {
    name: 'get_time_off_balances',
    description: 'Get time off balances for an employee across all policies',
    inputSchema: {
      type: 'object',
      properties: {
        employee_id: { type: 'string', description: 'Employee ID' },
      },
      required: ['employee_id'],
    },
  },
  {
    name: 'list_time_off_requests',
    description: 'List all time off requests for an employee',
    inputSchema: {
      type: 'object',
      properties: {
        employee_id: { type: 'string', description: 'Employee ID' },
      },
      required: ['employee_id'],
    },
  },
  {
    name: 'create_time_off_request',
    description: 'Create a new time off request for an employee',
    inputSchema: {
      type: 'object',
      properties: {
        employee_id: { type: 'string', description: 'Employee ID' },
        policy_id: { type: 'string', description: 'Time Off Policy ID' },
        start_date: { type: 'string', description: 'Start date (YYYY-MM-DD)' },
        end_date: { type: 'string', description: 'End date (YYYY-MM-DD)' },
        hours: { type: 'string', description: 'Number of hours (optional)' },
        note: { type: 'string', description: 'Request note or reason' },
      },
      required: ['employee_id', 'policy_id', 'start_date', 'end_date'],
    },
  },
];
