import { QuickBooksClient } from '../clients/quickbooks.js';
import type { Employee } from '../types/index.js';

export function getTools(client: QuickBooksClient) {
  return [
    {
      name: 'qbo_list_employees',
      description: 'List all employees with pagination',
      inputSchema: {
        type: 'object',
        properties: {
          startPosition: {
            type: 'number',
            description: 'Starting position (1-indexed, default 1)',
          },
          maxResults: {
            type: 'number',
            description: 'Maximum results to return (max 1000, default 100)',
          },
          active: {
            type: 'boolean',
            description: 'Filter by active status',
          },
        },
      },
      handler: async (args: any) => {
        const { startPosition = 1, maxResults = 100, active } = args;
        
        let query = 'SELECT * FROM Employee';
        if (active !== undefined) {
          query += ` WHERE Active = ${active}`;
        }
        
        return await client.query<Employee>(query, { startPosition, maxResults });
      },
    },
    {
      name: 'qbo_get_employee',
      description: 'Get a specific employee by ID',
      inputSchema: {
        type: 'object',
        properties: {
          id: {
            type: 'string',
            description: 'Employee ID',
          },
        },
        required: ['id'],
      },
      handler: async (args: any) => {
        return await client.read<Employee>('Employee', args.id);
      },
    },
    {
      name: 'qbo_create_employee',
      description: 'Create a new employee',
      inputSchema: {
        type: 'object',
        properties: {
          givenName: {
            type: 'string',
            description: 'First name',
          },
          familyName: {
            type: 'string',
            description: 'Last name',
          },
          displayName: {
            type: 'string',
            description: 'Display name (optional, auto-generated if not provided)',
          },
          primaryEmail: {
            type: 'string',
            description: 'Primary email address',
          },
          primaryPhone: {
            type: 'string',
            description: 'Primary phone number',
          },
          hiredDate: {
            type: 'string',
            description: 'Hire date (YYYY-MM-DD)',
          },
          employeeNumber: {
            type: 'string',
            description: 'Employee number',
          },
          ssn: {
            type: 'string',
            description: 'Social Security Number (encrypted)',
          },
          billableTime: {
            type: 'boolean',
            description: 'Whether employee can be billed for time',
          },
        },
        required: ['givenName', 'familyName'],
      },
      handler: async (args: any) => {
        const employee: any = {
          GivenName: args.givenName,
          FamilyName: args.familyName,
        };
        
        if (args.displayName) employee.DisplayName = args.displayName;
        if (args.primaryEmail) employee.PrimaryEmailAddr = { Address: args.primaryEmail };
        if (args.primaryPhone) employee.PrimaryPhone = { FreeFormNumber: args.primaryPhone };
        if (args.hiredDate) employee.HiredDate = args.hiredDate;
        if (args.employeeNumber) employee.EmployeeNumber = args.employeeNumber;
        if (args.ssn) employee.SSN = args.ssn;
        if (args.billableTime !== undefined) employee.BillableTime = args.billableTime;
        
        return await client.create<Employee>('Employee', employee);
      },
    },
    {
      name: 'qbo_update_employee',
      description: 'Update an existing employee (requires SyncToken)',
      inputSchema: {
        type: 'object',
        properties: {
          id: {
            type: 'string',
            description: 'Employee ID',
          },
          syncToken: {
            type: 'string',
            description: 'SyncToken from the employee',
          },
          displayName: {
            type: 'string',
            description: 'Display name',
          },
          primaryEmail: {
            type: 'string',
            description: 'Primary email address',
          },
          primaryPhone: {
            type: 'string',
            description: 'Primary phone number',
          },
          active: {
            type: 'boolean',
            description: 'Active status',
          },
          billableTime: {
            type: 'boolean',
            description: 'Whether employee can be billed for time',
          },
        },
        required: ['id', 'syncToken'],
      },
      handler: async (args: any) => {
        const employee: any = {
          Id: args.id,
          SyncToken: args.syncToken,
        };
        
        if (args.displayName) employee.DisplayName = args.displayName;
        if (args.primaryEmail) employee.PrimaryEmailAddr = { Address: args.primaryEmail };
        if (args.primaryPhone) employee.PrimaryPhone = { FreeFormNumber: args.primaryPhone };
        if (args.active !== undefined) employee.Active = args.active;
        if (args.billableTime !== undefined) employee.BillableTime = args.billableTime;
        
        return await client.update<Employee>('Employee', employee);
      },
    },
    {
      name: 'qbo_query_employees',
      description: 'Run a custom SQL-like query on employees',
      inputSchema: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'SQL-like query (e.g., "SELECT * FROM Employee WHERE Active = true")',
          },
          startPosition: {
            type: 'number',
            description: 'Starting position (default 1)',
          },
          maxResults: {
            type: 'number',
            description: 'Maximum results (default 100)',
          },
        },
        required: ['query'],
      },
      handler: async (args: any) => {
        return await client.query<Employee>(args.query, {
          startPosition: args.startPosition || 1,
          maxResults: args.maxResults || 100,
        });
      },
    },
  ];
}
