import { User } from '@/components/Attendees/types'
import { getCalendarVisibility } from '@/components/Calendar/utils/calendarUtils'
import { formatReduxError } from '@/utils/errorUtils'
import { createAsyncThunk } from '@reduxjs/toolkit'
import { getCalendars } from '../CalendarApi'
import { Calendar } from '../CalendarTypes'
import { RejectedError } from '../types/RejectedError'
import { fetchOwnerData } from './helpers'

export const getTempCalendarsListAsync = createAsyncThunk<
  Record<string, Calendar>,
  User,
  { rejectValue: RejectedError }
>('calendars/getTempCalendars', async (tempUser, { rejectWithValue }) => {
  try {
    const importedCalendars: Record<string, Calendar> = {}
    if (!tempUser.openpaasId) {
      const username = tempUser.displayName || tempUser.email || 'User'
      throw new Error(
        `TRANSLATION:calendar.userDoesNotHaveValidId|name=${encodeURIComponent(username)}`
      )
    }
    const calendars = await getCalendars(
      tempUser.openpaasId,
      'sharedPublic=true&'
    )

    const rawCalendars = calendars._embedded?.['dav:calendar']
    if (!rawCalendars || rawCalendars.length === 0) {
      const userName = tempUser.displayName || tempUser.email || 'User'
      // Format: TRANSLATION:key|param1=value1
      const encodedName = encodeURIComponent(userName)
      throw new Error(
        `TRANSLATION:calendar.userDoesNotHavePublicCalendars|name=${encodedName}`
      )
    }

    for (const cal of rawCalendars) {
      const name = cal['dav:name'] ?? ''
      const description = cal['caldav:description'] ?? ''
      const delegated = cal['calendarserver:delegatedsource'] ? true : false
      const source = cal['calendarserver:source']
        ? cal['calendarserver:source']._links.self?.href
        : cal._links.self?.href
      if (!source) {
        throw new Error('No source for calendar')
      }
      const link = cal._links.self?.href ?? ''

      const id = source.replace('/calendars/', '').replace('.json', '')
      const visibility = getCalendarVisibility(cal['acl'] ?? [])
      const ownerData = await fetchOwnerData(id.split('/')[0])

      importedCalendars[id] = {
        id,
        name,
        link,
        owner: ownerData,
        description,
        delegated,
        color: {
          light: tempUser.color?.light ?? '#a8a8a8ff',
          dark: tempUser.color?.dark ?? '#a8a8a8ff'
        },
        visibility,
        events: {}
      }
    }

    return importedCalendars
  } catch (err) {
    const error = err as { response?: { status?: number } }
    return rejectWithValue({
      message: formatReduxError(err),
      status: error.response?.status
    })
  }
})
