import LibraryPage from "@/components/user/library/LibraryPage";
import { getGenre } from "@/libs/axios/modules/genre";
import { getCollectionsPosts } from "@/libs/axios/modules/post";
import { GenreModel, PostModel } from "@/models";
import { cookies } from "next/headers";

export default async function Library({
    searchParams,
}: {
    searchParams: Promise<{
        sort?: string;
        genre?: string;
        page?: string;
        search?: string;
    }>;
}) {
    const params = await searchParams;
    const cookieStore = await cookies();
    const token = cookieStore.get("token");

    const genres: GenreModel[] = (await getGenre({ limit: 0, page: 1 })).data.data;

    const genre = params.genre || "";
    const search = params.search || "";

    const filter = {
        title: search,
        title_en: search,
        title_no: search,
        genre_id: genre,
    };

    const posts: PostModel[] = token ? (await getCollectionsPosts(token.value.slice(1, -1), { filter })).data.data : [];

    return <LibraryPage posts={posts} genres={genres} />
}