import type { GustoClient } from '../clients/gusto.js';
import type { Employee, Job, Termination } from '../types/index.js';

export function createEmployeeTools(client: GustoClient) {
  return {
    // List employees for a company
    list_employees: async (args: { company_id: string; page?: number; per?: number }) => {
      const employees = await client.getPaginated<Employee>(
        `/companies/${args.company_id}/employees`,
        { page: args.page, per: args.per }
      );
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(employees, null, 2),
          },
        ],
      };
    },

    // Get a specific employee
    get_employee: async (args: { employee_id: string }) => {
      const employee = await client.get<Employee>(`/employees/${args.employee_id}`);
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(employee, null, 2),
          },
        ],
      };
    },

    // Create a new employee
    create_employee: async (args: {
      company_id: string;
      first_name: string;
      last_name: string;
      email: string;
      date_of_birth?: string;
      ssn?: string;
      middle_initial?: string;
    }) => {
      const employee = await client.post<Employee>(
        `/companies/${args.company_id}/employees`,
        {
          first_name: args.first_name,
          last_name: args.last_name,
          email: args.email,
          date_of_birth: args.date_of_birth,
          ssn: args.ssn,
          middle_initial: args.middle_initial,
        }
      );
      return {
        content: [
          {
            type: 'text',
            text: `Employee created successfully:\n${JSON.stringify(employee, null, 2)}`,
          },
        ],
      };
    },

    // Update an employee
    update_employee: async (args: {
      employee_id: string;
      version: string;
      first_name?: string;
      last_name?: string;
      email?: string;
      date_of_birth?: string;
      ssn?: string;
    }) => {
      const employee = await client.put<Employee>(`/employees/${args.employee_id}`, {
        version: args.version,
        first_name: args.first_name,
        last_name: args.last_name,
        email: args.email,
        date_of_birth: args.date_of_birth,
        ssn: args.ssn,
      });
      return {
        content: [
          {
            type: 'text',
            text: `Employee updated successfully:\n${JSON.stringify(employee, null, 2)}`,
          },
        ],
      };
    },

    // Terminate an employee
    terminate_employee: async (args: {
      employee_id: string;
      effective_date: string;
      run_termination_payroll?: boolean;
    }) => {
      const termination = await client.post<Termination>(
        `/employees/${args.employee_id}/terminations`,
        {
          effective_date: args.effective_date,
          run_termination_payroll: args.run_termination_payroll,
        }
      );
      return {
        content: [
          {
            type: 'text',
            text: `Employee terminated successfully:\n${JSON.stringify(termination, null, 2)}`,
          },
        ],
      };
    },

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

    // Get a specific job
    get_job: async (args: { job_id: string }) => {
      const job = await client.get<Job>(`/jobs/${args.job_id}`);
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(job, null, 2),
          },
        ],
      };
    },

    // Create a job for an employee
    create_job: async (args: {
      employee_id: string;
      title: string;
      hire_date: string;
      location_id?: string;
      rate?: string;
      payment_unit?: 'Hour' | 'Week' | 'Month' | 'Year' | 'Paycheck';
    }) => {
      const job = await client.post<Job>(`/employees/${args.employee_id}/jobs`, {
        title: args.title,
        hire_date: args.hire_date,
        location_id: args.location_id,
        rate: args.rate,
        payment_unit: args.payment_unit,
      });
      return {
        content: [
          {
            type: 'text',
            text: `Job created successfully:\n${JSON.stringify(job, null, 2)}`,
          },
        ],
      };
    },

    // Update a job
    update_job: async (args: {
      job_id: string;
      version: string;
      title?: string;
      location_id?: string;
      rate?: string;
      payment_unit?: 'Hour' | 'Week' | 'Month' | 'Year' | 'Paycheck';
    }) => {
      const job = await client.put<Job>(`/jobs/${args.job_id}`, {
        version: args.version,
        title: args.title,
        location_id: args.location_id,
        rate: args.rate,
        payment_unit: args.payment_unit,
      });
      return {
        content: [
          {
            type: 'text',
            text: `Job updated successfully:\n${JSON.stringify(job, null, 2)}`,
          },
        ],
      };
    },

    // Get employee onboarding status
    get_onboarding_status: async (args: { employee_id: string }) => {
      const employee = await client.get<Employee>(`/employees/${args.employee_id}`);
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(
              {
                employee_id: employee.id,
                onboarded: employee.onboarded,
                email: employee.email,
                has_ssn: !!employee.ssn,
                has_dob: !!employee.date_of_birth,
                has_address: !!employee.home_address,
                jobs_count: employee.jobs?.length || 0,
              },
              null,
              2
            ),
          },
        ],
      };
    },
  };
}

export const employeeToolDefinitions = [
  {
    name: 'list_employees',
    description: 'List all employees for a company with pagination support',
    inputSchema: {
      type: 'object',
      properties: {
        company_id: { type: 'string', description: 'Company ID' },
        page: { type: 'number', description: 'Page number (default: 1)' },
        per: { type: 'number', description: 'Results per page (default: 25)' },
      },
      required: ['company_id'],
    },
  },
  {
    name: 'get_employee',
    description: 'Get detailed information about a specific employee',
    inputSchema: {
      type: 'object',
      properties: {
        employee_id: { type: 'string', description: 'Employee ID' },
      },
      required: ['employee_id'],
    },
  },
  {
    name: 'create_employee',
    description: 'Create a new employee',
    inputSchema: {
      type: 'object',
      properties: {
        company_id: { type: 'string', description: 'Company ID' },
        first_name: { type: 'string', description: 'First name' },
        last_name: { type: 'string', description: 'Last name' },
        email: { type: 'string', description: 'Email address' },
        date_of_birth: { type: 'string', description: 'Date of birth (YYYY-MM-DD)' },
        ssn: { type: 'string', description: 'Social Security Number' },
        middle_initial: { type: 'string', description: 'Middle initial' },
      },
      required: ['company_id', 'first_name', 'last_name', 'email'],
    },
  },
  {
    name: 'update_employee',
    description: 'Update an existing employee',
    inputSchema: {
      type: 'object',
      properties: {
        employee_id: { type: 'string', description: 'Employee ID' },
        version: { type: 'string', description: 'Version for optimistic locking' },
        first_name: { type: 'string', description: 'First name' },
        last_name: { type: 'string', description: 'Last name' },
        email: { type: 'string', description: 'Email address' },
        date_of_birth: { type: 'string', description: 'Date of birth (YYYY-MM-DD)' },
        ssn: { type: 'string', description: 'Social Security Number' },
      },
      required: ['employee_id', 'version'],
    },
  },
  {
    name: 'terminate_employee',
    description: 'Terminate an employee',
    inputSchema: {
      type: 'object',
      properties: {
        employee_id: { type: 'string', description: 'Employee ID' },
        effective_date: { type: 'string', description: 'Termination date (YYYY-MM-DD)' },
        run_termination_payroll: {
          type: 'boolean',
          description: 'Whether to run a termination payroll',
        },
      },
      required: ['employee_id', 'effective_date'],
    },
  },
  {
    name: 'list_employee_jobs',
    description: 'List all jobs for an employee',
    inputSchema: {
      type: 'object',
      properties: {
        employee_id: { type: 'string', description: 'Employee ID' },
      },
      required: ['employee_id'],
    },
  },
  {
    name: 'get_job',
    description: 'Get detailed information about a specific job',
    inputSchema: {
      type: 'object',
      properties: {
        job_id: { type: 'string', description: 'Job ID' },
      },
      required: ['job_id'],
    },
  },
  {
    name: 'create_job',
    description: 'Create a new job for an employee',
    inputSchema: {
      type: 'object',
      properties: {
        employee_id: { type: 'string', description: 'Employee ID' },
        title: { type: 'string', description: 'Job title' },
        hire_date: { type: 'string', description: 'Hire date (YYYY-MM-DD)' },
        location_id: { type: 'string', description: 'Location ID' },
        rate: { type: 'string', description: 'Pay rate' },
        payment_unit: {
          type: 'string',
          enum: ['Hour', 'Week', 'Month', 'Year', 'Paycheck'],
          description: 'Payment unit',
        },
      },
      required: ['employee_id', 'title', 'hire_date'],
    },
  },
  {
    name: 'update_job',
    description: 'Update an existing job',
    inputSchema: {
      type: 'object',
      properties: {
        job_id: { type: 'string', description: 'Job ID' },
        version: { type: 'string', description: 'Version for optimistic locking' },
        title: { type: 'string', description: 'Job title' },
        location_id: { type: 'string', description: 'Location ID' },
        rate: { type: 'string', description: 'Pay rate' },
        payment_unit: {
          type: 'string',
          enum: ['Hour', 'Week', 'Month', 'Year', 'Paycheck'],
          description: 'Payment unit',
        },
      },
      required: ['job_id', 'version'],
    },
  },
  {
    name: 'get_onboarding_status',
    description: 'Get the onboarding status and completeness for an employee',
    inputSchema: {
      type: 'object',
      properties: {
        employee_id: { type: 'string', description: 'Employee ID' },
      },
      required: ['employee_id'],
    },
  },
];
