import { notFound } from "next/navigation";

import { CustomerPageShell } from "@/app/customer/_components/customer-page-shell";
import { CustomerDiscussionViewPage } from "@/app/customer/[tenant]/discussion-board/_components/customer-discussion-view-page";
import { DISCUSSION_BOARD_PERMISSION } from "@/app/customer/[tenant]/discussion-board/_lib/discussion-board-route";
import { fetchDiscussionBoardView } from "@/app/customer/[tenant]/discussion-board/_lib/discussion-board-server-api";
import { getCustomerSessionContext } from "@/lib/frontend-auth/server";

type PageProps = {
  params: Promise<{ tenant: string; id: string }>;
};

export default async function CustomerDiscussionBoardViewPage({ params }: PageProps) {
  const { tenant, id } = await params;
  const threadToken = decodeURIComponent(id).trim();
  if (!threadToken) {
    notFound();
  }

  const session = await getCustomerSessionContext(tenant);
  const hasPermission = session.permissions.routes.includes(DISCUSSION_BOARD_PERMISSION);

  if (!hasPermission) {
    return (
      <CustomerPageShell>
        <CustomerDiscussionViewPage
          tenant={tenant}
          threadToken={threadToken}
          initialThread={{
            thread_id: 0,
            token: threadToken,
            title: "Discussion",
            body: "",
            body_format: "html",
            status: "published",
            visibility: "",
            visibilityLabel: "",
            authorDisplayName: "",
            upvote_count: 0,
            downvote_count: 0,
            comment_count: 0,
            last_activity_at: null,
            published_at: null,
            is_pinned: false,
            is_locked: false,
            date_added: "",
            last_updated: "",
          }}
          initialBodyHtml=""
          initialTags={[]}
          initialCommentBuckets={[]}
          initialReplyCountByRoot={{}}
          initialCommentsHasMoreRoots={false}
          initialCommentsNextRootOffset={0}
          initialThreadActorVote={0}
          initialIsThreadAuthor={false}
          initialIsSubscribed={false}
          permissionDenied
        />
      </CustomerPageShell>
    );
  }

  const result = await fetchDiscussionBoardView(tenant, threadToken);

  if (result.errorMessage || !result.data) {
    return (
      <CustomerPageShell>
        <CustomerDiscussionViewPage
          tenant={tenant}
          threadToken={threadToken}
          initialThread={{
            thread_id: 0,
            token: threadToken,
            title: "Discussion",
            body: "",
            body_format: "html",
            status: "published",
            visibility: "",
            visibilityLabel: "",
            authorDisplayName: "",
            upvote_count: 0,
            downvote_count: 0,
            comment_count: 0,
            last_activity_at: null,
            published_at: null,
            is_pinned: false,
            is_locked: false,
            date_added: "",
            last_updated: "",
          }}
          initialBodyHtml=""
          initialTags={[]}
          initialCommentBuckets={[]}
          initialReplyCountByRoot={{}}
          initialCommentsHasMoreRoots={false}
          initialCommentsNextRootOffset={0}
          initialThreadActorVote={0}
          initialIsThreadAuthor={false}
          initialIsSubscribed={false}
          initialErrorMessage={result.errorMessage ?? "Thread not found."}
        />
      </CustomerPageShell>
    );
  }

  const data = result.data;

  return (
    <CustomerPageShell>
      <CustomerDiscussionViewPage
        tenant={tenant}
        threadToken={threadToken}
        initialThread={data.thread}
        initialBodyHtml={data.bodyHtml}
        initialTags={data.tags}
        initialCommentBuckets={data.commentBuckets}
        initialReplyCountByRoot={data.replyCountByRoot}
        initialCommentsHasMoreRoots={data.commentsHasMoreRoots}
        initialCommentsNextRootOffset={data.commentsNextRootOffset}
        initialThreadActorVote={data.threadActorVote}
        initialIsThreadAuthor={data.isThreadAuthor}
        initialIsSubscribed={data.isSubscribed}
      />
    </CustomerPageShell>
  );
}
