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

export function getTools(client: QuickBooksClient) {
  return [
    {
      name: 'qbo_list_customers',
      description: 'List all customers 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 Customer';
        if (active !== undefined) {
          query += ` WHERE Active = ${active}`;
        }
        
        return await client.query<Customer>(query, { startPosition, maxResults });
      },
    },
    {
      name: 'qbo_get_customer',
      description: 'Get a specific customer by ID',
      inputSchema: {
        type: 'object',
        properties: {
          id: {
            type: 'string',
            description: 'Customer ID',
          },
        },
        required: ['id'],
      },
      handler: async (args: any) => {
        return await client.read<Customer>('Customer', args.id);
      },
    },
    {
      name: 'qbo_create_customer',
      description: 'Create a new customer',
      inputSchema: {
        type: 'object',
        properties: {
          displayName: {
            type: 'string',
            description: 'Display name for the customer',
          },
          givenName: {
            type: 'string',
            description: 'First name',
          },
          familyName: {
            type: 'string',
            description: 'Last name',
          },
          companyName: {
            type: 'string',
            description: 'Company name',
          },
          primaryEmail: {
            type: 'string',
            description: 'Primary email address',
          },
          primaryPhone: {
            type: 'string',
            description: 'Primary phone number',
          },
          billAddress: {
            type: 'object',
            description: 'Billing address',
            properties: {
              line1: { type: 'string' },
              city: { type: 'string' },
              countrySubDivisionCode: { type: 'string' },
              postalCode: { type: 'string' },
            },
          },
          notes: {
            type: 'string',
            description: 'Customer notes',
          },
        },
        required: ['displayName'],
      },
      handler: async (args: any) => {
        const customer: Partial<Customer> = {
          DisplayName: args.displayName,
        };
        
        if (args.givenName) customer.GivenName = args.givenName;
        if (args.familyName) customer.FamilyName = args.familyName;
        if (args.companyName) customer.CompanyName = args.companyName;
        if (args.primaryEmail) customer.PrimaryEmailAddr = { Address: args.primaryEmail };
        if (args.primaryPhone) customer.PrimaryPhone = { FreeFormNumber: args.primaryPhone };
        if (args.billAddress) customer.BillAddr = args.billAddress;
        if (args.notes) customer.Notes = args.notes;
        
        return await client.create<Customer>('Customer', customer);
      },
    },
    {
      name: 'qbo_update_customer',
      description: 'Update an existing customer (requires SyncToken)',
      inputSchema: {
        type: 'object',
        properties: {
          id: {
            type: 'string',
            description: 'Customer ID',
          },
          syncToken: {
            type: 'string',
            description: 'SyncToken from the customer',
          },
          displayName: {
            type: 'string',
            description: 'Display name',
          },
          givenName: {
            type: 'string',
            description: 'First name',
          },
          familyName: {
            type: 'string',
            description: 'Last name',
          },
          primaryEmail: {
            type: 'string',
            description: 'Primary email address',
          },
          primaryPhone: {
            type: 'string',
            description: 'Primary phone number',
          },
          active: {
            type: 'boolean',
            description: 'Active status',
          },
          notes: {
            type: 'string',
            description: 'Customer notes',
          },
        },
        required: ['id', 'syncToken'],
      },
      handler: async (args: any) => {
        const customer: any = {
          Id: args.id,
          SyncToken: args.syncToken,
        };
        
        if (args.displayName) customer.DisplayName = args.displayName;
        if (args.givenName) customer.GivenName = args.givenName;
        if (args.familyName) customer.FamilyName = args.familyName;
        if (args.primaryEmail) customer.PrimaryEmailAddr = { Address: args.primaryEmail };
        if (args.primaryPhone) customer.PrimaryPhone = { FreeFormNumber: args.primaryPhone };
        if (args.active !== undefined) customer.Active = args.active;
        if (args.notes) customer.Notes = args.notes;
        
        return await client.update<Customer>('Customer', customer);
      },
    },
    {
      name: 'qbo_search_customers',
      description: 'Search customers by name or email',
      inputSchema: {
        type: 'object',
        properties: {
          searchTerm: {
            type: 'string',
            description: 'Search term to match against customer name or email',
          },
          startPosition: {
            type: 'number',
            description: 'Starting position (default 1)',
          },
          maxResults: {
            type: 'number',
            description: 'Maximum results (default 100)',
          },
        },
        required: ['searchTerm'],
      },
      handler: async (args: any) => {
        const { searchTerm, startPosition = 1, maxResults = 100 } = args;
        const query = `SELECT * FROM Customer WHERE DisplayName LIKE '%${searchTerm}%'`;
        
        return await client.query<Customer>(query, { startPosition, maxResults });
      },
    },
    {
      name: 'qbo_query_customers',
      description: 'Run a custom SQL-like query on customers',
      inputSchema: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'SQL-like query (e.g., "SELECT * FROM Customer 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<Customer>(args.query, {
          startPosition: args.startPosition || 1,
          maxResults: args.maxResults || 100,
        });
      },
    },
  ];
}
