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

export function getTools(client: QuickBooksClient) {
  return [
    {
      name: 'qbo_list_vendors',
      description: 'List all vendors 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 Vendor';
        if (active !== undefined) {
          query += ` WHERE Active = ${active}`;
        }
        
        return await client.query<Vendor>(query, { startPosition, maxResults });
      },
    },
    {
      name: 'qbo_get_vendor',
      description: 'Get a specific vendor by ID',
      inputSchema: {
        type: 'object',
        properties: {
          id: {
            type: 'string',
            description: 'Vendor ID',
          },
        },
        required: ['id'],
      },
      handler: async (args: any) => {
        return await client.read<Vendor>('Vendor', args.id);
      },
    },
    {
      name: 'qbo_create_vendor',
      description: 'Create a new vendor',
      inputSchema: {
        type: 'object',
        properties: {
          displayName: {
            type: 'string',
            description: 'Display name for the vendor',
          },
          companyName: {
            type: 'string',
            description: 'Company 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',
          },
          billAddress: {
            type: 'object',
            description: 'Billing address',
            properties: {
              line1: { type: 'string' },
              city: { type: 'string' },
              countrySubDivisionCode: { type: 'string' },
              postalCode: { type: 'string' },
            },
          },
          accountNumber: {
            type: 'string',
            description: 'Account number',
          },
          vendor1099: {
            type: 'boolean',
            description: 'Whether vendor is 1099 eligible',
          },
        },
        required: ['displayName'],
      },
      handler: async (args: any) => {
        const vendor: any = {
          DisplayName: args.displayName,
        };
        
        if (args.companyName) vendor.CompanyName = args.companyName;
        if (args.givenName) vendor.GivenName = args.givenName;
        if (args.familyName) vendor.FamilyName = args.familyName;
        if (args.primaryEmail) vendor.PrimaryEmailAddr = { Address: args.primaryEmail };
        if (args.primaryPhone) vendor.PrimaryPhone = { FreeFormNumber: args.primaryPhone };
        if (args.billAddress) vendor.BillAddr = args.billAddress;
        if (args.accountNumber) vendor.AcctNum = args.accountNumber;
        if (args.vendor1099 !== undefined) vendor.Vendor1099 = args.vendor1099;
        
        return await client.create<Vendor>('Vendor', vendor);
      },
    },
    {
      name: 'qbo_update_vendor',
      description: 'Update an existing vendor (requires SyncToken)',
      inputSchema: {
        type: 'object',
        properties: {
          id: {
            type: 'string',
            description: 'Vendor ID',
          },
          syncToken: {
            type: 'string',
            description: 'SyncToken from the vendor',
          },
          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',
          },
          vendor1099: {
            type: 'boolean',
            description: 'Whether vendor is 1099 eligible',
          },
        },
        required: ['id', 'syncToken'],
      },
      handler: async (args: any) => {
        const vendor: any = {
          Id: args.id,
          SyncToken: args.syncToken,
        };
        
        if (args.displayName) vendor.DisplayName = args.displayName;
        if (args.primaryEmail) vendor.PrimaryEmailAddr = { Address: args.primaryEmail };
        if (args.primaryPhone) vendor.PrimaryPhone = { FreeFormNumber: args.primaryPhone };
        if (args.active !== undefined) vendor.Active = args.active;
        if (args.vendor1099 !== undefined) vendor.Vendor1099 = args.vendor1099;
        
        return await client.update<Vendor>('Vendor', vendor);
      },
    },
    {
      name: 'qbo_search_vendors',
      description: 'Search vendors by name',
      inputSchema: {
        type: 'object',
        properties: {
          searchTerm: {
            type: 'string',
            description: 'Search term to match against vendor name',
          },
          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 Vendor WHERE DisplayName LIKE '%${searchTerm}%'`;
        
        return await client.query<Vendor>(query, { startPosition, maxResults });
      },
    },
    {
      name: 'qbo_query_vendors',
      description: 'Run a custom SQL-like query on vendors',
      inputSchema: {
        type: 'object',
        properties: {
          query: {
            type: 'string',
            description: 'SQL-like query (e.g., "SELECT * FROM Vendor WHERE Vendor1099 = 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<Vendor>(args.query, {
          startPosition: args.startPosition || 1,
          maxResults: args.maxResults || 100,
        });
      },
    },
  ];
}
