import { MembershipLevelEnum } from "@scandic-hotels/common/constants/membershipLevels"
import { Divider } from "@scandic-hotels/design-system/Divider"
import { MaterialIcon } from "@scandic-hotels/design-system/Icons/MaterialIcon"
import { Typography } from "@scandic-hotels/design-system/Typography"
import { isBoostedBySas } from "@scandic-hotels/trpc/routers/user/helpers"

import { membershipLevels } from "@/constants/membershipLevels"
import { getUsePointsModal } from "@/lib/trpc/memoizedRequests"

import MembershipLevelIcon from "@/components/Levels/Icon"
import { getIntl } from "@/i18n"

import { UsePointsModal } from "./UsePoints/UsePointsModal"
import SasBoostStatus from "./SasBoostStatus"

import styles from "./membershipOverviewCard.module.css"

import type { User } from "@scandic-hotels/trpc/types/user"

interface MembershipOverviewCardProps {
  user: User
}

export default async function MembershipOverviewCard({
  user,
}: MembershipOverviewCardProps) {
  const intl = await getIntl()

  const usePointsData = await getUsePointsModal()

  if (!user.membership?.membershipLevel) {
    return null
  }

  const pointsToSpendText =
    typeof user.membership.currentPoints === "number"
      ? intl.formatNumber(user.membership.currentPoints)
      : intl.formatMessage({ id: "common.NA", defaultMessage: "N/A" })

  const boostedBySas = user.loyalty ? isBoostedBySas(user.loyalty) : null

  return (
    <section className={styles.card} aria-labelledby="membership-level">
      <header className={styles.membershipHeader}>
        {boostedBySas ? (
          <MaterialIcon icon="travel" size={20} color="Icon/Accent" />
        ) : null}
        <Typography variant="Title/Overline/sm">
          <h2 className={styles.headingText} id="membership-level">
            {intl.formatMessage(
              {
                id: "common.membershipLevelWithValue",
                defaultMessage: "Level {level}",
              },
              { level: membershipLevels[user.membership.membershipLevel] }
            )}
          </h2>
        </Typography>

        {user.loyalty && boostedBySas ? (
          <SasBoostStatus expiryDate={user.loyalty?.tierExpires} />
        ) : null}
      </header>

      <MembershipLevelIcon
        level={MembershipLevelEnum[user.membership.membershipLevel]}
        height="44"
        width="268"
        rows={1}
      />

      <Divider
        className={styles.divider}
        color="Border/Divider/Brand/OnPrimary 3/Default"
      />

      <div className={styles.bottom}>
        <div>
          <Typography variant="Title/Overline/sm">
            <h3 className={styles.headingText}>
              {intl.formatMessage({
                id: "common.pointsToSpend",
                defaultMessage: "Points to spend",
              })}
            </h3>
          </Typography>
          <Typography variant="Title/lg">
            <p className={styles.pointsValue}>{pointsToSpendText}</p>
          </Typography>
        </div>
        {user.membership.currentPoints > 0 && usePointsData && (
          <UsePointsModal
            buttonVariant="Primary"
            contentData={usePointsData}
            points={user.membership.currentPoints}
          />
        )}
      </div>
    </section>
  )
}
