import { Experience } from "redux/Experience/types";
import { SocialProfileLinkTypes, UserProfile } from "redux/User/types";
import {
  DataCaptureMethod,
  DataCaptureType,
  Theme,
} from "@komi-app/components";

import {
  ModuleType,
  DataCaptureFieldType,
  MusicReleaseScope,
  MusicReleaseType,
  MusicReleaseMode,
  LegalsType,
  DataCaptureFormType,
} from "@komi-app/shared-types";
import { CommissionType } from "@komi-app/creator-ui";
import { AffiliateProduct } from "@komi-app/affiliate-sdk";
import { AffiliateNetworkType, AutomationMode } from "@komi-app/profiles-sdk";

export type TalentModuleMixItem =
  | ProductItem
  | LinkItem
  | MusicItem
  | OnDemandVideoItem
  | YoutubeVideoItem
  | ExperienceItem
  | PodcastItem
  | FanClubItem
  | BandsintownItem
  | ShopifyProductItem
  | ShopifyModuleItem
  | EventItem
  | DataCaptureFormItem
  | GroupItem;

export type ThirdPartyModuleType =
  | ModuleType.SHOP_MY_SHELF
  | ModuleType.SHOP_LIST
  | ModuleType.BANDSINTOWN
  | ModuleType.SEATED
  | ModuleType.YOUTUBE_COLLECTION
  | ModuleType.PODCAST_AUTOMATION;

// modules profile
export interface CreateTalentProfileModuleRequest<T> {
  order: number;
  type: ModuleType;
  items: T;
  expand?: boolean;
  isEdit?: boolean;
  isCreate?: boolean;
}

export interface UpdateTalentProfileModuleRequest<T> {
  id: string;
  order: number;
  type: ModuleType;
  items: T[];
  expand?: boolean;
  isEdit?: boolean;
  isCreate?: boolean;
}

export interface ReorderTalentProfileModuleRequest {
  id: string;
  order: number;
}

export interface ReorderShopifyStoreRequest {
  shop: string;
  order: number;
}

export interface autoSaveProfileRequest {
  localizationId: string;
  localizationIds?: string[];
  profile: UserProfile;
  modules: TalentProfileModule<TalentModuleMixItem>[];
  updatedById?: number;
  digitalProductsFilesToUpload?: Record<string, File>;
  updateTalentId?: string;
  onNetworkError?: () => void;
  onSuccess?: () => void;
}
export interface publishProfileRequest {
  localizationId: string[];
  publishTalentId?: number;
  onNetworkError?: () => void;
  onSuccess?: () => void;
}
export interface DeleteLocalizationRequest {
  talentProfileId: string;
  localizationId: string;
  onNetworkError?: () => void;
  onSuccess?: () => void;
  onError?: () => void;
}
export interface requestOTPRequest {
  method: "phone" | "email";
  phone?: string;
  email?: string;
  userId?: number;
}
export interface TFAVerificationRequest {
  method: "phone" | "email";
  phone?: string;
  id?: string;
  otp?: string;
}

export interface BaseItem {
  id?: string;
  order?: number;
  isUpdate?: boolean;
  isCreate?: boolean;
  isSuccess?: boolean;
  error?: any;
  visible?: boolean;
}

export interface LinkItem extends BaseItem {
  url?: string;
  title?: string;
  thumbnail?: string;
  specialOffer?: {
    storeUrl?: string;
    title?: string;
    thumbnail?: string;
    couponCode?: string;
  };
}

export interface TextItem extends BaseItem {
  content: string;
}

export interface MediaGalleryItem extends BaseItem {
  src: string;
  thumbnailSrc?: string;
  contentType: string;
  originalFilename: string;
  caption?: string;
  url?: string;
}

export interface TipsLinkItem extends BaseItem {
  src: string;
  thumbnailSrc?: string;
  contentType: string;
  originalFilename: string;
  title?: string;
  url?: string;
  platform?: string;
}

export type AffiliateItem = BaseItem &
  AffiliateProduct & {
    trackingUrl: string;
    affiliateNetwork?: AffiliateNetworkType;
    affiliateMetadata?: Record<string, any> & AffiliateMetadata;
  };

export interface AffiliateMetadata {
  commissionRate: number;
  commissionType: CommissionType;
}

export interface BandsintownItem extends BaseItem {
  url?: string;
  metadata?: {
    name: string;
    id: string;
  };
}
export interface SeatedItem extends BaseItem {
  seatedId?: string;
}
export interface ShopMyShelfItem extends BaseItem {
  url?: string;
}
export interface ShopListItem extends BaseItem {
  url?: string;
}
export interface YoutubeItem extends BaseItem {
  url?: string;
  totalSelected?: number;
  sort?: string;
  metadata?: Record<string, any>;
}

export interface TikTokItem extends BaseItem {
  url: string;
}

export interface TikTokAutomationItem extends BaseItem {
  mode: AutomationMode;
  tiktokAccountId: string;
  links: TikTokItem[];
}

export interface InstagramReelItem extends BaseItem {
  url: string;
}

export interface ProductItem extends BaseItem {
  url?: string;
  title?: string;
  thumbnail?: string;
  price?: number;
  currency?: string;
}
export interface EventItem extends BaseItem {
  eventDate?: string;
  venueName?: string;
  location?: string;
  ticketLink?: string;
  soldOut?: boolean;
}

export interface ShopifyProductItem extends BaseItem {
  id?: string;
  shop?: string;
  itemIds?: string[];
  collectionId?: string;
  images: { src: string }[];
  title: string;
  variants: ShopifyVariant[];
}

export interface ShopifyVariant {
  price: string;
  priceV2: {
    currencyCode: string;
    amount: string;
  };
}

export interface ShopifyModuleItem extends BaseItem {
  shop?: string;
  itemIds?: string[];
  collectionId?: string;
  order?: number;
}

export interface MusicItemLink extends BaseItem {
  url?: string;
  isVisible?: boolean;
  isTemporary?: boolean;
  artistsToFollow?: MusicArtist[];
  type?: SocialProfileLinkTypes;
}

export interface MusicArtist {
  name: string;
  url: string;
}
export interface MusicItem extends BaseItem {
  id?: string;
  metadata: Record<string, any>;
  links: MusicItemLink[];
  order?: number;
  urlSlug?: string;
  type?: MusicReleaseType;
  releaseDate?: string;
  releaseType?: MusicReleaseMode;
  releaseScope?: MusicReleaseScope;
  timezone?: string;
  persistentId: string;
}

export interface PodcastItemLink extends BaseItem {
  url?: string;
  type?: "SPOTIFY" | "STITCHER" | "APPLE_PODCAST" | "OVERCAST" | "AUDIBLE";
}

export interface PodcastItem extends BaseItem {
  id?: string;
  metadata?: Record<string, any>;
  links: PodcastItemLink[];
  numberOfEpisodes?: number;
  activeLatestPodcast?: boolean;
  isDifferent?: boolean;
  customUrl?: string;
}

export interface FanClubItem extends BaseItem {
  id?: string;
  thumbnail?: string;
}

export interface OnDemandVideoItem extends Experience, BaseItem {
  experienceId?: string;
}

export interface YoutubeVideoItem extends BaseItem {
  url?: string;
  metadata?: Record<string, any>;
}

export interface ExperienceItem extends Experience, BaseItem {
  experienceId?: string;
}

export enum DATA_FORM_LAYOUT {
  left = "left",
  right = "right",
}

export interface DataFormFieldItem {
  name: string;
  label: string;
  required: boolean;
  type: DataCaptureFieldType;
  legalType?: LegalsType;
  options: OptionItem[];
  text?: string;
  order: number;
}

export interface OptionItem {
  value?: string;
  label: string;
  checked: boolean;
  order: number;
}

/*
 * <LEGACY DATA CAPTURE>
 */
export interface DataFormOptionItem extends OptionItem {
  edit?: boolean;
}
export interface DataCaptureFieldItem
  extends Omit<DataFormFieldItem, "options"> {
  edit?: boolean;
  options?: DataFormOptionItem[];
}

export interface DataCaptureFormItem extends BaseItem {
  layout: DATA_FORM_LAYOUT;
  hasImage: boolean;
  enableImage?: boolean;
  image?: string;
  title: string;
  subTitle?: string;
  form: {
    title?: string;
    subTitle?: string;
    name: string;
    fields: DataFormFieldItem[];
  };
  formType?: DataCaptureFormType;
  emailNotifications?: boolean;
}
/*
 * </LEGACY DATA CAPTURE>
 */

/*
 * </NEW DATA CAPTURE>
 */
export interface DataCaptureActivationItemNew extends BaseItem {
  type: ModuleType;
  name: string;
  order: number;

  secretCode?: string;
  secretLink?: string;
  imagePath?: string;
  isImageVisible: boolean;
  title: string;
  subtitle?: string;
  countdownExpiry?: number;
  countdownTimeZone?: string;
  methods: DataCaptureMethod[];
  theme?: Theme;
  moduleType: DataCaptureType;
  isPaused?: boolean;
}
/*
 * </NEW DATA CAPTURE>
 */
export type GroupItem = TalentProfileModule<TalentModuleMixItem>;

export interface TalentProfileModule<T> {
  id?: string;
  name: string;
  type: ModuleType | string;
  items: T[];
  order: number;
  expand?: boolean;
  isEdit?: boolean; // check state editing
  isUpdate?: boolean; // check to send dispatch action update
  isCreate?: boolean; // check to send dispatch action create
  isLoading?: boolean;
  talentProfileId?: string;
  localizationId?: string;
  groupId?: string | null;
  showTitle?: boolean;
  template?: boolean;
  visible: boolean;
}
export type TalentProfileModuleGroup<T> = TalentProfileModule<
  TalentProfileModule<T>
>;

export class TalentProfileModuleObj<T> implements TalentProfileModule<T> {
  id?: string;
  name: string;
  type: ModuleType | string;
  items: T[];
  order: number;
  expand?: boolean;
  isEdit?: boolean;
  isUpdate?: boolean;
  isCreate?: boolean;
  isLoading?: boolean;
  visible: boolean;

  constructor(profileModule?: TalentProfileModule<T>) {
    this.id = profileModule && profileModule.id ? profileModule.id : "";
    this.name = profileModule && profileModule.name ? profileModule.name : "";
    this.type = profileModule ? profileModule.type : "";
    this.items = profileModule ? profileModule.items : [];
    this.order = profileModule ? profileModule.order : -1;
    this.expand =
      profileModule && profileModule.expand ? profileModule.expand : true;
    this.isEdit =
      profileModule && profileModule.isEdit ? profileModule.isEdit : false;
    this.isUpdate =
      profileModule && profileModule.isUpdate ? profileModule.isUpdate : false;
    this.isCreate =
      profileModule && profileModule.isCreate ? profileModule.isCreate : false;
    this.isLoading =
      profileModule && profileModule.isLoading
        ? profileModule.isLoading
        : false;
    this.visible =
      profileModule && profileModule.visible ? profileModule.visible : true;
  }
}
