import {
  type MembershipLevel,
  MembershipLevelEnum,
} from "@/constants/membershipLevels"

import type {
  EurobonusMembership,
  Membership,
  NativeFriendsMembership,
  User,
  UserLoyalty,
} from "@/types/user"

export enum scandicMembershipTypes {
  SCANDIC_NATIVE = "SCANDIC_NATIVE",
  SAS_EB = "SAS_EB",
}

export function isScandicNativeMembership(
  membership: Membership
): membership is NativeFriendsMembership {
  return membership.type === scandicMembershipTypes.SCANDIC_NATIVE
}

export function getFriendsMembership(userLoyalty: UserLoyalty) {
  const { memberships, ...loyalty } = userLoyalty

  const friendsMembership = memberships.find(isScandicNativeMembership)

  if (!friendsMembership) return null

  const pointExpiration = loyalty.pointExpirations
    .sort(
      (a, b) => new Date(a.expires).getTime() - new Date(b.expires).getTime()
    )
    .at(0)

  // Map to the same format that was used with Profile V1 to avoid larger changes for now.
  const result = {
    membershipType: friendsMembership.type,
    membershipNumber: friendsMembership.membershipNumber,
    membershipLevel: loyalty.tier,
    nextLevel: friendsMembership.nextTier,
    currentPoints: loyalty.points.spendable,
    expirationDate: loyalty.tierExpires,
    nightsToTopTier: friendsMembership.nightsToTopTier,
    pointsRequiredToNextlevel: friendsMembership.pointsToNextTier,
    tierExpirationDate: loyalty.tierExpires,
    pointsExpiryDate: pointExpiration?.expires,
    pointsToExpire: pointExpiration?.points,
    memberSince: friendsMembership.memberSince,
  }
  return result
}

function isEurobonusMembership(
  membership: Membership
): membership is EurobonusMembership {
  return membership.type === scandicMembershipTypes.SAS_EB
}

export function getEurobonusMembership(loyalty: UserLoyalty) {
  return loyalty.memberships?.find(isEurobonusMembership)
}

export function getMembershipCards(userLoyalty: UserLoyalty) {
  return userLoyalty.memberships
    .filter(
      (membership) => membership.type !== scandicMembershipTypes.SCANDIC_NATIVE
    )
    .map((membership) => ({
      currentPoints: 0, // We only have points for Friends so we can't set this for now
      expirationDate: membership.tierExpires,
      membershipNumber: membership.membershipNumber,
      membershipType: membership.type,
      memberSince: membership.memberSince,
    }))
}

export function isHighestMembership(
  membershipLevel: MembershipLevel | undefined
) {
  return membershipLevel == MembershipLevelEnum.L7
}

export function getInitials(
  firstName: User["firstName"],
  lastName: User["lastName"]
) {
  if (!firstName || !lastName) return null
  const firstInitial = firstName.charAt(0).toUpperCase()
  const lastInitial = lastName.charAt(0).toUpperCase()
  return `${firstInitial}${lastInitial}`
}

export function getSteppedUpLevel(
  currentValue: MembershipLevel,
  stepsUp: number
): MembershipLevel {
  const values = Object.values(MembershipLevelEnum)
  const currentIndex = values.indexOf(currentValue as MembershipLevelEnum)
  if (currentIndex === -1 || currentIndex === values.length - 1) {
    return currentValue
  }
  return values[currentIndex + stepsUp]
}
