import {deleteContact, getContact, updateContact} from '@/dal/contacts'
import {ok} from '@/lib/routeResponses'
import {protectedApiRoute} from '@/lib/apiRoute'
import {updateContactSchema} from '@/schemas/contactSchemas'

interface RouteParams {
  contactId: string
}

export const GET = protectedApiRoute({
  routeFn: async ({profile}, {contactId}: RouteParams) => {
    const contact = await getContact(contactId, profile.id)
    return ok(contact)
  },
})

export const PUT = protectedApiRoute({
  routeFn: async ({profile, data}, {contactId}: RouteParams) => {
    const updatedContact = await updateContact({...data, userId: profile.id, id: contactId})
    return ok(updatedContact)
  },
  schema: updateContactSchema,
})

export const DELETE = protectedApiRoute({
  routeFn: async ({profile}, {contactId}: RouteParams) => {
    await deleteContact(contactId, profile.id)
    return ok()
  },
})
