"use server";
cache: "no-store";
import AuthHandler from "@/components/server/admin/authHandler";
import Sidebar, { SidebarEntry } from "@/components/server/admin/views/sidebar";
import { ProjectView } from "@/components/views/admin/project";
import { PostView } from "@/components/views/admin/post";
import { redirect } from 'next/navigation'
import { ReactNode } from "react";
import { IOptionalChildrenProps, ISlugArrayProps } from "@/components/shared/props";

const viewMapRecords: Record<string, ReactNode> = {
	"home": <div>home</div>,
	"man-post": <PostView/>,
	"man-proj": <ProjectView/>
}

const sidebarEntries: SidebarEntry[] = [
  { label: "Home", view: "home" },
  { label: "Post Management", view: "man-post" },
  { label: "Project Management", view: "man-proj" },
  { label: "Tag Management", view: "man-tags" },
  { label: "User Management", view: "man-user" },
];

function getCurrentView(view: string): ReactNode {
	const viewJSX = viewMapRecords[view || "home"];
	return viewJSX;
}

interface AdminPageProps extends ISlugArrayProps, IOptionalChildrenProps {} 

export default async function Page({ params: { slug = ["home"] } }: AdminPageProps) {
	if (
		(await sidebarEntries)
			.map((entry) => entry.view)
			.indexOf(slug.toString()) < 0
	) redirect("/admin/")
	return (
		<main className="h-screen w-screen flex flex-col p-0 bg-gray-300 box-border m-0">
			<AuthHandler params={null}>
				<Sidebar
					sidebarEntries={sidebarEntries}
					params={{slug: slug.toString()}}
				/>
				<div className="AdminPanelWrapper flex flex-col p-2">
					{getCurrentView(slug.toString())}
				</div>
			</AuthHandler>
			{/* <section>{JSON.stringify(cookies().getAll())}</section> */}
		</main>
	);
}
