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

export function createSpaceTools(client: WrikeClient) {
  return {
    // List spaces
    wrike_list_spaces: {
      name: 'wrike_list_spaces',
      description: 'List all spaces in the account',
      inputSchema: {
        type: 'object',
        properties: {
          withArchived: { type: 'boolean', description: 'Include archived spaces' },
        },
      },
      handler: async (params: { withArchived?: boolean }) => {
        const response = await client.get<WrikeSpace>('/spaces', {
          withArchived: params.withArchived,
        });
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data, null, 2),
            },
          ],
        };
      },
    },

    // Get space by ID
    wrike_get_space: {
      name: 'wrike_get_space',
      description: 'Get a specific space by ID',
      inputSchema: {
        type: 'object',
        properties: {
          spaceId: { type: 'string', description: 'Space ID (required)' },
        },
        required: ['spaceId'],
      },
      handler: async (params: { spaceId: string }) => {
        const response = await client.get<WrikeSpace>(`/spaces/${params.spaceId}`);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Create space
    wrike_create_space: {
      name: 'wrike_create_space',
      description: 'Create a new space',
      inputSchema: {
        type: 'object',
        properties: {
          title: { type: 'string', description: 'Space title (required)' },
          description: { type: 'string', description: 'Space description' },
          members: { type: 'array', items: { type: 'string' }, description: 'Member user IDs' },
          accessType: { 
            type: 'string', 
            enum: ['Personal', 'Private', 'Public'],
            description: 'Access type' 
          },
        },
        required: ['title'],
      },
      handler: async (params: { title: string; [key: string]: unknown }) => {
        const body: Record<string, unknown> = {
          title: params.title,
        };
        
        if (params.description) body.description = params.description;
        if (params.members) body.members = params.members;
        if (params.accessType) body.accessType = params.accessType;
        
        const response = await client.post<WrikeSpace>('/spaces', body);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Update space
    wrike_update_space: {
      name: 'wrike_update_space',
      description: 'Update an existing space',
      inputSchema: {
        type: 'object',
        properties: {
          spaceId: { type: 'string', description: 'Space ID (required)' },
          title: { type: 'string', description: 'New space title' },
          description: { type: 'string', description: 'New space description' },
          archived: { type: 'boolean', description: 'Archive/unarchive the space' },
        },
        required: ['spaceId'],
      },
      handler: async (params: { spaceId: string; [key: string]: unknown }) => {
        const body: Record<string, unknown> = {};
        
        if (params.title) body.title = params.title;
        if (params.description !== undefined) body.description = params.description;
        if (params.archived !== undefined) body.archived = params.archived;
        
        const response = await client.put<WrikeSpace>(`/spaces/${params.spaceId}`, body);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },

    // Delete space
    wrike_delete_space: {
      name: 'wrike_delete_space',
      description: 'Delete a space',
      inputSchema: {
        type: 'object',
        properties: {
          spaceId: { type: 'string', description: 'Space ID (required)' },
        },
        required: ['spaceId'],
      },
      handler: async (params: { spaceId: string }) => {
        const response = await client.delete<WrikeSpace>(`/spaces/${params.spaceId}`);
        
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(response.data[0], null, 2),
            },
          ],
        };
      },
    },
  };
}
