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

export function createCustomFieldTools(client: WrikeClient) {
  return {
    // List custom fields
    wrike_list_custom_fields: {
      name: 'wrike_list_custom_fields',
      description: 'List all custom field definitions',
      inputSchema: {
        type: 'object',
        properties: {},
      },
      handler: async () => {
        const response = await client.get<WrikeCustomFieldDefinition>('/customfields');
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data, null, 2),
            },
          ],
        };
      },
    },

    // Get custom field by ID
    wrike_get_custom_field: {
      name: 'wrike_get_custom_field',
      description: 'Get a specific custom field definition by ID',
      inputSchema: {
        type: 'object',
        properties: {
          customFieldId: { type: 'string', description: 'Custom field ID (required)' },
        },
        required: ['customFieldId'],
      },
      handler: async (params: { customFieldId: string }) => {
        const response = await client.get<WrikeCustomFieldDefinition>(`/customfields/${params.customFieldId}`);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Create custom field
    wrike_create_custom_field: {
      name: 'wrike_create_custom_field',
      description: 'Create a new custom field',
      inputSchema: {
        type: 'object',
        properties: {
          title: { type: 'string', description: 'Field title (required)' },
          type: { 
            type: 'string', 
            enum: ['Text', 'DropDown', 'Numeric', 'Currency', 'Percentage', 'Date', 'Duration', 'Checkbox', 'Contacts', 'Multiple'],
            description: 'Field type (required)' 
          },
          shareds: { type: 'array', items: { type: 'string' }, description: 'Shared folder IDs' },
          values: { type: 'array', items: { type: 'string' }, description: 'Dropdown values (for DropDown/Multiple types)' },
          currency: { type: 'string', description: 'Currency code (for Currency type)' },
          decimalPlaces: { type: 'number', description: 'Decimal places (for Numeric/Currency/Percentage)' },
          allowOtherValues: { type: 'boolean', description: 'Allow other values (for DropDown/Multiple)' },
        },
        required: ['title', 'type'],
      },
      handler: async (params: { title: string; type: string; [key: string]: unknown }) => {
        const body: Record<string, unknown> = {
          title: params.title,
          type: params.type,
        };
        
        if (params.shareds) body.shareds = params.shareds;
        
        const settings: Record<string, unknown> = {};
        if (params.values) settings.values = params.values;
        if (params.currency) settings.currency = params.currency;
        if (params.decimalPlaces !== undefined) settings.decimalPlaces = params.decimalPlaces;
        if (params.allowOtherValues !== undefined) settings.allowOtherValues = params.allowOtherValues;
        
        if (Object.keys(settings).length > 0) {
          body.settings = settings;
        }
        
        const response = await client.post<WrikeCustomFieldDefinition>('/customfields', body);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Update custom field
    wrike_update_custom_field: {
      name: 'wrike_update_custom_field',
      description: 'Update an existing custom field',
      inputSchema: {
        type: 'object',
        properties: {
          customFieldId: { type: 'string', description: 'Custom field ID (required)' },
          title: { type: 'string', description: 'New field title' },
          type: { 
            type: 'string', 
            enum: ['Text', 'DropDown', 'Numeric', 'Currency', 'Percentage', 'Date', 'Duration', 'Checkbox', 'Contacts', 'Multiple'],
            description: 'New field type' 
          },
          addShareds: { type: 'array', items: { type: 'string' }, description: 'Folder IDs to add to shared' },
          removeShareds: { type: 'array', items: { type: 'string' }, description: 'Folder IDs to remove from shared' },
        },
        required: ['customFieldId'],
      },
      handler: async (params: { customFieldId: string; [key: string]: unknown }) => {
        const body: Record<string, unknown> = {};
        
        if (params.title) body.title = params.title;
        if (params.type) body.type = params.type;
        if (params.addShareds) body.addShareds = params.addShareds;
        if (params.removeShareds) body.removeShareds = params.removeShareds;
        
        const response = await client.put<WrikeCustomFieldDefinition>(`/customfields/${params.customFieldId}`, body);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Delete custom field
    wrike_delete_custom_field: {
      name: 'wrike_delete_custom_field',
      description: 'Delete a custom field',
      inputSchema: {
        type: 'object',
        properties: {
          customFieldId: { type: 'string', description: 'Custom field ID (required)' },
        },
        required: ['customFieldId'],
      },
      handler: async (params: { customFieldId: string }) => {
        const response = await client.delete<WrikeCustomFieldDefinition>(`/customfields/${params.customFieldId}`);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },
  };
}
