import { UserEventCategory } from '@spiffy-ai/commerce-api-client';
import {
  FormResponseAttributes,
  Message,
  MessageRole,
  MessageType,
  UserEvent,
} from 'src/application/models';

export const messageFromFormSubmittedEvent = (
  event: UserEvent,
  formResponseAttributes: FormResponseAttributes['attributes'],
): Message | undefined => {
  if (event.category !== UserEventCategory.FormSubmitted) {
    return undefined;
  }

  const formStringContents = Object.entries(formResponseAttributes.schema.properties)
    .map(([key, value]) => `${value.title}: ${event.attributes.filledSchema[key]}`)
    .join('\n');

  return {
    id: event.eventId,
    role: MessageRole.User,
    type: MessageType.QueryTyped,
    createdAt: event.createdAt,
    metadata: {
      content: formStringContents,
    },
  };
};
