import type { UseMutationResult } from "@tanstack/react-query"
import { toast } from "sonner"
import {
  agendaEventPath,
  canAutoGenerateVideoLink,
  newAgendaEventUid,
} from "@/lib/agenda/agenda-video-conference"
import { videoProviderLabel } from "@/lib/agenda/agenda-settings-labels"
import type { AgendaVideoProvider } from "@/lib/agenda/agenda-settings-types"
import type { AgendaApiEvent, AgendaCalendar, AgendaEventDraft } from "@/lib/agenda/agenda-types"
import { draftToApiEvent } from "@/lib/api/hooks/use-calendar-mutations"

type CreateMutation = UseMutationResult<
  unknown,
  Error,
  { calendarId: string; event: Partial<AgendaApiEvent> },
  unknown
>

type MeetMutation = UseMutationResult<
  { meet_url: string; etag: string },
  Error,
  { path: string; etag?: string },
  unknown
>

type UpdateMutation = UseMutationResult<
  { etag: string },
  Error,
  { path: string; etag: string; event: Partial<AgendaApiEvent> },
  unknown
>

export async function createAgendaEventWithVideo({
  draft,
  calendar,
  userEmail,
  includeVideo,
  videoProvider,
  createMutation,
  meetLinkMutation,
}: {
  draft: AgendaEventDraft
  calendar: AgendaCalendar
  userEmail?: string
  includeVideo: boolean
  videoProvider: AgendaVideoProvider
  createMutation: CreateMutation
  meetLinkMutation: MeetMutation
}) {
  const uid = newAgendaEventUid()
  const apiEvent = draftToApiEvent(draft, undefined, { uid })
  if (userEmail) apiEvent.organizer = userEmail
  await createMutation.mutateAsync({ calendarId: draft.calendarId, event: apiEvent })

  if (!includeVideo || videoProvider === "none") return

  if (canAutoGenerateVideoLink(videoProvider)) {
    await meetLinkMutation.mutateAsync({
      path: agendaEventPath(calendar, uid),
      etag: "",
    })
    return
  }

  toast.message(
    `Événement créé. La génération automatique ${videoProviderLabel(videoProvider)} arrive bientôt.`,
  )
}

export async function saveAgendaEventEdit({
  draft,
  path,
  etag,
  master,
  includeVideo,
  meetUrl,
  videoProvider,
  updateMutation,
  meetLinkMutation,
}: {
  draft: AgendaEventDraft
  path: string
  etag: string
  master: AgendaApiEvent
  includeVideo: boolean
  meetUrl: string
  videoProvider: AgendaVideoProvider
  updateMutation: UpdateMutation
  meetLinkMutation: MeetMutation
}) {
  if (!includeVideo) {
    await updateMutation.mutateAsync({
      path,
      etag,
      event: draftToApiEvent(draft, master, { meetUrl: "" }),
    })
    return
  }

  if (!meetUrl && canAutoGenerateVideoLink(videoProvider)) {
    const { etag: newEtag } = await updateMutation.mutateAsync({
      path,
      etag,
      event: draftToApiEvent(draft, master, { meetUrl: "" }),
    })
    await meetLinkMutation.mutateAsync({ path, etag: newEtag })
    return
  }

  await updateMutation.mutateAsync({
    path,
    etag,
    event: draftToApiEvent(draft, master, { meetUrl: meetUrl || "" }),
  })

  if (!meetUrl && !canAutoGenerateVideoLink(videoProvider)) {
    toast.message(
      `La génération automatique ${videoProviderLabel(videoProvider)} arrive bientôt.`,
    )
  }
}
