import { UserEventCategory } from '@spiffy-ai/commerce-api-client';
import { atom } from 'jotai';
import { FormSubmittedAttributes, UserEvent } from 'src/application/models';
import { v4 as uuid } from 'uuid';
import { formSubmitAtom, replyEventCategoryAtom } from 'src/atoms/chat';
import { queueUserEventAtom } from 'src/atoms/chat/messageQueue';

export const handleFormSubmittedAtom = atom(null, (_, set, form: FormSubmittedAttributes) => {
  set(replyEventCategoryAtom, UserEventCategory.FormSubmitted);
  set(formSubmitAtom, form);

  const formUserEvent = {
    eventId: uuid(),
    category: UserEventCategory.FormSubmitted,
    createdAt: new Date().toISOString(),
    attributes: form,
  } satisfies UserEvent;
  set(queueUserEventAtom, formUserEvent);
});
