import { BloodMeta } from "./blood.typings";
import { CurrencyCode } from "../../core/commonTypes";
import { OxygenMeta } from "./oxygen.typings";

export interface CoordinatesObject {
  lat?: number;
  lon?: number;
}

export enum ECommerceTypes {
  CancelCheckout = "cancel_checkout",
  Cart = "cart",
  Checkout = "checkout",
  Delivery = "delivery",
  Item = "item",
  ItemReport = "item_report",
  ItemRequest = "item_request",
  ItemVerification = "item_verification",
}

export enum ItemAction {
  View = "view",
  Detail = "detail",
  Impression = "impression",
  TopUp = "top_up",
  Cancel = "cancel",
  Update = "update",
  Remove = "remove",
  Add = "add",
  Select = "select",
  AddFavorite = "add_favorite",
  RemoveFavorite = "remove_favorite",
  AddReminder = "add_reminder",
  RemoveReminder = "remove_reminder",
  RemoveReminderAuto = "remove_reminder_auto",
  Other = "other",
}

export enum ItemType {
  Blood = "blood",
  Book = "book",
  Clothing = "clothing",
  Drug = "drug",
  Grocery = "grocery",
  Subscription = "subscription",
  Facility = "facility",
  Electronics = "electronics",
  MedicalEquipment = "medical_equipment",
  Misc = "misc",
  Oxygen = "oxygen",
  ItemVerification = "item_verification",
  ItemReport = "item_report",
  Reward = "reward",
  Survey = "survey",
  Other = "other",
}

export enum StockStatus {
  InStock = "in_stock",
  LowStock = "low_stock",
  OutOfStock = "out_of_stock",
}

export enum SubscriptionStatus {
  Active = "active",
  Inactive = "inactive",
  Paused = "paused",
  Other = "other",
}

export enum SubscriptionType {
  PayAsYouSell = "pay_as_you_sell",
  PayAsYouGo = "pay_as_you_go",
  Other = "other",
}

// not supported by ts-interface-checker
// export type ItemTypeWithoutMeta = Exclude<ItemType, ItemType.Blood | ItemType.Oxygen>

export interface ItemMinObject {
  id: string;
  type: ItemType;
}

export interface ItemInfoObject {
  id: string;
  type: ItemType;
  batch_id?: string;
  reward_id?: string;
  survey_id?: string;
  is_featured?: boolean;
  expiry_date?: number;
  production_date?: number;
}

export type BaseItemDetail = {
  id: string;
  quantity: number;
  price: number;
  currency: CurrencyCode;
  stock_status?: StockStatus;
  promo_id?: string;
  facility_id?: string;
  discount?: number;
};

export type SubscriptionItemDetail = {
  status: SubscriptionStatus;
  type: SubscriptionType;
  subscription_items: Array<ItemMinObject>;
};

export type MetaBloodItemDetail = BaseItemDetail & {
  type: ItemType.Blood;
  meta: BloodMeta;
};
export type MetaOxygenItemDetail = BaseItemDetail & {
  type: ItemType.Oxygen;
  meta: OxygenMeta;
};
export type DrugItemDetail = BaseItemDetail & { type: ItemType.Drug };
export type GroceryItemDetail = BaseItemDetail & { type: ItemType.Grocery };
export type BloodItemDetail = BaseItemDetail & { type: ItemType.Blood };
export type OxygenItemDetail = BaseItemDetail & { type: ItemType.Oxygen };
export type MedicalEquipmentDetail = BaseItemDetail & {
  type: ItemType.MedicalEquipment;
};
export type SubscriptionDetail = BaseItemDetail & {
  type: ItemType.Subscription;
  subscription: SubscriptionItemDetail;
};

export type MetaItemObject =
  | MetaBloodItemDetail
  | MetaOxygenItemDetail
  | DrugItemDetail
  | MedicalEquipmentDetail
  | GroceryItemDetail
  | SubscriptionDetail;

export type ItemObject =
  | BloodItemDetail
  | OxygenItemDetail
  | DrugItemDetail
  | MedicalEquipmentDetail
  | GroceryItemDetail
  | SubscriptionDetail;

export interface LegacyItemProperties {
  action: ItemAction;
  item: ItemObject;
  search_id?: string;
  meta?: any;
}

export interface ReportObject {
  id: string;
  remarks: string;
  short_desc?: string;
}

export interface StoreObject {
  id: string;
  lat?: number;
  lon?: number;
}

export enum CartAction {
  AddItem = "add_item",
  RemoveItem = "remove_item",
}
export interface LegacyCartProperties {
  id: string;
  action: CartAction;
  item: ItemObject;
  cart_price: number;
  currency: CurrencyCode;
  meta?: any;
}

export enum ScanChannel {
  App = "app",
  Ussd = "ussd",
}

export enum ScanType {
  Pin = "pin",
  QrCode = "qr_code",
}

export enum ListAction {
  Add = "add_item",
  Discard = "discard",
  Edit = "edit_item",
  Remove = "remove_item",
  View = "view",
  Expired = "expired",
}

export enum ListType {
  Cart = "cart",
  Favourite = "favourite",
  Order = "order",
  Reminder = "reminder",
}

export enum ShopMode {
  Delivery = "delivery",
  Pickup = "pickup",
}

export interface LegacyCheckoutProperties {
  id: string; //order_id
  is_successful: boolean;
  cart_price: number;
  currency: CurrencyCode;
  items: Array<MetaItemObject>;
  cart_id: string;
  shop_mode?: ShopMode;
  meta?: any;
}

export enum DeliveryAction {
  Schedule = "schedule",
  Update = "update",
  Dispatch = "dispatch",
  Delivered = "delivered",
}

export interface LegacyDeliveryProperties {
  id: string;
  order_id: string;
  is_urgent: boolean; // true = emergency, false = schedule
  action: DeliveryAction;
  est_delivery_ts?: Date | string;
  delivery_coordinates?: CoordinatesObject;
  dispatch_coordinates?: CoordinatesObject;
  meta?: any;
}

export interface LegacyDrugProperties {
  market_id: string;
  name: string;
  description?: string;
  supplier_id: string;
  supplier_name: string;
  producer?: string;
  packaging?: string;
  active_ingredients: Array<string>;
  drug_form?: string;
  drug_strength?: string;
  atc_anatomical_group?: string;
  otc_or_ethical?: string;
}

/**
 * @internal
 */
export interface InternalDrugProperties extends LegacyDrugProperties {
  id: string;
}

export interface LegacyGroceryProperties {
  name: string;
  description?: string;
  category?: string;
  active_ingredients?: Array<string>;
  market_id?: string;
  packaging?: string;
  packaging_size?: number;
  packaging_units?: string;
  producer?: string;
  supplier_id?: string;
  supplier_name?: string;
}

/**
 * @internal
 */
export interface InternalGroceryProperties extends LegacyGroceryProperties {
  id: string;
}

export interface FacilityProperties {
  name: string;
  type?: string;
  country?: string;
  region_state?: string;
  city?: string;
  is_active?: boolean;
  has_delivery?: boolean;
  is_sponsored?: boolean;
}

/**
 * @internal
 */
export interface InternalFacilityProperties extends FacilityProperties {
  id: string;
}

export enum CancelType {
  Cart = "cart",
  Order = "order",
}

export interface LegacyCancelCheckoutProperties {
  id: string;
  type: CancelType;
  items: ItemMinObject[];
  reason: string;
  meta?: any;
}

export interface LegacyItemReportProperties {
  item: ItemMinObject;
  report_info: ReportObject;
  store_info: StoreObject;
}

export interface LegacyItemRequestProperties {
  id: string;
  item_name: string;
  manufacturer: string;
}

export interface LegacyItemVerificationProperties {
  scan_channel: ScanChannel;
  scan_type: ScanType;
  is_successful: boolean;
  item_info: ItemInfoObject;
}
