import type { WrikeClient } from '../clients/wrike.js';
import type { WrikeWorkflow, WrikeCustomStatus } from '../types/index.js';

export function createWorkflowTools(client: WrikeClient) {
  return {
    // List workflows
    wrike_list_workflows: {
      name: 'wrike_list_workflows',
      description: 'List all workflows in the account',
      inputSchema: {
        type: 'object',
        properties: {},
      },
      handler: async () => {
        const response = await client.get<WrikeWorkflow>('/workflows');
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data, null, 2),
            },
          ],
        };
      },
    },

    // Get workflow by ID
    wrike_get_workflow: {
      name: 'wrike_get_workflow',
      description: 'Get a specific workflow by ID',
      inputSchema: {
        type: 'object',
        properties: {
          workflowId: { type: 'string', description: 'Workflow ID (required)' },
        },
        required: ['workflowId'],
      },
      handler: async (params: { workflowId: string }) => {
        const response = await client.get<WrikeWorkflow>(`/workflows/${params.workflowId}`);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Create workflow
    wrike_create_workflow: {
      name: 'wrike_create_workflow',
      description: 'Create a new custom workflow',
      inputSchema: {
        type: 'object',
        properties: {
          name: { type: 'string', description: 'Workflow name (required)' },
        },
        required: ['name'],
      },
      handler: async (params: { name: string }) => {
        const body = { name: params.name };
        const response = await client.post<WrikeWorkflow>('/workflows', body);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Update workflow
    wrike_update_workflow: {
      name: 'wrike_update_workflow',
      description: 'Update an existing workflow',
      inputSchema: {
        type: 'object',
        properties: {
          workflowId: { type: 'string', description: 'Workflow ID (required)' },
          name: { type: 'string', description: 'New workflow name' },
          hidden: { type: 'boolean', description: 'Hide/unhide workflow' },
        },
        required: ['workflowId'],
      },
      handler: async (params: { workflowId: string; [key: string]: unknown }) => {
        const body: Record<string, unknown> = {};
        
        if (params.name) body.name = params.name;
        if (params.hidden !== undefined) body.hidden = params.hidden;
        
        const response = await client.put<WrikeWorkflow>(`/workflows/${params.workflowId}`, body);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // List custom statuses
    wrike_list_custom_statuses: {
      name: 'wrike_list_custom_statuses',
      description: 'List all custom statuses',
      inputSchema: {
        type: 'object',
        properties: {},
      },
      handler: async () => {
        const response = await client.get<WrikeCustomStatus>('/customstatuses');
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data, null, 2),
            },
          ],
        };
      },
    },

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

    // Create custom status
    wrike_create_custom_status: {
      name: 'wrike_create_custom_status',
      description: 'Create a new custom status',
      inputSchema: {
        type: 'object',
        properties: {
          workflowId: { type: 'string', description: 'Workflow ID (required)' },
          name: { type: 'string', description: 'Status name (required)' },
          group: { 
            type: 'string', 
            enum: ['Active', 'Completed', 'Deferred', 'Cancelled'],
            description: 'Status group (required)' 
          },
          color: { type: 'string', description: 'Status color (hex)' },
          hidden: { type: 'boolean', description: 'Hide status' },
        },
        required: ['workflowId', 'name', 'group'],
      },
      handler: async (params: { workflowId: string; name: string; group: string; [key: string]: unknown }) => {
        const body: Record<string, unknown> = {
          name: params.name,
          group: params.group,
        };
        
        if (params.color) body.color = params.color;
        if (params.hidden !== undefined) body.hidden = params.hidden;
        
        const response = await client.post<WrikeCustomStatus>(`/workflows/${params.workflowId}/customstatuses`, body);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Update custom status
    wrike_update_custom_status: {
      name: 'wrike_update_custom_status',
      description: 'Update an existing custom status',
      inputSchema: {
        type: 'object',
        properties: {
          customStatusId: { type: 'string', description: 'Custom status ID (required)' },
          name: { type: 'string', description: 'New status name' },
          color: { type: 'string', description: 'New status color (hex)' },
          hidden: { type: 'boolean', description: 'Hide/unhide status' },
          group: { 
            type: 'string', 
            enum: ['Active', 'Completed', 'Deferred', 'Cancelled'],
            description: 'New status group' 
          },
        },
        required: ['customStatusId'],
      },
      handler: async (params: { customStatusId: string; [key: string]: unknown }) => {
        const body: Record<string, unknown> = {};
        
        if (params.name) body.name = params.name;
        if (params.color) body.color = params.color;
        if (params.hidden !== undefined) body.hidden = params.hidden;
        if (params.group) body.group = params.group;
        
        const response = await client.put<WrikeCustomStatus>(`/customstatuses/${params.customStatusId}`, body);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

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