import { countries } from "../countries";

import {
  SegmentActionAttributeType,
  SegmentActionProperty,
  SegmentActionAttributeTag,
  SegmentAction,
  SegmentProperty,
  SegmentValue,
  SegmentActionAttribute,
  SegmentPropertyAttribute,
} from "@komi-app/fans-sdk";
import { CommonSelectOptions, SelectOption } from "../../types/select";
import { fetchPropValues, segmentAttributesToOptions } from "../../utils/attributes";
import { objectKeysToOptions } from "../../utils/select";
import { emailMarketingStatusMap } from "@komi-app/fans-sdk";
import { format } from "date-fns";

export const anySelectionValues = [CommonSelectOptions.Any];
export const anyOption: SelectOption<CommonSelectOptions> = {
  label: "Any",
  value: CommonSelectOptions.Any,
};

export const segmentAttributeActionFilterOptions: SelectOption<SegmentActionAttributeType>[] = [
  { label: "All actions", value: SegmentActionAttributeType.Any },
  { label: "Profile page actions", value: SegmentActionAttributeType.ProfilePage },
  { label: "Campaign actions", value: SegmentActionAttributeType.Campaign },
];

// TODO: As this requires auth, should this be moved and injected from
//   komi-client?
export const segmentActionAttributeMap: Record<SegmentAction, SegmentActionAttribute> = {
  [SegmentAction.ClickBandsintownRsvpLink]: {
    name: SegmentAction.ClickBandsintownRsvpLink,
    tags: [
      SegmentActionAttributeTag.Event,
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        name: SegmentActionProperty.VenueName,
        valueType: SegmentValue.Option,
        getValues: () => fetchPropValues("/api/segments/bandsintown/venue-names"),
      }
    ],
  },
  [SegmentAction.ClickBandsintownTicketLink]: {
    name: SegmentAction.ClickBandsintownTicketLink,
    tags: [
      SegmentActionAttributeTag.Event,
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/module-names?types=BANDSINTOWN"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.VenueName,
        getValues: () => fetchPropValues("/api/segments/bandsintown/venue-names"),
      },
    ],
  },
  [SegmentAction.ClickCustomEventTicketLink]: {
    name: SegmentAction.ClickCustomEventTicketLink,
    tags: [
      SegmentActionAttributeTag.Event,
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/module-names?types=EVENTS"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.LinkTitle,
        getValues: () => fetchPropValues("/api/segments/events/titles"),
      },
    ],
  },
  [SegmentAction.ClickCustomLink]: {
    name: SegmentAction.ClickCustomLink,
    tags: [
      SegmentActionAttributeTag.Link,
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/module-names?types=LINKS"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.LinkTitle,
        getValues: () => fetchPropValues("/api/segments/links/titles"),
      },
    ],
  },
  [SegmentAction.ClickMusicProvider]: {
    name: SegmentAction.ClickMusicProvider,
    tags: [
      SegmentActionAttributeTag.Music,
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/module-names?types=MUSIC"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.MusicTitle,
        getValues: () => fetchPropValues("/api/segments/music/titles"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.Platform,
        getValues: () => fetchPropValues("/api/segments/music/platforms"),
      },
    ],
  },
  [SegmentAction.ClickPlayFullPodcast]: {
    name: SegmentAction.ClickPlayFullPodcast,
    tags: [
      SegmentActionAttributeTag.Podcast,
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/module-names?types=PODCASTS"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.PodcastTitle,
        getValues: () => fetchPropValues("/api/segments/podcasts/titles"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.Platform,
        getValues: () => fetchPropValues("/api/segments/podcasts/platforms"),
      },
    ],
  },
  [SegmentAction.ClickPlayFullSong]: {
    name: SegmentAction.ClickPlayFullSong,
    tags: [
      SegmentActionAttributeTag.Music
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/module-names?types=MUSIC"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.MusicTitle,
        getValues: () => fetchPropValues("/api/segments/music/titles"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.Platform,
        getValues: () => fetchPropValues("/api/segments/music/platforms"),
      },
    ],
  },
  [SegmentAction.ClickPodcastProvider]: {
    name: SegmentAction.ClickPodcastProvider,
    tags: [
      SegmentActionAttributeTag.Podcast,
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/module-names?types=PODCASTS"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.PodcastTitle,
        getValues: () => fetchPropValues("/api/segments/podcasts/titles"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.Platform,
        getValues: () => fetchPropValues("/api/segments/podcasts/platforms"),
      },
    ],
  },
  [SegmentAction.ClickPresaveProvider]: {
    name: SegmentAction.ClickPresaveProvider,
    tags: [
      SegmentActionAttributeTag.Music,
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/module-names?types=MUSIC"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.MusicTitle,
        getValues: () => fetchPropValues("/api/segments/music/titles"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.Platform,
        getValues: () => fetchPropValues("/api/segments/music/platforms"),
      },
    ],
  },
  [SegmentAction.ClickProductLink]: {
    name: SegmentAction.ClickProductLink,
    tags: [
      SegmentActionAttributeTag.Store
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/module-names?types=PRODUCT"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ProductName,
        getValues: () => fetchPropValues("/api/segments/products/titles"),
      },
    ],
  },
  [SegmentAction.ClickShopMyShelfProduct]: {
    name: SegmentAction.ClickShopMyShelfProduct,
    tags: [
      SegmentActionAttributeTag.Store
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/module-names?types=SHOP_MY_SHELF"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ProductTitle,
        getValues: () => fetchPropValues("/api/segments/shopmyshelf/titles"),
      },
    ],
  },
  [SegmentAction.CopySpecialOfferCode]: {
    name: SegmentAction.CopySpecialOfferCode,
    tags: [
      SegmentActionAttributeTag.Link
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/links/special-offer/module-names"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.LinkTitle,
        getValues: () => fetchPropValues("/api/segments/links/special-offer/titles"),
      },
    ],
  },
  [SegmentAction.ClickSpecialOfferLink]: {
    name: SegmentAction.ClickSpecialOfferLink,
    tags: [
      SegmentActionAttributeTag.Link
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/links/special-offer/module-names"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.LinkTitle,
        getValues: () => fetchPropValues("/api/segments/links/special-offer/titles"),
      },
    ],
  },
  [SegmentAction.ClickSeatedPromotedLink]: {
    name: SegmentAction.ClickSeatedPromotedLink,
    tags: [
      SegmentActionAttributeTag.Event
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/module-names?types=SEATED"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.VenueName,
        getValues: () => fetchPropValues("/api/segments/seated/venue-names"),
      },
    ],
  },
  [SegmentAction.ClickSeatedTicketLink]: {
    name: SegmentAction.ClickSeatedTicketLink,
    tags: [
      SegmentActionAttributeTag.Event
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/module-names?types=SEATED"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.VenueName,
        getValues: () => fetchPropValues("/api/segments/seated/venue-names"),
      },
    ],
  },
  [SegmentAction.PlayYoutubeVideo]: {
    name: SegmentAction.PlayYoutubeVideo,
    tags: [
      SegmentActionAttributeTag.Video
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/module-names?types=VIDEO"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.VideoTitle,
        getValues: () => fetchPropValues("/api/segments/youtube/titles"),
      },
    ],
  },
  [SegmentAction.PreviewMusic]: {
    name: SegmentAction.PreviewMusic,
    tags: [
      SegmentActionAttributeTag.Music
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/module-names?types=MUSIC"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.MusicTitle,
        getValues: () => fetchPropValues("/api/segments/music/titles"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.Platform,
        getValues: () => fetchPropValues("/api/segments/music/platforms"),
      },
    ],
  },
  [SegmentAction.PreviewPodcast]: {
    name: SegmentAction.PreviewPodcast,
    tags: [
      SegmentActionAttributeTag.Podcast
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/module-names?types=PODCASTS"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.PodcastTitle,
        getValues: () => fetchPropValues("/api/segments/podcasts/titles"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.Platform,
        getValues: () => fetchPropValues("/api/segments/podcasts/platforms"),
      },
    ],
  },
  [SegmentAction.SuccessfullPresave]: {
    name: SegmentAction.SuccessfullPresave,
    tags: [
      SegmentActionAttributeTag.Music
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/module-names?types=MUSIC"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.MusicTitle,
        getValues: () => fetchPropValues("/api/segments/music/titles"),
      },
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.Platform,
        getValues: () => fetchPropValues("/api/segments/music/platforms"),
      },
    ],
  },
  [SegmentAction.ViewProfile]: {
    name: SegmentAction.ViewProfile,
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.PageName,
        getValues: () => fetchPropValues("/api/segments/profiles/page-names"), // TODO
      },
    ],
  },
  [SegmentAction.ClickSecretLink]: {
    name: SegmentAction.ClickSecretLink,
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/"), // TODO
      },
    ],
  },
  [SegmentAction.CopyCouponCode]: {
    name: SegmentAction.CopyCouponCode,
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.ModuleName,
        getValues: () => fetchPropValues("/api/segments/"), // TODO
      },
    ],
  },
  [SegmentAction.OpenEmail]: {
    name: SegmentAction.OpenEmail,
    tags: [
      SegmentActionAttributeTag.Campaign
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.CampaignName,
        getValues: () => fetchPropValues("/api/segments/campaigns/names"),
      },
    ],
  },
  [SegmentAction.ClickLinkInEmail]: {
    name: SegmentAction.ClickLinkInEmail,
    tags: [
      SegmentActionAttributeTag.Campaign
    ],
    clauseProperties: [
      {
        defaultValues: anySelectionValues,
        valueType: SegmentValue.Option,
        name: SegmentActionProperty.CampaignName,
        getValues: () => fetchPropValues("/api/segments/campaigns/names"),
      },
      // SegmentAttributePropertyName.EmailLink, // TODO
    ],
  },
};
export const segmentActionAttributes: SegmentActionAttribute[] = Object.values(segmentActionAttributeMap);

export const segmentPropertyAttributeMap: Partial<
  Record<SegmentProperty, SegmentPropertyAttribute>
> = {
  [SegmentProperty.Age]: {
    name: SegmentProperty.Age,
    defaultValues: ["18", "65"],
    valueType: SegmentValue.Number,
  },
  [SegmentProperty.Country]: {
    name: SegmentProperty.Country,
    defaultValues: anySelectionValues,
    valueType: SegmentValue.Option,
    getOptions: () => {
      const countryEntries = Object.entries(countries);
      const formattedCountries = countryEntries.map(([code, { fullName }]) => ({
        value: code,
        label: fullName,
      }));
      // alphabetical sorting
      return formattedCountries.sort((a, b) => a.label.localeCompare(b.label))
    }
  },
  [SegmentProperty.EmailMarketingStatus]: {
    name: SegmentProperty.EmailMarketingStatus,
    defaultValues: anySelectionValues,
    valueType: SegmentValue.Option,
    getOptions: () => objectKeysToOptions(emailMarketingStatusMap),
  },
  [SegmentProperty.SignUpDate]: {
    name: SegmentProperty.SignUpDate,
    defaultValues: [format(Date.now(), "yyyy-MM-dd"),format(Date.now(), "yyyy-MM-dd")],
    valueType: SegmentValue.Option,
    getOptions: () => objectKeysToOptions(emailMarketingStatusMap),
  },
  //
  // These are descoped, but may be useful in the future
  //
  // [SegmentProperty.Name]: {
  //   name: SegmentProperty.Name,
  //   valueType: SegmentValue.Option,
  //   getValues: () => fetchPropValues("/api/segments/names"),
  // },
  //
  // These are for > v1
  //
  // [SegmentProperty.City]: {
  //   name: SegmentProperty.City,
  //   valueType: SegmentValue.Option,
  //   getValues: () => fetchPropValues("/api/segments/cities"),
  // },
  // [SegmentProperty.OperatingSystem]: {
  //   name: SegmentProperty.OperatingSystem,
  //   valueType: SegmentValue.Option,
  //   getValues: () => fetchPropValues("/api/segments/operating-systems"),
  // },
  // [SegmentProperty.Gender]: {
  //   name: SegmentProperty.Gender,
  //   valueType: SegmentValue.Option,
  //   getValues: () => fetchPropValues("/api/segments/genders"),
  // },
  // [SegmentProperty.SignupSource]: {
  //   name: SegmentProperty.SignupSource,
  //   valueType: SegmentValue.Option,
  //   getValues: () => fetchPropValues("/api/segments/signup-sources"),
  // },
  // [SegmentProperty.ReceiveAnEmail]: {
  //   name: SegmentProperty.ReceiveAnEmail,
  //   valueType: SegmentValue.Boolean,
  // },
  // [SegmentProperty.CampaignEngagement]: {
  //   name: SegmentProperty.CampaignEngagement,
  //   valueType: SegmentValue.Option,
  // },
  // [SegmentProperty.ClickSocialLink]: {
  //   name: SegmentProperty.ClickSocialLink,
  //   valueType: SegmentValue.Option,
  // },
  // [SegmentProperty.OriginPlatform]: {
  //   name: SegmentProperty.OriginPlatform,
  //   valueType: SegmentValue.Option,
  // },
};
export const segmentPropertyAttributes: SegmentPropertyAttribute[] = Object.values(segmentPropertyAttributeMap);

export const segmentAttributeActionOptions = segmentAttributesToOptions(segmentActionAttributes);
export const segmentAttributePropertyOptions = segmentAttributesToOptions(
  segmentPropertyAttributes,
  "Audience Properties",
);
