/**
 * Basecamp Projects Tools
 */

import { BasecampClient } from '../clients/basecamp.js';
import type { Project, CreateProjectRequest, UpdateProjectRequest } from '../types/index.js';

export function registerProjectsTools(client: BasecampClient) {
  return [
    {
      name: 'basecamp_projects_list',
      description: 'List all projects. Returns active, archived, or trashed projects.',
      inputSchema: {
        type: 'object',
        properties: {
          status: {
            type: 'string',
            enum: ['active', 'archived', 'trashed'],
            description: 'Filter by project status (default: active)',
          },
        },
      },
      handler: async (args: { status?: string }) => {
        const status = args.status || 'active';
        const { data } = await client.get<Project[]>(`/projects.json`, { status });
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

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

    {
      name: 'basecamp_project_create',
      description: 'Create a new project',
      inputSchema: {
        type: 'object',
        properties: {
          name: {
            type: 'string',
            description: 'The name of the project',
          },
          description: {
            type: 'string',
            description: 'Optional project description',
          },
        },
        required: ['name'],
      },
      handler: async (args: CreateProjectRequest) => {
        const data = await client.post<Project>('/projects.json', args);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

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

    {
      name: 'basecamp_project_archive',
      description: 'Archive a project',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project to archive',
          },
        },
        required: ['project_id'],
      },
      handler: async (args: { project_id: number }) => {
        const data = await client.put<Project>(`/projects/${args.project_id}.json`, { status: 'archived' });
        return {
          content: [
            {
              type: 'text',
              text: `Project ${args.project_id} archived successfully`,
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_project_trash',
      description: 'Move a project to trash',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project to trash',
          },
        },
        required: ['project_id'],
      },
      handler: async (args: { project_id: number }) => {
        await client.delete(`/projects/${args.project_id}.json`);
        return {
          content: [
            {
              type: 'text',
              text: `Project ${args.project_id} moved to trash`,
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_project_tools_list',
      description: 'List all tools (dock items) available in a project',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
        },
        required: ['project_id'],
      },
      handler: async (args: { project_id: number }) => {
        const { data } = await client.get<Project>(`/projects/${args.project_id}.json`);
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data.dock, null, 2),
            },
          ],
        };
      },
    },
  ];
}
