/*
  @author: Hemal
  File: page.tsx
  Purpose: This file contains the main content for the Find a home page.
  Description: 
    This page is for the Find a home.
    It includes a banner with a title and a link to the list a home page.
    It also includes a section with the Find a home.
*/
import { getDictionarys } from "@/getDictionarys";
import PropertyDetailsMain from "@/src/components/page/FindAHome/PropertyDetails/PropertyDetails";
import getProfileView from "@/src/ssrapi/getProfileView";
import getPropertyDetails from "@/src/ssrapi/getPropertyDetails";
import { PropertyResponse } from "@/src/types/api/property.type";
import { FindAHomeContent } from "@/src/types/StaticContent/findahome.content.type";
import { PropertyDetailContent } from "@/src/types/StaticContent/propertydetail.content.type";
import { getCapitalizeHyphenated } from "@/src/utils/commonFunctions";
import Routes from "@/src/utils/RouteConstants";
import { notFound } from "next/navigation";
import React from "react";

// Slug params
interface SlugParams {
  params: {
    slug: string;
  };
  searchParams: { [key: string]: string | string[] | undefined };
}

const page: React.FC<SlugParams> = async ({ params, searchParams }) => {
  const { slug } = await params;
  const searchParamsObj = await searchParams;
  const referrer = searchParamsObj.referrer as string;
  const PropertyDetails = await getPropertyDetails(slug) as PropertyResponse;
  const FindAHomeContent = await getDictionarys('find-a-home') as FindAHomeContent;
  const PropertyDetailContent = await getDictionarys('property-detail') as PropertyDetailContent;
  const ProfileData = JSON.parse(JSON.stringify(await getProfileView() || null));

  //if no data found, then show not found page
  if (!PropertyDetails?.data) {
    notFound();
  }

  let breadcrumbData = [];
  if (referrer === PropertyDetailContent.BREADCRUMB_DATA.Home) {
    breadcrumbData = [
      {
        Link: PropertyDetailContent.BREADCRUMB_DATA.HOME,
        LinkUrl: Routes.HOME,
      },
      {
        Link: PropertyDetailContent.BREADCRUMB_DATA.FIND_A_HOME,
        LinkUrl: Routes.FIND_A_HOME,
      },
      {
        ActiveLink: getCapitalizeHyphenated(slug),
      },
    ];
  } else {
    breadcrumbData = [
      {
        Link: PropertyDetailContent.BREADCRUMB_DATA.FIND_A_HOME,
        LinkUrl: Routes.FIND_A_HOME,
      },
      {
        ActiveLink: getCapitalizeHyphenated(slug),
      },
    ];
  }


  return (
    <>
      <PropertyDetailsMain
        breadcrumbData={breadcrumbData}
        slug={slug}
        PropertyDetails={PropertyDetails}
        PropertyDetailContent={PropertyDetailContent}
        ProfileData={ProfileData}
        FindAHomeContent={FindAHomeContent}
      />
    </>
  );
};

export default page;
