import { UserEventCategory } from '@spiffy-ai/commerce-api-client';
import { isApiUserEvent } from 'src/application/models/guards/api/isApiUserEvent';
import { UserEvent } from 'src/application/models/index';
import { isApiPDPAttributes } from 'src/application/models/guards/api/isApiPDPEventAttributes';
import { isApiQueryTypedAttributes } from 'src/application/models/guards/api/isApiQueryTypedEventAttributes';
import { isApiSearchAttributes } from 'src/application/models/guards/api/isApiSearchEventAttributes';
import { isApiSuggestionClickedAttributes } from 'src/application/models/guards/api/isApiSuggestionClickedEventAttributes';
import {
  isApiPLPEventAttributes,
  isApiPLPIdAttribute,
} from 'src/application/models/guards/api/isApiPLPEventAttributes';
import { isApiFormSubmittedResponseAttributes } from 'src/application/models/guards/api/isApiFormSubmittedResponseAttributes';

export const validateUserEvent = (data: unknown): UserEvent | undefined => {
  if (!isApiUserEvent(data)) {
    return undefined;
  }

  if (data.category === UserEventCategory.PdpVisit && isApiPDPAttributes(data.attributes)) {
    return {
      eventId: data.event_id,
      createdAt: data.created_at,
      category: UserEventCategory.PdpVisit,
      attributes: {
        productId: data.attributes.product_id,
        parentProductId: data.attributes.parent_product_id,
        url: data.attributes.url,
      },
    };
  }

  if (
    data.category === UserEventCategory.QueryTyped &&
    isApiQueryTypedAttributes(data.attributes)
  ) {
    return {
      eventId: data.event_id,
      createdAt: data.created_at,
      category: UserEventCategory.QueryTyped,
      attributes: {
        query: data.attributes.query,
      },
    };
  }

  if (data.category === UserEventCategory.Search && isApiSearchAttributes(data.attributes)) {
    return {
      eventId: data.event_id,
      createdAt: data.created_at,
      category: UserEventCategory.Search,
      attributes: {
        searchTerm: data.attributes.search_term,
        selectedFilters: data.attributes.selected_filters,
      },
    };
  }

  if (
    data.category === UserEventCategory.SuggestionClicked &&
    isApiSuggestionClickedAttributes(data.attributes)
  ) {
    return {
      eventId: data.event_id,
      createdAt: data.created_at,
      category: UserEventCategory.SuggestionClicked,
      attributes: {
        suggestionId: data.attributes.suggestion_id,
      },
    };
  }

  if (data.category === UserEventCategory.PlpVisit && isApiPLPEventAttributes(data.attributes)) {
    if (isApiPLPIdAttribute(data.attributes)) {
      return {
        eventId: data.event_id,
        createdAt: data.created_at,
        category: UserEventCategory.PlpVisit,
        attributes: {
          category: data.attributes.category,
          attributes: {
            id: data.attributes.id,
          },
        },
      };
    }
  }

  if (
    data.category === UserEventCategory.FormSubmitted &&
    isApiFormSubmittedResponseAttributes(data.attributes)
  ) {
    return {
      eventId: data.event_id,
      createdAt: data.created_at,
      category: UserEventCategory.FormSubmitted,
      attributes: {
        filledSchema: { ...data.attributes.filled_schema },
        formResponseId: data.attributes.form_response_id,
        formType: data.attributes.form_type,
      },
    };
  }

  // we're not using app_unloaded at the moment so return the app_loaded event as default
  return {
    eventId: data.event_id,
    createdAt: data.created_at,
    category: UserEventCategory.AppLoaded,
  };
};
