/**
 * Basecamp Todos Tools
 */

import { BasecampClient } from '../clients/basecamp.js';
import type { Todo, CreateTodoRequest, UpdateTodoRequest } from '../types/index.js';

export function registerTodosTools(client: BasecampClient) {
  return [
    {
      name: 'basecamp_todos_list',
      description: 'List all todos in 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',
          },
          status: {
            type: 'string',
            enum: ['active', 'archived', 'trashed'],
            description: 'Filter by todo status',
          },
          completed: {
            type: 'boolean',
            description: 'Filter by completion status',
          },
        },
        required: ['project_id', 'todolist_id'],
      },
      handler: async (args: {
        project_id: number;
        todolist_id: number;
        status?: string;
        completed?: boolean;
      }) => {
        const params: any = {};
        if (args.status) params.status = args.status;
        if (args.completed !== undefined) params.completed = args.completed;

        const { data } = await client.get<Todo[]>(
          `/buckets/${args.project_id}/todolists/${args.todolist_id}/todos.json`,
          Object.keys(params).length > 0 ? params : undefined
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

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

    {
      name: 'basecamp_todo_create',
      description: 'Create a new todo in 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',
          },
          content: {
            type: 'string',
            description: 'The content/title of the todo',
          },
          description: {
            type: 'string',
            description: 'Optional description',
          },
          assignee_ids: {
            type: 'array',
            items: { type: 'number' },
            description: 'Array of person IDs to assign',
          },
          due_on: {
            type: 'string',
            description: 'Due date (YYYY-MM-DD)',
          },
          starts_on: {
            type: 'string',
            description: 'Start date (YYYY-MM-DD)',
          },
          notify: {
            type: 'boolean',
            description: 'Send notifications to assignees',
          },
        },
        required: ['project_id', 'todolist_id', 'content'],
      },
      handler: async (args: { project_id: number; todolist_id: number } & CreateTodoRequest) => {
        const { project_id, todolist_id, ...payload } = args;
        const data = await client.post<Todo>(
          `/buckets/${project_id}/todolists/${todolist_id}/todos.json`,
          payload
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_todo_update',
      description: 'Update a todo',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          todo_id: {
            type: 'number',
            description: 'The ID of the todo',
          },
          content: {
            type: 'string',
            description: 'Updated content/title',
          },
          description: {
            type: 'string',
            description: 'Updated description',
          },
          assignee_ids: {
            type: 'array',
            items: { type: 'number' },
            description: 'Updated assignees',
          },
          due_on: {
            type: 'string',
            description: 'Updated due date (YYYY-MM-DD)',
          },
          starts_on: {
            type: 'string',
            description: 'Updated start date (YYYY-MM-DD)',
          },
          notify: {
            type: 'boolean',
            description: 'Send notifications',
          },
        },
        required: ['project_id', 'todo_id'],
      },
      handler: async (args: { project_id: number; todo_id: number } & UpdateTodoRequest) => {
        const { project_id, todo_id, ...updates } = args;
        const data = await client.put<Todo>(
          `/buckets/${project_id}/todos/${todo_id}.json`,
          updates
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_todo_complete',
      description: 'Mark a todo as complete',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          todo_id: {
            type: 'number',
            description: 'The ID of the todo',
          },
        },
        required: ['project_id', 'todo_id'],
      },
      handler: async (args: { project_id: number; todo_id: number }) => {
        const data = await client.post<Todo>(
          `/buckets/${args.project_id}/todos/${args.todo_id}/completion.json`,
          {}
        );
        return {
          content: [
            {
              type: 'text',
              text: `Todo ${args.todo_id} marked as complete`,
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_todo_uncomplete',
      description: 'Mark a todo as incomplete',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          todo_id: {
            type: 'number',
            description: 'The ID of the todo',
          },
        },
        required: ['project_id', 'todo_id'],
      },
      handler: async (args: { project_id: number; todo_id: number }) => {
        await client.delete(`/buckets/${args.project_id}/todos/${args.todo_id}/completion.json`);
        return {
          content: [
            {
              type: 'text',
              text: `Todo ${args.todo_id} marked as incomplete`,
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_todos_reorder',
      description: 'Reorder todos within 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',
          },
          todo_ids: {
            type: 'array',
            items: { type: 'number' },
            description: 'Array of todo IDs in desired order',
          },
        },
        required: ['project_id', 'todolist_id', 'todo_ids'],
      },
      handler: async (args: { project_id: number; todolist_id: number; todo_ids: number[] }) => {
        await client.put(
          `/buckets/${args.project_id}/todolists/${args.todolist_id}/todos/reorder.json`,
          { todo_ids: args.todo_ids }
        );
        return {
          content: [
            {
              type: 'text',
              text: 'Todos reordered successfully',
            },
          ],
        };
      },
    },
  ];
}
