'use server'

import {revalidatePath} from 'next/cache'
import {redirect} from 'next/navigation'
import {createMeeting, deleteMeeting, updateMeeting} from '@/dal/meetings'
import {protectedFormAction, protectedServerFunction} from '@/lib/serverFunctions'
import {createMeetingSchema, deleteMeetingSchema, updateMeetingSchema} from '@/schemas/meetingSchemas'

export const createMeetingAction = protectedFormAction({
  schema: createMeetingSchema,
  serverFn: async ({data, profile}) => {
    await createMeeting({...data, userId: profile.id})
    revalidatePath('/meetings')
  },
  functionName: 'Create meeting action',
})

export const updateMeetingAction = protectedFormAction({
  schema: updateMeetingSchema,
  serverFn: async ({data, profile}) => {
    await updateMeeting({...data, userId: profile.id})
    redirect(`/meetings`)
  },
  functionName: 'Update meeting action',
})

export const cancelMeetingServerFunction = protectedServerFunction({
  schema: deleteMeetingSchema,
  serverFn: async ({data, profile}) => {
    await deleteMeeting(data.id, profile.id)
    redirect('/meetings')
  },
  functionName: 'Cancel meeting action',
})
