import { Lock } from "react-feather"

import { MembershipLevelEnum } from "@/constants/membershipLevels"
import { serverClient } from "@/lib/trpc/server"

import SectionContainer from "@/components/Section/Container"
import SectionHeader from "@/components/Section/Header"
import SectionLink from "@/components/Section/Link"
import Chip from "@/components/TempDesignSystem/Chip"
import Grids from "@/components/TempDesignSystem/Grids"
import Body from "@/components/TempDesignSystem/Text/Body"
import Title from "@/components/TempDesignSystem/Text/Title"
import { getIntl } from "@/i18n"
import { getLang } from "@/i18n/serverContext"
import { getMembershipLevelObject } from "@/utils/membershipLevel"

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

import type { AccountPageComponentProps } from "@/types/components/myPages/myPage/accountPage"

export default async function NextLevelBenefitsBlock({
  title,
  subtitle,
  link,
}: AccountPageComponentProps) {
  const intl = await getIntl()
  const user = await serverClient().user.get()
  if (!user || "error" in user) {
    return null
  }
  const nextLevel = getMembershipLevelObject(
    user.memberships[0].nextLevel as MembershipLevelEnum,
    getLang()
  )
  if (!nextLevel) {
    //  TODO: handle this case, when missing or when user is top level?
    return null
  }
  // TODO: how to handle different count of unlockable benefits?
  return (
    <SectionContainer>
      <SectionHeader title={title} preamble={subtitle} link={link} />
      <Grids.Stackable columns={2}>
        {nextLevel.benefits.map((benefit) => (
          <article key={benefit.title} className={styles.card}>
            <Chip>
              <Lock height={16} />
              {intl.formatMessage({ id: "Level up to unlock" })}
            </Chip>
            <div className={styles.textContainer}>
              <Body color="peach50" textAlign="center">
                {intl.formatMessage(
                  { id: "As our" },
                  { level: nextLevel.name }
                )}
              </Body>{" "}
              <Title level="h4" as="h5" color="pale" textAlign="center">
                {benefit.title}
              </Title>
            </div>
          </article>
        ))}
      </Grids.Stackable>
      <SectionLink link={link} variant="mobile" />
    </SectionContainer>
  )
}
