"use server";
cache: "no-store";

import { ReactNode } from "react";
import {
	deletePost,
	getPostsWithBucketsAndProject,
	updatePost,
	GetPostsAttributes,
	PostServerActions,
} from "@/app/lib/actions/entitymanagement/post/postActions";
import { getProjects } from "@/app/lib/actions/entitymanagement/post/projectActions";
import { Bucket, Project, Post, dbSync, Attachment } from "@/models";
import { handleActionResult } from "@/app/lib/actions/clientActionHandler";
import { CPostView } from "@/components/views/admin/post";

type Props = {
	children?: ReactNode;
};

export async function PostView(props: Props) {
	const sync = await dbSync;

	const headings: string[] = [
		"#",
		"Title",
		"Content",
		"Project",
		"CreatedAt",
		"UpdatedAt",
	];

	const posts: GetPostsAttributes[] | undefined = handleActionResult(
		await getPostsWithBucketsAndProject()
	);
	const projects = await Project.findAll().then((projects) =>
		projects.map((project) => JSON.parse(JSON.stringify(project)))
	);

	return (
		<div className="w-[100%] min-h-fit bg-gray-100 overflow-scroll">
			<div className="w-[100%] m-auto">
				<CPostView
					posts={posts ? posts : []}
					projects={projects}
					headings={headings}
					actions={{
						deletePost: deletePost,
						getPosts: getPostsWithBucketsAndProject,
						getProjects: getProjects,
						savePost: updatePost
					}}
				/>
			</div>
		</div>
	);
}
