import SectionContainer from "@/components/Section/Container"
import SectionHeader from "@/components/Section/Header"
import Card from "@/components/TempDesignSystem/Card"
import Grids from "@/components/TempDesignSystem/Grids"
import LoyaltyCard from "@/components/TempDesignSystem/LoyaltyCard"
import TeaserCard from "@/components/TempDesignSystem/TeaserCard"

import type { CardsGridProps } from "@/types/components/blocks/cardsGrid"
import { CardsGridEnum, CardsGridLayoutEnum } from "@/types/enums/cardsGrid"
import type { StackableGridProps } from "../TempDesignSystem/Grids/Stackable/stackable"

export default function CardsGrid({
  cards_grid,
  firstItem = false,
}: CardsGridProps) {
  let columns: StackableGridProps["columns"]

  switch (cards_grid.layout) {
    case CardsGridLayoutEnum.ONE_COLUMN:
      columns = 1
      break
    case CardsGridLayoutEnum.TWO_COLUMNS:
      columns = 2
      break
    case CardsGridLayoutEnum.THREE_COLUMNS:
      columns = 3
      break
    default:
      columns = 3
  }

  return (
    <SectionContainer>
      <SectionHeader
        title={cards_grid.title}
        preamble={cards_grid.preamble}
        topTitle={firstItem}
      />
      <Grids.Stackable columns={columns}>
        {cards_grid.cards.map((card) => {
          switch (card.__typename) {
            case CardsGridEnum.cards.Card:
              return (
                <Card
                  theme={cards_grid.theme ?? "one"}
                  key={card.system.uid}
                  scriptedTopTitle={card.scripted_top_title}
                  bodyText={card.body_text}
                  {...card}
                />
              )
            case CardsGridEnum.cards.TeaserCard:
              return (
                <TeaserCard
                  key={card.system.uid}
                  title={card.heading}
                  description={card.body_text}
                  {...card}
                />
              )
            case CardsGridEnum.cards.LoyaltyCard:
              return (
                <LoyaltyCard
                  key={card.system.uid}
                  bodyText={card.body_text}
                  {...card}
                />
              )
          }
        })}
      </Grids.Stackable>
    </SectionContainer>
  )
}
