import { env } from "@/env/server"
import { serverClient } from "@/lib/trpc/server"

import ClaimPoints from "@/components/Blocks/DynamicContent/Points/ClaimPoints"
import { Section } from "@/components/Section"
import SectionHeader from "@/components/Section/Header/Deprecated"
import SectionLink from "@/components/Section/Link"

import { Cards } from "./Cards"
import { ClientPreviousStays } from "./Client"

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

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

export default async function PreviousStays({
  title,
  link,
}: AccountPageComponentProps) {
  const caller = await serverClient()
  const initialPreviousStays = await caller.user.stays.previous({
    limit: 6,
  })

  if (!initialPreviousStays?.data.length) {
    return null
  }

  const StaysComponent = env.NEW_STAYS_ON_MY_PAGES ? Cards : ClientPreviousStays

  return (
    <Section>
      <div className={styles.header}>
        <SectionHeader title={title} link={link} />
        <ClaimPoints />
      </div>
      <StaysComponent initialPreviousStays={initialPreviousStays} />
      <SectionLink link={link} variant="mobile" />
    </Section>
  )
}
