import type { ReactNode } from "react";
import type { LayoutProps } from '@/core/types/common';

import { auth } from "@/auth";
import { permanentRedirect } from "next/navigation";
import { getInitialServerPathname } from "@/core/utils/helpers/cookies";

import AdminPanel from "@/core/common/AdminPanel";

export const dynamic = "force-dynamic";
/**
 * Profile layout. It's specifics to authorized users to manage their account & personal data.\
 *
 * @param {LayoutProps & ParallelSlots<"sidebar">} props   common {@link LayoutProps} props object containing `children`
 *                                                         and the extended {@link ParallelSlots} to allow the parallel
 *                                                         slots in the layout segment.
 * @return {Promise<React.ReactNode>}
 * @constructor
 */
export default async function Layout({ children }: LayoutProps): Promise<ReactNode | never> {
  const pathname = await getInitialServerPathname();
  const session = await auth();

  if (!session) {
    return permanentRedirect("/auth/sign-in");
  }

  return (
    <div className={"h-screen flex flex-col items-center bg-[#F6F6F6] px-4 overflow-hidden"}>
      <div className={"flex flex-col w-full max-w-[1440px] pt-5"}>
        <AdminPanel session={session}
                    initialPathname={pathname} />
        <section className={"flex-grow justify-center"}>
          <div className={"h-full pt-20"}>
            {children}
          </div>
        </section>
      </div>
    </div>
  );
}
