import { redirect } from "next/navigation";
import { getCurrentUser } from "@/lib/api";
import { getDict, getLocale } from "@/lib/i18n";
import { apiFetch } from "@/lib/api";
import { SocialBoard } from "@/components/SocialBoard";

export const dynamic = "force-dynamic";

export default async function SocialPage() {
  const user = await getCurrentUser();
  if (!user) redirect("/login");
  const t = await getDict();
  const siteLocale = await getLocale();

  const feed = await apiFetch<{ items: { id: number; body: string; created_at: string; author_name: string; author_username: string }[]; total: number }>(
    "/api/social/posts",
  );
  const notifs = await apiFetch<{ unread: number }>("/api/social/notifications");
  const canPublish = user.permissions.includes("social.publish");

  return (
    <SocialBoard
      initialPosts={feed.status === 200 ? feed.data?.items ?? [] : []}
      unread={notifs.status === 200 ? notifs.data?.unread ?? 0 : 0}
      canPublish={canPublish}
      locale={siteLocale}
    />
  );
}
