'use server'

import DAL, {CreateMeetingParams, UpdateMeetingParams} from '@dal'
import {revalidatePath} from 'next/cache'
import {redirect} from 'next/navigation'
import {getSessionProfileAndOptionallyRenew} from '@mediators'

/**
 * Create a new meeting.
 *
 * @param meeting The information about the meeting.
 */
export async function createMeetings(meeting: CreateMeetingParams): Promise<void> {
  const profile = await getSessionProfileAndOptionallyRenew()
  await DAL.createMeeting(profile.id, meeting)
  revalidatePath('/meetings')
}

/**
 * Update a meeting
 *
 * @param meeting The information about the meeting.
 */
export async function updateMeetings(meeting: UpdateMeetingParams): Promise<void> {
  const profile = await getSessionProfileAndOptionallyRenew()
  await DAL.updateMeeting(profile.id, meeting)
  revalidatePath(`/meetings/`)
}

/**
 * Delete a meeting.
 *
 * @param id The id of the meeting to delete.
 */
export async function deleteMeeting(id: string): Promise<void> {
  const profile = await getSessionProfileAndOptionallyRenew()
  await DAL.deleteMeeting(profile.id, id)
  redirect('/meetings')
}
