import { env } from "@/env/server"
import {
  getCurrentRewards,
  getMembershipLevel,
} from "@/lib/trpc/memoizedRequests"

import SectionContainer from "@/components/Section/Container"
import SectionHeader from "@/components/Section/Header"
import SectionLink from "@/components/Section/Link"

import ClientCurrentRewards from "./Client"

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

export default async function CurrentRewardsBlock({
  title,
  subtitle,
  link,
}: AccountPageComponentProps) {
  const [rewardsResponse, membershipLevel] = await Promise.all([
    getCurrentRewards(),
    getMembershipLevel(),
  ])

  if (!rewardsResponse?.rewards.length) {
    return null
  }

  return (
    <SectionContainer>
      <SectionHeader title={title} link={link} preamble={subtitle} />
      <ClientCurrentRewards
        rewards={rewardsResponse.rewards}
        pageSize={6}
        showRedeem={env.USE_NEW_REWARDS_ENDPOINT && env.USE_NEW_REWARD_MODEL}
        membershipNumber={membershipLevel?.membershipNumber}
      />
      <SectionLink link={link} variant="mobile" />
    </SectionContainer>
  )
}
