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

/**
 * Transforms a server Response object into a Message object for presentation.
 *
 * @param response The response object received from the server containing model generated content
 *
 * @returns A Message if the response contains known attributes, undefined otherwise
 */
export const messageFromResponse = (response?: Response): Message | undefined => {
  if (response == null) {
    return undefined;
  }

  if (response.category === ResponseCategory.Text) {
    return {
      id: response.id,
      createdAt: response.createdAt,
      type: MessageType.Text,
      role: MessageRole.Assistant,
      metadata: {
        content: response.attributes.content,
      },
    };
  }

  if (response.category === ResponseCategory.Product) {
    return {
      id: response.id,
      createdAt: response.createdAt,
      role: MessageRole.Assistant,
      type: MessageType.Product,
      metadata: {
        ...response.attributes,
      },
    };
  }

  if (response.category === ResponseCategory.Review) {
    return {
      id: response.id,
      createdAt: response.createdAt,
      type: MessageType.Review,
      role: MessageRole.Assistant,
      metadata: {
        review: response.attributes.review,
        reviewer: response.attributes.reviewer,
        stars: response.attributes.stars,
        title: response.attributes.title,
        richInformation: response.attributes.richInformation,
      },
    };
  }

  if (response.category === ResponseCategory.Separator) {
    return {
      id: response.id,
      createdAt: response.createdAt,
      type: MessageType.Separator,
      role: MessageRole.Assistant,
    };
  }

  if (response.category === ResponseCategory.Page) {
    return {
      id: response.id,
      createdAt: response.createdAt,
      role: MessageRole.Assistant,
      type: MessageType.Page,
      metadata: {
        ...response.attributes,
      },
    };
  }

  if (response.category === ResponseCategory.ProductSearch) {
    return {
      id: response.id,
      createdAt: response.createdAt,
      role: MessageRole.Assistant,
      type: MessageType.ProductSearch,
      metadata: {
        ...response.attributes,
      },
    };
  }

  if (response.category === ResponseCategory.ProductSearchFilter) {
    return {
      id: response.id,
      createdAt: response.createdAt,
      role: MessageRole.Assistant,
      type: MessageType.ProductSearchFilter,
      metadata: {
        ...response.attributes,
      },
    };
  }

  if (response.category === ResponseCategory.Form) {
    return {
      id: response.id,
      createdAt: response.createdAt,
      role: MessageRole.Assistant,
      type: MessageType.Form,
      metadata: {
        formType: response.attributes.formCategory?.formType,
        fields: Object.entries(response.attributes.schema.properties).map(([key, value]) => ({
          key,
          title: value.title,
          type: value.type,
          format: value.format,
          required: response.attributes.schema.required.includes(key),
        })),
      },
    };
  }

  if (response.category === ResponseCategory.Order) {
    return {
      id: response.id,
      createdAt: response.createdAt,
      role: MessageRole.Assistant,
      type: MessageType.Order,
      metadata: {
        ...response.attributes,
      },
    };
  }

  return undefined;
};
