import type { ToolConfig } from '@/tools/types'
import type { ZoomGetMeetingParams, ZoomGetMeetingResponse } from '@/tools/zoom/types'

export const zoomGetMeetingTool: ToolConfig<ZoomGetMeetingParams, ZoomGetMeetingResponse> = {
  id: 'zoom_get_meeting',
  name: 'Zoom Get Meeting',
  description: 'Get details of a specific Zoom meeting',
  version: '1.0.0',

  oauth: {
    required: true,
    provider: 'zoom',
    requiredScopes: ['meeting:read:meeting'],
  },

  params: {
    meetingId: {
      type: 'string',
      required: true,
      visibility: 'user-or-llm',
      description: 'The meeting ID',
    },
    occurrenceId: {
      type: 'string',
      required: false,
      visibility: 'user-or-llm',
      description: 'Occurrence ID for recurring meetings',
    },
    showPreviousOccurrences: {
      type: 'boolean',
      required: false,
      visibility: 'user-or-llm',
      description: 'Show previous occurrences for recurring meetings',
    },
  },

  request: {
    url: (params) => {
      const baseUrl = `https://api.zoom.us/v2/meetings/${encodeURIComponent(params.meetingId)}`
      const queryParams = new URLSearchParams()

      if (params.occurrenceId) {
        queryParams.append('occurrence_id', params.occurrenceId)
      }
      if (params.showPreviousOccurrences != null) {
        queryParams.append('show_previous_occurrences', String(params.showPreviousOccurrences))
      }

      const queryString = queryParams.toString()
      return queryString ? `${baseUrl}?${queryString}` : baseUrl
    },
    method: 'GET',
    headers: (params) => {
      if (!params.accessToken) {
        throw new Error('Missing access token for Zoom API request')
      }
      return {
        'Content-Type': 'application/json',
        Authorization: `Bearer ${params.accessToken}`,
      }
    },
  },

  transformResponse: async (response) => {
    if (!response.ok) {
      const errorData = await response.json().catch(() => ({}))
      return {
        success: false,
        error: errorData.message || `Zoom API error: ${response.status} ${response.statusText}`,
        output: { meeting: {} as any },
      }
    }

    const data = await response.json()

    return {
      success: true,
      output: {
        meeting: {
          id: data.id,
          uuid: data.uuid,
          host_id: data.host_id,
          host_email: data.host_email,
          topic: data.topic,
          type: data.type,
          status: data.status,
          start_time: data.start_time,
          duration: data.duration,
          timezone: data.timezone,
          agenda: data.agenda,
          created_at: data.created_at,
          start_url: data.start_url,
          join_url: data.join_url,
          password: data.password,
          h323_password: data.h323_password,
          pstn_password: data.pstn_password,
          encrypted_password: data.encrypted_password,
          settings: data.settings,
          recurrence: data.recurrence,
          occurrences: data.occurrences,
        },
      },
    }
  },

  outputs: {
    meeting: {
      type: 'object',
      description: 'The meeting details',
      properties: {
        id: { type: 'number', description: 'Meeting ID' },
        uuid: { type: 'string', description: 'Meeting UUID' },
        host_id: { type: 'string', description: 'Host user ID' },
        host_email: { type: 'string', description: 'Host email' },
        topic: { type: 'string', description: 'Meeting topic' },
        type: { type: 'number', description: 'Meeting type' },
        status: { type: 'string', description: 'Meeting status' },
        start_time: { type: 'string', description: 'Start time' },
        duration: { type: 'number', description: 'Duration in minutes' },
        timezone: { type: 'string', description: 'Timezone' },
        agenda: { type: 'string', description: 'Meeting agenda' },
        created_at: { type: 'string', description: 'Creation timestamp' },
        start_url: { type: 'string', description: 'Host start URL' },
        join_url: { type: 'string', description: 'Participant join URL' },
        password: { type: 'string', description: 'Meeting password' },
        settings: { type: 'object', description: 'Meeting settings' },
        recurrence: { type: 'object', description: 'Recurrence settings' },
        occurrences: { type: 'array', description: 'Meeting occurrences' },
      },
    },
  },
}
