"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 { ClientPostView } from "@/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 actions: PostServerActions = {
		deletePost: deletePost,
		getPosts: getPostsWithBucketsAndProject,
		getProjects: getProjects,
		savePost: updatePost,
		// uploadAttachment:tryCreateAttachment
	};

	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">
				<ClientPostView
					posts={posts ? posts : []}
					projects={projects}
					headings={headings}
					actions={actions}
				/>
			</div>
		</div>
	);
}
