cache: 'no-store'

import { tryFetchPosts } from "@/app/api/post/route";
import { constructAPIUrl } from "@/util/Utils";
import { ReactNode, useEffect } from "react";
import TableGen from "../../../client/TableGen";
import PostTable from "@/components/client/admin/PostTable";
import { deletePost, getPostsWithBuckets, updatePost } from "@/app/lib/actions/entityManagement/postActions";
import { getProjects } from "@/app/lib/actions/entityManagement/projectActions";
import { Bucket, Project, Post, dbSync, Attachment } from "@/model/Models";
import { tryCreateAttachment } from "@/app/api/attachment/route";

type Props = {
    children?:ReactNode
}


export default async function PostView(props:Props){
    await dbSync;

    const headings = [
        '#', 
        'Title',
        'Content',
        'Project',
        'Date Created',
        'Date Modified',
        'Edit',
        'Delete',
    ]
    const actions = {
        deletePost: deletePost, 
        getPosts:getPostsWithBuckets, 
        getProjects:getProjects, 
        savePost:updatePost,
        // uploadAttachment:tryCreateAttachment
    };

    const posts:Post[] = await Post.findAll({include: {model: Bucket, include: {model: Attachment}}}).then(posts=>posts.map((e)=>JSON.parse(JSON.stringify(e))));
    const projects = await Project.findAll().then(projects=>projects.map((e)=>JSON.parse(JSON.stringify(e))));
    
    return (
        <div className="w-[100%] min-h-fit bg-gray-100 overflow-scroll">
            <span className="flex flex-row flex-grow w-[100%] pl-2 pr-2"><h1 className="p-2 inline-block">Post Management</h1><section className="flex-grow"></section><button className='btn btn-success h-12 mt-auto mb-auto self-end'>New</button></span>
            <div className="w-[100%] m-auto">
                <PostTable data={posts} projects={projects} headings={headings} actions={actions}></PostTable>
            </div>
        </div>
    );
}