/**
 * Basecamp Todolists Tools
 */

import { BasecampClient } from '../clients/basecamp.js';
import type { Todolist, CreateTodolistRequest } from '../types/index.js';

export function registerTodolistsTools(client: BasecampClient) {
  return [
    {
      name: 'basecamp_todolists_list',
      description: 'List all todolists in a project',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          status: {
            type: 'string',
            enum: ['active', 'archived', 'trashed'],
            description: 'Filter by todolist status',
          },
        },
        required: ['project_id'],
      },
      handler: async (args: { project_id: number; status?: string }) => {
        const { data } = await client.get<Todolist[]>(
          `/buckets/${args.project_id}/todolists.json`,
          args.status ? { status: args.status } : undefined
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_todolist_get',
      description: 'Get a specific todolist by ID',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          todolist_id: {
            type: 'number',
            description: 'The ID of the todolist',
          },
        },
        required: ['project_id', 'todolist_id'],
      },
      handler: async (args: { project_id: number; todolist_id: number }) => {
        const { data } = await client.get<Todolist>(
          `/buckets/${args.project_id}/todolists/${args.todolist_id}.json`
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_todolist_create',
      description: 'Create a new todolist in a project',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          name: {
            type: 'string',
            description: 'The name of the todolist',
          },
          description: {
            type: 'string',
            description: 'Optional description',
          },
        },
        required: ['project_id', 'name'],
      },
      handler: async (args: { project_id: number } & CreateTodolistRequest) => {
        const { project_id, ...payload } = args;
        const data = await client.post<Todolist>(
          `/buckets/${project_id}/todolists.json`,
          payload
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_todolist_update',
      description: 'Update a todolist',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          todolist_id: {
            type: 'number',
            description: 'The ID of the todolist',
          },
          name: {
            type: 'string',
            description: 'Updated name',
          },
          description: {
            type: 'string',
            description: 'Updated description',
          },
        },
        required: ['project_id', 'todolist_id'],
      },
      handler: async (args: {
        project_id: number;
        todolist_id: number;
        name?: string;
        description?: string;
      }) => {
        const { project_id, todolist_id, ...updates } = args;
        const data = await client.put<Todolist>(
          `/buckets/${project_id}/todolists/${todolist_id}.json`,
          updates
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_todolist_reorder',
      description: 'Reorder todolists within a project',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          todolist_ids: {
            type: 'array',
            items: { type: 'number' },
            description: 'Array of todolist IDs in desired order',
          },
        },
        required: ['project_id', 'todolist_ids'],
      },
      handler: async (args: { project_id: number; todolist_ids: number[] }) => {
        await client.put(`/buckets/${args.project_id}/todolists/reorder.json`, {
          todolist_ids: args.todolist_ids,
        });
        return {
          content: [
            {
              type: 'text',
              text: 'Todolists reordered successfully',
            },
          ],
        };
      },
    },
  ];
}
