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

import { getIntl } from "@/i18n"
import { getMembership } from "@/utils/user"

import PointsContainer from "../../../Overview/Stats/Points/Container"
import {
  NextLevelNightsColumn,
  NextLevelPointsColumn,
  StayOnLevelColumn,
  YourPointsColumn,
} from "../../../Overview/Stats/Points/PointsColumn"

import { UserProps } from "@/types/components/myPages/user"
import { LangParams } from "@/types/params"

/* TODO */
export default async function Points({ user, lang }: UserProps & LangParams) {
  const intl = await getIntl()

  const membership = getMembership(user.memberships)
  if (!membership?.nextLevel || !MembershipLevelEnum[membership.nextLevel]) {
    return null
  }
  const nextLevel = await serverClient().contentstack.loyaltyLevels.byLevel({
    level: MembershipLevelEnum[membership.nextLevel],
  })
  return (
    <PointsContainer>
      <YourPointsColumn points={membership?.currentPoints} />
      {nextLevel && (
        <>
          {membership?.currentPoints ? (
            <StayOnLevelColumn
              points={membership?.currentPoints} //TODO
              subtitle={`${intl.formatMessage({ id: "by" })} ${membership?.expirationDate}`}
            />
          ) : (
            <>
              <NextLevelPointsColumn
                points={membership?.pointsRequiredToNextlevel}
                subtitle={`${intl.formatMessage({ id: "next level:" })} ${nextLevel.name}`}
              />
              {membership?.nightsToTopTier && (
                <NextLevelNightsColumn
                  nights={membership.nightsToTopTier}
                  subtitle={
                    membership.tierExpirationDate &&
                    `by ${membership.tierExpirationDate}`
                  }
                />
              )}
            </>
          )}
        </>
      )}
    </PointsContainer>
  )
}
