/**
 * Basecamp Schedules Tools
 */

import { BasecampClient } from '../clients/basecamp.js';
import type { Schedule, ScheduleEntry, CreateScheduleEntryRequest, UpdateScheduleEntryRequest } from '../types/index.js';

export function registerSchedulesTools(client: BasecampClient) {
  return [
    {
      name: 'basecamp_schedules_list',
      description: 'List all schedules 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<Schedule[]>(
          `/buckets/${args.project_id}/schedules.json`
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_schedule_entries_list',
      description: 'List entries in a schedule',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          schedule_id: {
            type: 'number',
            description: 'The ID of the schedule',
          },
          start_date: {
            type: 'string',
            description: 'Filter entries starting from this date (YYYY-MM-DD)',
          },
          end_date: {
            type: 'string',
            description: 'Filter entries up to this date (YYYY-MM-DD)',
          },
        },
        required: ['project_id', 'schedule_id'],
      },
      handler: async (args: {
        project_id: number;
        schedule_id: number;
        start_date?: string;
        end_date?: string;
      }) => {
        const params: any = {};
        if (args.start_date) params.start_date = args.start_date;
        if (args.end_date) params.end_date = args.end_date;

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

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

    {
      name: 'basecamp_schedule_entry_create',
      description: 'Create a new schedule entry/event',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          schedule_id: {
            type: 'number',
            description: 'The ID of the schedule',
          },
          summary: {
            type: 'string',
            description: 'Event summary/title',
          },
          description: {
            type: 'string',
            description: 'Event description',
          },
          starts_at: {
            type: 'string',
            description: 'Start date/time (ISO 8601)',
          },
          ends_at: {
            type: 'string',
            description: 'End date/time (ISO 8601)',
          },
          all_day: {
            type: 'boolean',
            description: 'Whether this is an all-day event',
          },
          participant_ids: {
            type: 'array',
            items: { type: 'number' },
            description: 'Array of person IDs to invite',
          },
          notify: {
            type: 'boolean',
            description: 'Send notifications to participants',
          },
        },
        required: ['project_id', 'schedule_id', 'summary', 'starts_at'],
      },
      handler: async (args: { project_id: number; schedule_id: number } & CreateScheduleEntryRequest) => {
        const { project_id, schedule_id, ...payload } = args;
        const data = await client.post<ScheduleEntry>(
          `/buckets/${project_id}/schedules/${schedule_id}/entries.json`,
          payload
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },

    {
      name: 'basecamp_schedule_entry_update',
      description: 'Update a schedule entry',
      inputSchema: {
        type: 'object',
        properties: {
          project_id: {
            type: 'number',
            description: 'The ID of the project',
          },
          entry_id: {
            type: 'number',
            description: 'The ID of the schedule entry',
          },
          summary: {
            type: 'string',
            description: 'Updated summary',
          },
          description: {
            type: 'string',
            description: 'Updated description',
          },
          starts_at: {
            type: 'string',
            description: 'Updated start time',
          },
          ends_at: {
            type: 'string',
            description: 'Updated end time',
          },
          all_day: {
            type: 'boolean',
            description: 'Updated all-day flag',
          },
          participant_ids: {
            type: 'array',
            items: { type: 'number' },
            description: 'Updated participants',
          },
          notify: {
            type: 'boolean',
            description: 'Send notifications',
          },
        },
        required: ['project_id', 'entry_id'],
      },
      handler: async (args: { project_id: number; entry_id: number } & UpdateScheduleEntryRequest) => {
        const { project_id, entry_id, ...updates } = args;
        const data = await client.put<ScheduleEntry>(
          `/buckets/${project_id}/schedule_entries/${entry_id}.json`,
          updates
        );
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(data, null, 2),
            },
          ],
        };
      },
    },
  ];
}
