/**
 * Basecamp Recordings Tools (Generic Content)
 */

import { BasecampClient } from '../clients/basecamp.js';
import type { Recording } from '../types/index.js';

export function registerRecordingsTools(client: BasecampClient) {
  return [
    {
      name: 'basecamp_recordings_list',
      description: 'List all recordings (generic content) in a project',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          type: {
            type: 'string',
            description: 'Filter by recording type (e.g., "Message", "Todo", "Document")',
          },
          status: {
            type: 'string',
            enum: ['active', 'archived', 'trashed'],
            description: 'Filter by status',
          },
        },
        required: ['project_id'],
      },
      handler: async (args: { project_id: number; type?: string; status?: string }) => {
        const params: any = {};
        if (args.type) params.type = args.type;
        if (args.status) params.status = args.status;

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

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

    {
      name: 'basecamp_recording_archive',
      description: 'Archive a recording',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          recording_id: {
            type: 'number',
            description: 'The ID of the recording',
          },
        },
        required: ['project_id', 'recording_id'],
      },
      handler: async (args: { project_id: number; recording_id: number }) => {
        await client.put(
          `/buckets/${args.project_id}/recordings/${args.recording_id}/status/archived.json`,
          {}
        );
        return {
          content: [
            {
              type: 'text',
              text: `Recording ${args.recording_id} archived successfully`,
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_recording_unarchive',
      description: 'Unarchive a recording',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          recording_id: {
            type: 'number',
            description: 'The ID of the recording',
          },
        },
        required: ['project_id', 'recording_id'],
      },
      handler: async (args: { project_id: number; recording_id: number }) => {
        await client.put(
          `/buckets/${args.project_id}/recordings/${args.recording_id}/status/active.json`,
          {}
        );
        return {
          content: [
            {
              type: 'text',
              text: `Recording ${args.recording_id} unarchived successfully`,
            },
          ],
        };
      },
    },

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