import type { WrikeClient } from '../clients/wrike.js';
import type { WrikeContact } from '../types/index.js';

export function createContactTools(client: WrikeClient) {
  return {
    // List contacts
    wrike_list_contacts: {
      name: 'wrike_list_contacts',
      description: 'List all contacts/users in the account',
      inputSchema: {
        type: 'object',
        properties: {
          me: { type: 'boolean', description: 'Get current user only' },
          metadata: { type: 'string', description: 'Metadata filter (JSON)' },
          deleted: { type: 'boolean', description: 'Include deleted users' },
          fields: { type: 'array', items: { type: 'string' }, description: 'Additional fields' },
        },
      },
      handler: async (params: Record<string, unknown>) => {
        const queryParams: Record<string, unknown> = {};
        
        if (params.me !== undefined) queryParams.me = params.me;
        if (params.metadata) queryParams.metadata = params.metadata;
        if (params.deleted !== undefined) queryParams.deleted = params.deleted;
        if (params.fields) queryParams.fields = params.fields;
        
        const response = await client.get<WrikeContact>('/contacts', queryParams);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data, null, 2),
            },
          ],
        };
      },
    },

    // Get contact by ID
    wrike_get_contact: {
      name: 'wrike_get_contact',
      description: 'Get a specific contact/user by ID',
      inputSchema: {
        type: 'object',
        properties: {
          contactId: { type: 'string', description: 'Contact ID (required)' },
          fields: { type: 'array', items: { type: 'string' }, description: 'Additional fields' },
        },
        required: ['contactId'],
      },
      handler: async (params: { contactId: string; fields?: string[] }) => {
        const response = await client.get<WrikeContact>(`/contacts/${params.contactId}`, {
          fields: params.fields,
        });
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Update contact
    wrike_update_contact: {
      name: 'wrike_update_contact',
      description: 'Update contact information',
      inputSchema: {
        type: 'object',
        properties: {
          contactId: { type: 'string', description: 'Contact ID (required)' },
          metadata: { type: 'array', items: { type: 'object' }, description: 'Metadata key-value pairs' },
        },
        required: ['contactId'],
      },
      handler: async (params: { contactId: string; metadata?: Array<{ key: string; value: string }> }) => {
        const body: Record<string, unknown> = {};
        
        if (params.metadata) body.metadata = params.metadata;
        
        const response = await client.put<WrikeContact>(`/contacts/${params.contactId}`, body);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },
  };
}
