'use server'

import {revalidatePath} from 'next/cache'
import {redirect} from 'next/navigation'
import {convertFormData} from '@/lib/convertFormData'
import type {CreateMeetingParams, UpdateMeetingParams} from '@/dal/meetings'
import {createMeeting, deleteMeeting, updateMeeting} from '@/dal/meetings'
import {getLogger} from '@/lib/logger'

export async function createMeetingAction(_prevData: unknown, formData: FormData): Promise<void> {
  const logger = await getLogger()
  logger.info('createMeetingAction called')

  const meeting = convertFormData<CreateMeetingParams>(formData)
  await createMeeting(meeting)

  revalidatePath('/meetings')
  logger.info('createMeetingAction completed successfully')
}

export async function updateMeetingAction(_prevData: unknown, formData: FormData): Promise<void> {
  const logger = await getLogger()
  logger.info('updateMeetingAction called')

  const meeting = convertFormData<UpdateMeetingParams>(formData)
  await updateMeeting(meeting)

  logger.info('updateMeetingAction completed successfully')
  redirect(`/meetings`)
}

export async function cancelMeetingServerFunction({id}: {id: string}): Promise<void> {
  const logger = await getLogger()
  logger.info('cancelMeetingAction called')

  await deleteMeeting(id)

  logger.info('cancelMeetingAction completed successfully')
  redirect('/meetings')
}
