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

export function getTools(client: QuickBooksClient) {
  return [
    {
      name: 'qbo_list_time_activities',
      description: 'List time activities with optional filters',
      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)',
          },
          employeeId: {
            type: 'string',
            description: 'Filter by employee ID',
          },
          customerId: {
            type: 'string',
            description: 'Filter by customer ID',
          },
        },
      },
      handler: async (args: any) => {
        const { startPosition = 1, maxResults = 100, employeeId, customerId } = args;
        
        let query = 'SELECT * FROM TimeActivity';
        const conditions: string[] = [];
        
        if (employeeId) conditions.push(`EmployeeRef = '${employeeId}'`);
        if (customerId) conditions.push(`CustomerRef = '${customerId}'`);
        
        if (conditions.length > 0) {
          query += ' WHERE ' + conditions.join(' AND ');
        }
        
        return await client.query<TimeActivity>(query, { startPosition, maxResults });
      },
    },
    {
      name: 'qbo_get_time_activity',
      description: 'Get a specific time activity by ID',
      inputSchema: {
        type: 'object',
        properties: {
          id: {
            type: 'string',
            description: 'Time activity ID',
          },
        },
        required: ['id'],
      },
      handler: async (args: any) => {
        return await client.read<TimeActivity>('TimeActivity', args.id);
      },
    },
    {
      name: 'qbo_create_time_activity',
      description: 'Create a new time activity',
      inputSchema: {
        type: 'object',
        properties: {
          employeeId: {
            type: 'string',
            description: 'Employee reference ID',
          },
          customerId: {
            type: 'string',
            description: 'Customer reference ID (optional)',
          },
          itemId: {
            type: 'string',
            description: 'Service item reference ID (optional)',
          },
          hours: {
            type: 'number',
            description: 'Number of hours',
          },
          minutes: {
            type: 'number',
            description: 'Number of minutes',
          },
          txnDate: {
            type: 'string',
            description: 'Transaction date (YYYY-MM-DD)',
          },
          description: {
            type: 'string',
            description: 'Description of work performed',
          },
          hourlyRate: {
            type: 'number',
            description: 'Hourly rate (optional)',
          },
          billableStatus: {
            type: 'string',
            description: 'Billable status: Billable, NotBillable, HasBeenBilled',
          },
        },
        required: ['employeeId', 'txnDate'],
      },
      handler: async (args: any) => {
        const timeActivity: any = {
          EmployeeRef: { value: args.employeeId },
          TxnDate: args.txnDate,
          NameOf: 'Employee',
        };
        
        if (args.customerId) timeActivity.CustomerRef = { value: args.customerId };
        if (args.itemId) timeActivity.ItemRef = { value: args.itemId };
        if (args.hours !== undefined) timeActivity.Hours = args.hours;
        if (args.minutes !== undefined) timeActivity.Minutes = args.minutes;
        if (args.description) timeActivity.Description = args.description;
        if (args.hourlyRate !== undefined) timeActivity.HourlyRate = args.hourlyRate;
        if (args.billableStatus) timeActivity.BillableStatus = args.billableStatus;
        
        return await client.create<TimeActivity>('TimeActivity', timeActivity);
      },
    },
    {
      name: 'qbo_update_time_activity',
      description: 'Update an existing time activity (requires SyncToken)',
      inputSchema: {
        type: 'object',
        properties: {
          id: {
            type: 'string',
            description: 'Time activity ID',
          },
          syncToken: {
            type: 'string',
            description: 'SyncToken from the time activity',
          },
          hours: {
            type: 'number',
            description: 'Number of hours',
          },
          minutes: {
            type: 'number',
            description: 'Number of minutes',
          },
          description: {
            type: 'string',
            description: 'Description of work performed',
          },
          billableStatus: {
            type: 'string',
            description: 'Billable status: Billable, NotBillable, HasBeenBilled',
          },
        },
        required: ['id', 'syncToken'],
      },
      handler: async (args: any) => {
        const timeActivity: any = {
          Id: args.id,
          SyncToken: args.syncToken,
        };
        
        if (args.hours !== undefined) timeActivity.Hours = args.hours;
        if (args.minutes !== undefined) timeActivity.Minutes = args.minutes;
        if (args.description) timeActivity.Description = args.description;
        if (args.billableStatus) timeActivity.BillableStatus = args.billableStatus;
        
        return await client.update<TimeActivity>('TimeActivity', timeActivity);
      },
    },
    {
      name: 'qbo_delete_time_activity',
      description: 'Delete a time activity (requires SyncToken)',
      inputSchema: {
        type: 'object',
        properties: {
          id: {
            type: 'string',
            description: 'Time activity ID',
          },
          syncToken: {
            type: 'string',
            description: 'SyncToken from the time activity',
          },
        },
        required: ['id', 'syncToken'],
      },
      handler: async (args: any) => {
        return await client.delete('TimeActivity', args.id, args.syncToken);
      },
    },
  ];
}
