/**
 * Basecamp Questionnaires Tools (Automatic Check-ins)
 */

import { BasecampClient } from '../clients/basecamp.js';
import type { Questionnaire, Question, Answer } from '../types/index.js';

export function registerQuestionnairesTools(client: BasecampClient) {
  return [
    {
      name: 'basecamp_questionnaires_list',
      description: 'List all questionnaires (automatic check-ins) 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<Questionnaire[]>(
          `/buckets/${args.project_id}/questionnaires.json`
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

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

    {
      name: 'basecamp_questions_list',
      description: 'List all questions in a questionnaire',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          questionnaire_id: {
            type: 'number',
            description: 'The ID of the questionnaire',
          },
        },
        required: ['project_id', 'questionnaire_id'],
      },
      handler: async (args: { project_id: number; questionnaire_id: number }) => {
        const { data } = await client.get<Question[]>(
          `/buckets/${args.project_id}/questionnaires/${args.questionnaire_id}/questions.json`
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_answers_list',
      description: 'List answers to a question',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          question_id: {
            type: 'number',
            description: 'The ID of the question',
          },
        },
        required: ['project_id', 'question_id'],
      },
      handler: async (args: { project_id: number; question_id: number }) => {
        const { data } = await client.get<Answer[]>(
          `/buckets/${args.project_id}/questions/${args.question_id}/answers.json`
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },
  ];
}
