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

export function createATSTools(client: RipplingClient) {
  return {
    rippling_list_candidates: {
      description: 'List all candidates in the ATS',
      inputSchema: z.object({
        jobId: z.string().optional().describe('Filter by job ID'),
        stage: z.string().optional().describe('Filter by current stage'),
        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.listCandidates(args);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    rippling_get_candidate: {
      description: 'Get detailed candidate information',
      inputSchema: z.object({
        id: z.string().describe('Candidate ID'),
      }),
      handler: async (args: any) => {
        const result = await client.getCandidate(args.id);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    rippling_list_jobs: {
      description: 'List all job postings',
      inputSchema: z.object({
        status: z.enum(['DRAFT', 'OPEN', 'CLOSED', 'ON_HOLD']).optional().describe('Filter by job status'),
        department: z.string().optional().describe('Filter by department'),
        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.listJobs(args);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    rippling_get_job: {
      description: 'Get detailed job posting information',
      inputSchema: z.object({
        id: z.string().describe('Job ID'),
      }),
      handler: async (args: any) => {
        const result = await client.getJob(args.id);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    rippling_list_applications: {
      description: 'List job applications',
      inputSchema: z.object({
        candidateId: z.string().optional().describe('Filter by candidate ID'),
        jobId: z.string().optional().describe('Filter by job ID'),
        status: z.enum(['ACTIVE', 'HIRED', 'REJECTED', 'WITHDRAWN']).optional().describe('Filter by status'),
        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.listApplications(args);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(result, null, 2),
            },
          ],
        };
      },
    },

    rippling_update_application_stage: {
      description: 'Move an application to a different stage in the pipeline',
      inputSchema: z.object({
        id: z.string().describe('Application ID'),
        stage: z.string().describe('New stage (e.g., "Phone Screen", "Technical Interview", "Offer")'),
      }),
      handler: async (args: any) => {
        const { id, stage } = args;
        const result = await client.updateApplicationStage(id, stage);
        return {
          content: [
            {
              type: 'text',
              text: `Application stage updated successfully:\n${JSON.stringify(result, null, 2)}`,
            },
          ],
        };
      },
    },
  };
}
