import {
  CALENDAR_API_BASE,
  type GoogleCalendarApiEventResponse,
  type GoogleCalendarGetParams,
  type GoogleCalendarGetResponse,
} from '@/tools/google_calendar/types'
import type { ToolConfig } from '@/tools/types'

export const getTool: ToolConfig<GoogleCalendarGetParams, GoogleCalendarGetResponse> = {
  id: 'google_calendar_get',
  name: 'Google Calendar Get Event',
  description: 'Get a specific event from Google Calendar',
  version: '1.0.0',

  oauth: {
    required: true,
    provider: 'google-calendar',
  },

  params: {
    accessToken: {
      type: 'string',
      required: true,
      visibility: 'hidden',
      description: 'Access token for Google Calendar API',
    },
    calendarId: {
      type: 'string',
      required: false,
      visibility: 'user-only',
      description: 'Calendar ID (defaults to primary)',
    },
    eventId: {
      type: 'string',
      required: true,
      visibility: 'user-only',
      description: 'Event ID to retrieve',
    },
  },

  request: {
    url: (params: GoogleCalendarGetParams) => {
      const calendarId = params.calendarId || 'primary'
      return `${CALENDAR_API_BASE}/calendars/${encodeURIComponent(calendarId)}/events/${encodeURIComponent(params.eventId)}`
    },
    method: 'GET',
    headers: (params: GoogleCalendarGetParams) => ({
      Authorization: `Bearer ${params.accessToken}`,
      'Content-Type': 'application/json',
    }),
  },

  transformResponse: async (response: Response) => {
    const data: GoogleCalendarApiEventResponse = await response.json()

    return {
      success: true,
      output: {
        content: `Retrieved event "${data.summary}"`,
        metadata: {
          id: data.id,
          htmlLink: data.htmlLink,
          status: data.status,
          summary: data.summary,
          description: data.description,
          location: data.location,
          start: data.start,
          end: data.end,
          attendees: data.attendees,
          creator: data.creator,
          organizer: data.organizer,
        },
      },
    }
  },

  outputs: {
    content: { type: 'string', description: 'Event retrieval confirmation message' },
    metadata: {
      type: 'json',
      description: 'Event details including ID, status, times, and attendees',
    },
  },
}

interface GoogleCalendarGetV2Response {
  success: boolean
  output: {
    id: string
    htmlLink: string
    status: string
    summary: string | null
    description: string | null
    location: string | null
    start: any
    end: any
    attendees: any | null
    creator: any
    organizer: any
  }
}

export const getV2Tool: ToolConfig<GoogleCalendarGetParams, GoogleCalendarGetV2Response> = {
  id: 'google_calendar_get_v2',
  name: 'Google Calendar Get Event',
  description: 'Get a specific event from Google Calendar. Returns API-aligned fields only.',
  version: '2.0.0',
  oauth: getTool.oauth,
  params: getTool.params,
  request: getTool.request,
  transformResponse: async (response: Response) => {
    const data: GoogleCalendarApiEventResponse = await response.json()

    return {
      success: true,
      output: {
        id: data.id,
        htmlLink: data.htmlLink,
        status: data.status,
        summary: data.summary ?? null,
        description: data.description ?? null,
        location: data.location ?? null,
        start: data.start,
        end: data.end,
        attendees: data.attendees ?? null,
        creator: data.creator,
        organizer: data.organizer,
      },
    }
  },
  outputs: {
    id: { type: 'string', description: 'Event ID' },
    htmlLink: { type: 'string', description: 'Event link' },
    status: { type: 'string', description: 'Event status' },
    summary: { type: 'string', description: 'Event title', optional: true },
    description: { type: 'string', description: 'Event description', optional: true },
    location: { type: 'string', description: 'Event location', optional: true },
    start: { type: 'json', description: 'Event start' },
    end: { type: 'json', description: 'Event end' },
    attendees: { type: 'json', description: 'Event attendees', optional: true },
    creator: { type: 'json', description: 'Event creator' },
    organizer: { type: 'json', description: 'Event organizer' },
  },
}
