import type { GustoClient } from '../clients/gusto.js';
import type { Compensation } from '../types/index.js';

export function createCompensationTools(client: GustoClient) {
  return {
    // List compensations for a job
    list_compensations: async (args: { job_id: string }) => {
      const compensations = await client.get<Compensation[]>(
        `/jobs/${args.job_id}/compensations`
      );
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(compensations, null, 2),
          },
        ],
      };
    },

    // Get a specific compensation
    get_compensation: async (args: { compensation_id: string }) => {
      const compensation = await client.get<Compensation>(
        `/compensations/${args.compensation_id}`
      );
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(compensation, null, 2),
          },
        ],
      };
    },

    // Create a new compensation
    create_compensation: async (args: {
      job_id: string;
      rate: string;
      payment_unit: 'Hour' | 'Week' | 'Month' | 'Year' | 'Paycheck';
      flsa_status?: 'Exempt' | 'Nonexempt';
      effective_date: string;
    }) => {
      const compensation = await client.post<Compensation>(
        `/jobs/${args.job_id}/compensations`,
        {
          rate: args.rate,
          payment_unit: args.payment_unit,
          flsa_status: args.flsa_status,
          effective_date: args.effective_date,
        }
      );
      return {
        content: [
          {
            type: 'text',
            text: `Compensation created successfully:\n${JSON.stringify(
              compensation,
              null,
              2
            )}`,
          },
        ],
      };
    },

    // Update a compensation
    update_compensation: async (args: {
      compensation_id: string;
      version: string;
      rate?: string;
      payment_unit?: 'Hour' | 'Week' | 'Month' | 'Year' | 'Paycheck';
      flsa_status?: 'Exempt' | 'Nonexempt';
    }) => {
      const compensation = await client.put<Compensation>(
        `/compensations/${args.compensation_id}`,
        {
          version: args.version,
          rate: args.rate,
          payment_unit: args.payment_unit,
          flsa_status: args.flsa_status,
        }
      );
      return {
        content: [
          {
            type: 'text',
            text: `Compensation updated successfully:\n${JSON.stringify(
              compensation,
              null,
              2
            )}`,
          },
        ],
      };
    },
  };
}

export const compensationToolDefinitions = [
  {
    name: 'list_compensations',
    description: 'List all compensation records for a job',
    inputSchema: {
      type: 'object',
      properties: {
        job_id: { type: 'string', description: 'Job ID' },
      },
      required: ['job_id'],
    },
  },
  {
    name: 'get_compensation',
    description: 'Get detailed information about a specific compensation',
    inputSchema: {
      type: 'object',
      properties: {
        compensation_id: { type: 'string', description: 'Compensation ID' },
      },
      required: ['compensation_id'],
    },
  },
  {
    name: 'create_compensation',
    description: 'Create a new compensation record for a job',
    inputSchema: {
      type: 'object',
      properties: {
        job_id: { type: 'string', description: 'Job ID' },
        rate: { type: 'string', description: 'Pay rate amount' },
        payment_unit: {
          type: 'string',
          enum: ['Hour', 'Week', 'Month', 'Year', 'Paycheck'],
          description: 'Payment frequency',
        },
        flsa_status: {
          type: 'string',
          enum: ['Exempt', 'Nonexempt'],
          description: 'FLSA overtime exemption status',
        },
        effective_date: {
          type: 'string',
          description: 'Effective date for compensation (YYYY-MM-DD)',
        },
      },
      required: ['job_id', 'rate', 'payment_unit', 'effective_date'],
    },
  },
  {
    name: 'update_compensation',
    description: 'Update an existing compensation record',
    inputSchema: {
      type: 'object',
      properties: {
        compensation_id: { type: 'string', description: 'Compensation ID' },
        version: { type: 'string', description: 'Version for optimistic locking' },
        rate: { type: 'string', description: 'Pay rate amount' },
        payment_unit: {
          type: 'string',
          enum: ['Hour', 'Week', 'Month', 'Year', 'Paycheck'],
          description: 'Payment frequency',
        },
        flsa_status: {
          type: 'string',
          enum: ['Exempt', 'Nonexempt'],
          description: 'FLSA overtime exemption status',
        },
      },
      required: ['compensation_id', 'version'],
    },
  },
];
