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

export function createBenefitTools(client: GustoClient) {
  return {
    // List company benefits
    list_company_benefits: async (args: { company_id: string }) => {
      const benefits = await client.get<CompanyBenefit[]>(
        `/companies/${args.company_id}/company_benefits`
      );
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(benefits, null, 2),
          },
        ],
      };
    },

    // Get a specific company benefit
    get_company_benefit: async (args: { company_benefit_id: string }) => {
      const benefit = await client.get<CompanyBenefit>(
        `/company_benefits/${args.company_benefit_id}`
      );
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(benefit, null, 2),
          },
        ],
      };
    },

    // Create a company benefit
    create_company_benefit: async (args: {
      company_id: string;
      benefit_type: string;
      description?: string;
      responsible_for_employer_taxes?: boolean;
      responsible_for_employee_w2?: boolean;
    }) => {
      const benefit = await client.post<CompanyBenefit>(
        `/companies/${args.company_id}/company_benefits`,
        {
          benefit_type: args.benefit_type,
          description: args.description,
          responsible_for_employer_taxes: args.responsible_for_employer_taxes,
          responsible_for_employee_w2: args.responsible_for_employee_w2,
        }
      );
      return {
        content: [
          {
            type: 'text',
            text: `Company benefit created successfully:\n${JSON.stringify(benefit, null, 2)}`,
          },
        ],
      };
    },

    // Update a company benefit
    update_company_benefit: async (args: {
      company_benefit_id: string;
      version: string;
      description?: string;
      active?: boolean;
    }) => {
      const benefit = await client.put<CompanyBenefit>(
        `/company_benefits/${args.company_benefit_id}`,
        {
          version: args.version,
          description: args.description,
          active: args.active,
        }
      );
      return {
        content: [
          {
            type: 'text',
            text: `Company benefit updated successfully:\n${JSON.stringify(benefit, null, 2)}`,
          },
        ],
      };
    },

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

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

    // Create an employee benefit enrollment
    create_employee_benefit: async (args: {
      employee_id: string;
      company_benefit_id: string;
      employee_deduction?: string;
      company_contribution?: string;
      deduct_as_percentage?: boolean;
      contribute_as_percentage?: boolean;
      coverage_amount?: string;
    }) => {
      const benefit = await client.post<EmployeeBenefit>(
        `/employees/${args.employee_id}/employee_benefits`,
        {
          company_benefit_id: args.company_benefit_id,
          employee_deduction: args.employee_deduction,
          company_contribution: args.company_contribution,
          deduct_as_percentage: args.deduct_as_percentage,
          contribute_as_percentage: args.contribute_as_percentage,
          coverage_amount: args.coverage_amount,
        }
      );
      return {
        content: [
          {
            type: 'text',
            text: `Employee benefit enrollment created successfully:\n${JSON.stringify(
              benefit,
              null,
              2
            )}`,
          },
        ],
      };
    },

    // Update an employee benefit enrollment
    update_employee_benefit: async (args: {
      employee_benefit_id: string;
      version: string;
      employee_deduction?: string;
      company_contribution?: string;
      active?: boolean;
      coverage_amount?: string;
    }) => {
      const benefit = await client.put<EmployeeBenefit>(
        `/employee_benefits/${args.employee_benefit_id}`,
        {
          version: args.version,
          employee_deduction: args.employee_deduction,
          company_contribution: args.company_contribution,
          active: args.active,
          coverage_amount: args.coverage_amount,
        }
      );
      return {
        content: [
          {
            type: 'text',
            text: `Employee benefit updated successfully:\n${JSON.stringify(benefit, null, 2)}`,
          },
        ],
      };
    },

    // List available benefit types
    list_benefit_types: async () => {
      // This would typically return supported benefit types from Gusto
      const benefitTypes = {
        retirement: ['401k', 'Simple IRA', 'Roth 401k'],
        health: ['Health Insurance', 'Dental Insurance', 'Vision Insurance'],
        other: [
          'Health Savings Account',
          'Flexible Spending Account',
          'Life Insurance',
          'Disability Insurance',
          'Commuter Benefits',
        ],
      };
      return {
        content: [
          {
            type: 'text',
            text: JSON.stringify(benefitTypes, null, 2),
          },
        ],
      };
    },
  };
}

export const benefitToolDefinitions = [
  {
    name: 'list_company_benefits',
    description: 'List all benefits offered by a company',
    inputSchema: {
      type: 'object',
      properties: {
        company_id: { type: 'string', description: 'Company ID' },
      },
      required: ['company_id'],
    },
  },
  {
    name: 'get_company_benefit',
    description: 'Get detailed information about a specific company benefit',
    inputSchema: {
      type: 'object',
      properties: {
        company_benefit_id: { type: 'string', description: 'Company Benefit ID' },
      },
      required: ['company_benefit_id'],
    },
  },
  {
    name: 'create_company_benefit',
    description: 'Create a new benefit offering for a company',
    inputSchema: {
      type: 'object',
      properties: {
        company_id: { type: 'string', description: 'Company ID' },
        benefit_type: { type: 'string', description: 'Type of benefit (e.g., 401k, Health Insurance)' },
        description: { type: 'string', description: 'Benefit description' },
        responsible_for_employer_taxes: {
          type: 'boolean',
          description: 'Whether company is responsible for employer taxes',
        },
        responsible_for_employee_w2: {
          type: 'boolean',
          description: 'Whether benefit should appear on W2',
        },
      },
      required: ['company_id', 'benefit_type'],
    },
  },
  {
    name: 'update_company_benefit',
    description: 'Update an existing company benefit',
    inputSchema: {
      type: 'object',
      properties: {
        company_benefit_id: { type: 'string', description: 'Company Benefit ID' },
        version: { type: 'string', description: 'Version for optimistic locking' },
        description: { type: 'string', description: 'Benefit description' },
        active: { type: 'boolean', description: 'Whether the benefit is active' },
      },
      required: ['company_benefit_id', 'version'],
    },
  },
  {
    name: 'list_employee_benefits',
    description: 'List all benefit enrollments for an employee',
    inputSchema: {
      type: 'object',
      properties: {
        employee_id: { type: 'string', description: 'Employee ID' },
      },
      required: ['employee_id'],
    },
  },
  {
    name: 'get_employee_benefit',
    description: 'Get detailed information about a specific employee benefit enrollment',
    inputSchema: {
      type: 'object',
      properties: {
        employee_benefit_id: { type: 'string', description: 'Employee Benefit ID' },
      },
      required: ['employee_benefit_id'],
    },
  },
  {
    name: 'create_employee_benefit',
    description: 'Enroll an employee in a company benefit',
    inputSchema: {
      type: 'object',
      properties: {
        employee_id: { type: 'string', description: 'Employee ID' },
        company_benefit_id: { type: 'string', description: 'Company Benefit ID' },
        employee_deduction: { type: 'string', description: 'Employee contribution amount' },
        company_contribution: { type: 'string', description: 'Company contribution amount' },
        deduct_as_percentage: {
          type: 'boolean',
          description: 'Whether employee deduction is a percentage',
        },
        contribute_as_percentage: {
          type: 'boolean',
          description: 'Whether company contribution is a percentage',
        },
        coverage_amount: { type: 'string', description: 'Coverage amount (for insurance)' },
      },
      required: ['employee_id', 'company_benefit_id'],
    },
  },
  {
    name: 'update_employee_benefit',
    description: 'Update an employee benefit enrollment',
    inputSchema: {
      type: 'object',
      properties: {
        employee_benefit_id: { type: 'string', description: 'Employee Benefit ID' },
        version: { type: 'string', description: 'Version for optimistic locking' },
        employee_deduction: { type: 'string', description: 'Employee contribution amount' },
        company_contribution: { type: 'string', description: 'Company contribution amount' },
        active: { type: 'boolean', description: 'Whether the enrollment is active' },
        coverage_amount: { type: 'string', description: 'Coverage amount (for insurance)' },
      },
      required: ['employee_benefit_id', 'version'],
    },
  },
  {
    name: 'list_benefit_types',
    description: 'List all available benefit types that can be created',
    inputSchema: {
      type: 'object',
      properties: {},
    },
  },
];
