import { CustServiceAttachment, FormType } from '@spiffy-ai/commerce-api-client';
import {
  AcceptableFormFieldType,
  OrderResponseAttributes,
  PageResponseAttributes,
  ProductResponseAttributes,
  ReviewResponseAttributes,
  SearchProductFilterAttributes,
  SearchProductResponseAttributes,
  SearchResponseAttributes,
  TextResponseAttributes,
} from './api/response';

export enum MessageRole {
  User = 'user',
  Assistant = 'assistant',
}

export enum MessageType {
  Text = 'text',
  SuggestionClicked = 'suggestion_clicked',
  Product = 'product',
  ProductSearch = 'product_search',
  Review = 'review',
  QueryTyped = 'query_typed',
  Separator = 'separator',
  Page = 'page',
  Search = 'search',
  Attachment = 'attachment',
  CXAgentResponse = 'cx_agent_response',
  Form = 'form',
  Order = 'order',
  ProductSearchFilter = 'product_search_filter',
}

export type Message = {
  id: string;
  role: MessageRole;
  createdAt: string;
} & (
  | TextMessageMetadata
  | QueryTypedMetadata
  | SuggestionMessageMetadata
  | ProductMessageMetadata
  | ProductSearchMessageMetadata
  | ReviewMessageMetadata
  | SeparatorMessageMetadata
  | PageMessageMetadata
  | SearchMessageMetadata
  | AttachmentMetadata
  | CXAgentResponseMetadata
  | FormMessageMetadata
  | OrderMessageMetadata
  | ProductSearchFilterMetadata
);

type AttachmentMetadata = {
  type: MessageType.Attachment;
  metadata: {
    message?: string;
    attachment: CustServiceAttachment[];
  };
};

type CXAgentResponseMetadata = {
  type: MessageType.CXAgentResponse;
  metadata: {
    message: string;
    agentName: string;
  };
};

type TextMessageMetadata = {
  type: MessageType.Text;
  metadata: TextResponseAttributes['attributes'];
};

type QueryTypedMetadata = {
  type: MessageType.QueryTyped;
  metadata: TextResponseAttributes['attributes'];
};

type SuggestionMessageMetadata = {
  type: MessageType.SuggestionClicked;
  metadata: {
    suggestionId: string;
    suggestionContent: string;
  };
};

export type ProductMessageMetadata = {
  type: MessageType.Product;
  metadata: ProductResponseAttributes['attributes'];
};

export type ProductSearchMessageMetadata = {
  type: MessageType.ProductSearch;
  metadata: SearchProductResponseAttributes['attributes'];
};

type ReviewMessageMetadata = {
  type: MessageType.Review;
  metadata: ReviewResponseAttributes['attributes'];
};

type SeparatorMessageMetadata = {
  type: MessageType.Separator;
};

type PageMessageMetadata = {
  type: MessageType.Page;
  metadata: PageResponseAttributes['attributes'];
};

type SearchMessageMetadata = {
  type: MessageType.Search;
  metadata: SearchResponseAttributes['attributes'];
};

type ProductSearchFilterMetadata = {
  type: MessageType.ProductSearchFilter;
  metadata: SearchProductFilterAttributes['attributes'];
};

export type FormField = {
  key: string;
  title: string;
  type: AcceptableFormFieldType;
  required: boolean;
  // this is the only format we currently support
  format?: 'email';
  value?: string;
};

type FormMessageMetadata = {
  type: MessageType.Form;
  metadata: {
    formType?: FormType;
    fields: FormField[];
  };
};

type OrderMessageMetadata = {
  type: MessageType.Order;
  metadata: OrderResponseAttributes['attributes'];
};
