import type { GustoClient } from '../clients/gusto.js';
import type { Contractor, ContractorPayment } from '../types/index.js';

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

    // Get a specific contractor
    get_contractor: async (args: { contractor_id: string }) => {
      const contractor = await client.get<Contractor>(`/contractors/${args.contractor_id}`);
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(contractor, null, 2),
          },
        ],
      };
    },

    // Create a new contractor
    create_contractor: async (args: {
      company_id: string;
      first_name: string;
      last_name: string;
      email: string;
      middle_initial?: string;
      business_name?: string;
      ein?: string;
      ssn?: string;
      start_date?: string;
    }) => {
      const contractor = await client.post<Contractor>(
        `/companies/${args.company_id}/contractors`,
        {
          first_name: args.first_name,
          last_name: args.last_name,
          email: args.email,
          middle_initial: args.middle_initial,
          business_name: args.business_name,
          ein: args.ein,
          ssn: args.ssn,
          start_date: args.start_date,
        }
      );
      return {
        content: [
          {
            type: 'text',
            text: `Contractor created successfully:\n${JSON.stringify(contractor, null, 2)}`,
          },
        ],
      };
    },

    // Update a contractor
    update_contractor: async (args: {
      contractor_id: string;
      version: string;
      first_name?: string;
      last_name?: string;
      email?: string;
      business_name?: string;
      is_active?: boolean;
    }) => {
      const contractor = await client.put<Contractor>(`/contractors/${args.contractor_id}`, {
        version: args.version,
        first_name: args.first_name,
        last_name: args.last_name,
        email: args.email,
        business_name: args.business_name,
        is_active: args.is_active,
      });
      return {
        content: [
          {
            type: 'text',
            text: `Contractor updated successfully:\n${JSON.stringify(contractor, null, 2)}`,
          },
        ],
      };
    },

    // List contractor payments
    list_contractor_payments: async (args: {
      contractor_id: string;
      start_date?: string;
      end_date?: string;
    }) => {
      const params: any = {};
      if (args.start_date) params.start_date = args.start_date;
      if (args.end_date) params.end_date = args.end_date;

      const payments = await client.get<ContractorPayment[]>(
        `/contractors/${args.contractor_id}/payments`,
        params
      );
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(payments, null, 2),
          },
        ],
      };
    },

    // Create a contractor payment
    create_contractor_payment: async (args: {
      contractor_id: string;
      date: string;
      wage?: string;
      hours?: string;
      bonus?: string;
      reimbursement?: string;
    }) => {
      const payment = await client.post<ContractorPayment>(
        `/contractors/${args.contractor_id}/payments`,
        {
          date: args.date,
          wage: args.wage,
          hours: args.hours,
          bonus: args.bonus,
          reimbursement: args.reimbursement,
        }
      );
      return {
        content: [
          {
            type: 'text',
            text: `Contractor payment created successfully:\n${JSON.stringify(payment, null, 2)}`,
          },
        ],
      };
    },
  };
}

export const contractorToolDefinitions = [
  {
    name: 'list_contractors',
    description: 'List all contractors for a company',
    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_contractor',
    description: 'Get detailed information about a specific contractor',
    inputSchema: {
      type: 'object',
      properties: {
        contractor_id: { type: 'string', description: 'Contractor ID' },
      },
      required: ['contractor_id'],
    },
  },
  {
    name: 'create_contractor',
    description: 'Create a new contractor',
    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' },
        middle_initial: { type: 'string', description: 'Middle initial' },
        business_name: { type: 'string', description: 'Business name (if applicable)' },
        ein: { type: 'string', description: 'Employer Identification Number' },
        ssn: { type: 'string', description: 'Social Security Number' },
        start_date: { type: 'string', description: 'Start date (YYYY-MM-DD)' },
      },
      required: ['company_id', 'first_name', 'last_name', 'email'],
    },
  },
  {
    name: 'update_contractor',
    description: 'Update an existing contractor',
    inputSchema: {
      type: 'object',
      properties: {
        contractor_id: { type: 'string', description: 'Contractor 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' },
        business_name: { type: 'string', description: 'Business name' },
        is_active: { type: 'boolean', description: 'Whether contractor is active' },
      },
      required: ['contractor_id', 'version'],
    },
  },
  {
    name: 'list_contractor_payments',
    description: 'List all payments for a contractor',
    inputSchema: {
      type: 'object',
      properties: {
        contractor_id: { type: 'string', description: 'Contractor ID' },
        start_date: { type: 'string', description: 'Filter by start date (YYYY-MM-DD)' },
        end_date: { type: 'string', description: 'Filter by end date (YYYY-MM-DD)' },
      },
      required: ['contractor_id'],
    },
  },
  {
    name: 'create_contractor_payment',
    description: 'Create a new payment for a contractor',
    inputSchema: {
      type: 'object',
      properties: {
        contractor_id: { type: 'string', description: 'Contractor ID' },
        date: { type: 'string', description: 'Payment date (YYYY-MM-DD)' },
        wage: { type: 'string', description: 'Wage amount' },
        hours: { type: 'string', description: 'Hours worked' },
        bonus: { type: 'string', description: 'Bonus amount' },
        reimbursement: { type: 'string', description: 'Reimbursement amount' },
      },
      required: ['contractor_id', 'date'],
    },
  },
];
