import { ResponseCategory } from '@spiffy-ai/commerce-api-client';
import { CamelCasedPropertiesDeep } from 'src/application/models/utilityTypes';

import type {
  ResponseProductAttributes as ApiProductResponseAttributes,
  ProductSearchResponseAttributes as ApiProductSearchResponseAttributes,
  SearchResponseProduct as ApiSearchResponseProduct,
  FormType,
  FulfillmentDisplayStatus,
} from '@spiffy-ai/commerce-api-client';
import {
  ApiPageResponseAttributes,
  ApiProductSearchFilterResponseAttributes,
  ApiReviewResponseAttributes,
  ApiReviewRichInformation,
  ApiSearchResponseAttributes,
  ApiTextResponseAttributes,
} from 'src/application/models/api/responseGenerics';

// Internal types
export interface ReviewRichInformation extends CamelCasedPropertiesDeep<ApiReviewRichInformation> {}

export type AcceptableFormFieldType = 'string' | 'number' | 'boolean' | 'object' | 'array';

export interface OrderItemInfo {
  itemTitle: string;
  itemPrice: number;
  itemQuantity: number;
  orderItemId: string;
  image?: string;
  inTransitAt?: string;
  deliveredAt?: string;
  estimatedDeliveryAt?: string;
  fulfillmentDisplayStatus?: FulfillmentDisplayStatus;
  trackingUrl?: string;
}

export interface OrderResponseAttributes {
  category: ResponseCategory.Order;
  attributes: {
    orderId: string;
    orderNumber: string;
    createdAt: string;
    latestEventDate: string;
    lineItems: OrderItemInfo[];
  };
}

export interface TextResponseAttributes {
  category: ResponseCategory.Text;
  attributes: CamelCasedPropertiesDeep<ApiTextResponseAttributes>;
}

export interface ProductResponseAttributes {
  category: ResponseCategory.Product;
  attributes: CamelCasedPropertiesDeep<
    ApiProductResponseAttributes & {
      filters?: {
        [key: string]: string[];
      };
    }
  >;
}

export interface SearchResponseProductAttributes {
  category: ResponseCategory.Product;
  attributes: CamelCasedPropertiesDeep<ApiSearchResponseProduct>;
}

export interface SearchResponseAttributes {
  category: ResponseCategory.Text;
  attributes: CamelCasedPropertiesDeep<ApiSearchResponseAttributes>;
}

export interface SearchProductResponseAttributes {
  category: ResponseCategory.ProductSearch;
  attributes: CamelCasedPropertiesDeep<ApiProductSearchResponseAttributes>;
}

export interface SearchProductFilterAttributes {
  category: ResponseCategory.ProductSearchFilter;
  attributes: CamelCasedPropertiesDeep<ApiProductSearchFilterResponseAttributes>;
}

export interface ReviewResponseAttributes {
  category: ResponseCategory.Review;
  attributes: CamelCasedPropertiesDeep<ApiReviewResponseAttributes>;
}

export interface SeparatorResponseAttributes {
  category: ResponseCategory.Separator;
}

export interface PageResponseAttributes {
  category: ResponseCategory.Page;
  attributes: CamelCasedPropertiesDeep<ApiPageResponseAttributes>;
}

export interface FormResponseAttributes {
  category: ResponseCategory.Form;
  attributes: {
    formCategory?: {
      formType: FormType;
    };
    schema: {
      properties: {
        [key: string]: {
          type: AcceptableFormFieldType;
          title: string;
          format?: 'email';
        };
      };
      required: string[];
    };
  };
}

export type Response = {
  id: string;
  createdAt: string;
  category: ResponseCategory;
} & (
  | TextResponseAttributes
  | ProductResponseAttributes
  | ReviewResponseAttributes
  | SeparatorResponseAttributes
  | PageResponseAttributes
  | SearchProductResponseAttributes
  | SearchProductFilterAttributes
  | FormResponseAttributes
  | OrderResponseAttributes
);
