"use client";

import type { CommunityEntityId, CommunityReplyView } from "@/features/community/communityRecord";
import { useTranslation } from "react-i18next";

import Loader from "@/components/ui/Loader";
import QueryErrorNotice from "@/components/shared/QueryErrorNotice";
import ReplyItem from "./ReplyItem";
import { useUserReply } from "./useUserReply";
import UserReplyModals from "./UserReplyModals";

/**
 * A reply, as the UI holds it.
 *
 * `CommunityReplyView` — the transcription of `WebsiteReplyResource` plus the
 * client-only extras — rather than a local shape. This interface existed
 * twice, byte-identical, and both copies disagreed with the resource: they
 * required `user` (which is `null` when the row has no author) and
 * `user.profile_pic` / `text_en` / `text_ar` as `string` (all nullable), they
 * omitted `post_id` and `parent_id` entirely, and they declared `is_deleted`
 * and `parent_is_deleted` as required booleans the API never sends.
 */
export type Reply = CommunityReplyView;

export interface UserReplyProps {
  postId: CommunityEntityId;
  isReplyCreated: boolean;
  onReplyDeleted?: () => void;
}

export default function UserReply({
  isReplyCreated,
  postId,
  onReplyDeleted,
}: UserReplyProps) {
  const { t } = useTranslation();
  const screen = useUserReply({
    isReplyCreated,
    postId,
    onReplyDeleted,
  });
  const {
    repliesLoading,
    repliesError,
    refetch,
    likeMutation,
    activeDropdown,
    setActiveDropdown,
    registerDropdown,
    topLevelReplies,
    handleReplyLike,
    handleEdit,
    handleDelete,
    handleReply,
    handleNavigateToReply,
    handleNavigateToProfile,
    handleImageClick,
  } = screen;

  if (repliesLoading) return <Loader />;
  if (repliesError) return <QueryErrorNotice error={repliesError} onRetry={() => void refetch()} />;

  return (
    <>
      <UserReplyModals screen={screen} postId={postId} />

      <div>
        <h3 className="font-bold text-xl my-4">
          {topLevelReplies.length === 1
            ? t("COMMON.REPLY")
            : t("COMMON.REPLIES")}{" "}
          ({topLevelReplies.length})
        </h3>

        {topLevelReplies.length === 0 ? (
          <div className="text-center py-8 text-secondary-102 text-lg font-medium">
            {t("COMMON.NO_REPLIES")}
          </div>
        ) : (
          topLevelReplies.map((reply, index) => (
            <ReplyItem
              key={reply.id}
              reply={reply}
              index={index}
              totalSiblings={topLevelReplies.length}
              onLike={handleReplyLike}
              likeLoading={likeMutation.isPending}
              onEdit={handleEdit}
              onDelete={handleDelete}
              onReply={handleReply}
              activeDropdown={activeDropdown}
              setActiveDropdown={setActiveDropdown}
              registerDropdown={registerDropdown}
              onNavigateToReply={handleNavigateToReply}
              onNavigateToProfile={handleNavigateToProfile}
              onImageClick={handleImageClick}
            />
          ))
        )}
      </div>
    </>
  );
}
