import { Suspense } from "react"

import { env } from "@/env/server"

import HowItWorks from "@/components/Blocks/DynamicContent/HowItWorks"
import LoyaltyLevels from "@/components/Blocks/DynamicContent/LoyaltyLevels"
import Overview from "@/components/Blocks/DynamicContent/Overview"
import OverviewTable from "@/components/Blocks/DynamicContent/OverviewTable"
import EarnAndBurn from "@/components/Blocks/DynamicContent/Points/EarnAndBurn"
import ExpiringPoints from "@/components/Blocks/DynamicContent/Points/ExpiringPoints"
import PointsOverview from "@/components/Blocks/DynamicContent/Points/Overview"
import CurrentRewardsBlock from "@/components/Blocks/DynamicContent/Rewards/CurrentRewards"
import NextLevelRewardsBlock from "@/components/Blocks/DynamicContent/Rewards/NextLevel"
import SASLinkedAccount from "@/components/Blocks/DynamicContent/SAS/LinkedAccounts"
import SASTierComparisonBlock from "@/components/Blocks/DynamicContent/SASTierComparison"
import SignupFormWrapper from "@/components/Blocks/DynamicContent/SignupFormWrapper"
import PreviousStays from "@/components/Blocks/DynamicContent/Stays/Previous"
import SoonestStays from "@/components/Blocks/DynamicContent/Stays/Soonest"
import UpcomingStays from "@/components/Blocks/DynamicContent/Stays/Upcoming"
import LoadingSpinner from "@/components/LoadingSpinner"

import JobylonFeed from "./JobylonFeed"

import type { DynamicContentProps } from "@/types/components/blocks/dynamicContent"
import { DynamicContentEnum } from "@/types/enums/dynamicContent"

export default function DynamicContent(props: DynamicContentProps) {
  return (
    <Suspense fallback={<LoadingSpinner />}>
      <DynamicContentBlocks {...props} />
    </Suspense>
  )
}

function DynamicContentBlocks(props: DynamicContentProps) {
  const { dynamic_content, firstItem } = props
  switch (dynamic_content.component) {
    case DynamicContentEnum.Blocks.components.current_benefits:
      return <CurrentRewardsBlock {...dynamic_content} />
    case DynamicContentEnum.Blocks.components.earn_and_burn:
      return <EarnAndBurn {...dynamic_content} />
    case DynamicContentEnum.Blocks.components.expiring_points:
      return env.HIDE_FOR_NEXT_RELEASE ? null : (
        <ExpiringPoints {...dynamic_content} />
      )
    case DynamicContentEnum.Blocks.components.how_it_works:
      return (
        <HowItWorks dynamic_content={dynamic_content} firstItem={firstItem} />
      )
    case DynamicContentEnum.Blocks.components.jobylon_feed:
      return <JobylonFeed {...dynamic_content} />
    case DynamicContentEnum.Blocks.components.loyalty_levels:
      return (
        <LoyaltyLevels
          dynamic_content={dynamic_content}
          firstItem={firstItem}
        />
      )
    case DynamicContentEnum.Blocks.components.membership_overview:
      return <Overview {...dynamic_content} />
    case DynamicContentEnum.Blocks.components.next_benefits:
      return <NextLevelRewardsBlock {...dynamic_content} />
    case DynamicContentEnum.Blocks.components.overview_table:
      return (
        <OverviewTable
          dynamic_content={dynamic_content}
          firstItem={firstItem}
        />
      )
    case DynamicContentEnum.Blocks.components.points_overview:
      return <PointsOverview {...dynamic_content} />
    case DynamicContentEnum.Blocks.components.previous_stays:
      return <PreviousStays {...dynamic_content} />
    case DynamicContentEnum.Blocks.components.sign_up_form:
      return <SignupFormWrapper dynamic_content={dynamic_content} />
    case DynamicContentEnum.Blocks.components.soonest_stays:
      return <SoonestStays {...dynamic_content} />
    case DynamicContentEnum.Blocks.components.upcoming_stays:
      return <UpcomingStays {...dynamic_content} />
    case DynamicContentEnum.Blocks.components.sas_linked_account:
      return <SASLinkedAccount {...dynamic_content} />
    case DynamicContentEnum.Blocks.components.sas_tier_comparison:
      return (
        <SASTierComparisonBlock
          title={dynamic_content.title}
          preamble={dynamic_content.subtitle}
        />
      )
    default:
      return null
  }
}
