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

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

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

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

export default async function CurrentBenefitsBlock({
  title,
  subtitle,
  link,
}: AccountPageComponentProps) {
  const user = await getProfile()
  // TAKE NOTE: we need clarification on how benefits stack from different levels
  // in order to determine if a benefit is specific to a level or if it is a cumulative benefit
  // we might have to add a new boolean property "exclusive" or similar
  if (!user || "error" in user || !user.membership) {
    return null
  }

  const currentLevel = getMembershipLevelObject(
    user.membership.membershipLevel as MembershipLevelEnum,
    getLang()
  )
  if (!currentLevel) {
    //  TODO: handle this case?
    return null
  }

  return (
    <SectionContainer>
      <SectionHeader title={title} link={link} preamble={subtitle} />
      <Grids.Stackable>
        {currentLevel.benefits.map((benefit, idx) => (
          <article className={styles.card} key={`${currentLevel}-${idx}`}>
            <Title
              as="h4"
              level="h3"
              textAlign="center"
              textTransform="regular"
            >
              {benefit.title}
            </Title>
          </article>
        ))}
      </Grids.Stackable>
      <SectionLink link={link} variant="mobile" />
    </SectionContainer>
  )
}
