"use client";

import { useTranslation } from "react-i18next";

import { Modal } from "@/components/ui/Modal";
import { Button } from "@/components/ui/Button";
import Loader from "@/components/ui/Loader";
import UserReply from "./UserReply";
import { useCommunityDetail } from "./useCommunityDetail";
import CommunityDetailModals from "./CommunityDetailModals";
import CommunityPostBody from "./CommunityPostBody";
import CommunityPostActions from "./CommunityPostActions";
import CommunityPostAuthor from "./CommunityPostAuthor";
import CommunityPostMenu from "./CommunityPostMenu";
import CommunityLoadError from "./CommunityLoadError";

export default function CommunityDetailPage({ id }: { id: string }) {
  const { t } = useTranslation();
  const {
    postUser,
    profileUrl,
    isLoading,
    error,
    refetch,
    open,
    setOpen,
    router,
    selectedLanguage,
    authToken,
    likeMutation,
    deleteMutation,
    isSubmitting,
    setIsSubmitting,
    isReplyCreated,
    setIsReplyCreated,
    showDropdown,
    setShowDropdown,
    opencreatepost,
    setcreatepost,
    openDeleteConfirmModal,
    setOpenDeleteConfirmModal,
    isPostSubmitting,
    setIsPostSubmitting,
    openConfirmModal,
    setOpenConfirmModal,
    localPostData,
    validateFormRef,
    formRef,
    dropdownRef,
    postData,
    handlePostLike,
    handleEdit,
    handleDelete,
    handleImageClick,
    handleTagClick,
    handleConfirmDelete,
    handleSaveClick,
    handleConfirmUpdate,
  } = useCommunityDetail(id);

  if (isLoading) return <Loader />;
  if (!postData) return <CommunityLoadError error={error} fallbackKey="COMMON.COMMUNITY_POST_UNAVAILABLE" onRetry={error ? () => void refetch() : undefined} />;

  return (
    <>
      <CommunityDetailModals
        open={open}
        setOpen={setOpen}
        opencreatepost={opencreatepost}
        setcreatepost={setcreatepost}
        isSubmitting={isSubmitting}
        setIsSubmitting={setIsSubmitting}
        id={id}
        refetch={refetch}
        postId={String(postData.id)}
        setIsReplyCreated={setIsReplyCreated}
        handleSaveClick={handleSaveClick}
        isPostSubmitting={isPostSubmitting}
        setIsPostSubmitting={setIsPostSubmitting}
        formRef={formRef}
        validateFormRef={validateFormRef}
      />

      {/* Confirmation Modal for Update */}
      <Modal
        open={openConfirmModal}
        onClose={() => setOpenConfirmModal(false)}
        title={t("COMMON.UPDATE_POST")}
        size="sm"
        footer={
          <div className="flex xss:flex-col justify-center w-full gap-5">
            <Button
              variant="primary"
              size="medium"
              className="xss:!w-full"
              onClick={handleConfirmUpdate}
              disabled={isPostSubmitting}
            >
              {t("COMMON.CONFIRM")}
            </Button>
            <Button
              variant="secondary"
              size="medium"
              className="xss:!w-full"
              onClick={() => setOpenConfirmModal(false)}
              disabled={isPostSubmitting}
            >
              {t("COMMON.CANCEL")}
            </Button>
          </div>
        }
      >
        <h2 className="text-center pb-10 text-lg">
          {t("COMMON.ARE_YOU_SURE_UPDATE_COMMUNITY_POST")}
        </h2>
      </Modal>

      {/* Delete Confirmation Modal */}
      <Modal
        open={openDeleteConfirmModal}
        onClose={() => setOpenDeleteConfirmModal(false)}
        title={t("COMMON.DELETE_POST")}
        size="sm"
        footer={
          <div className="flex xss:flex-col justify-center w-full gap-5">
            <Button
              variant="primary"
              size="medium"
              className="xss:!w-full"
              onClick={handleConfirmDelete}
              disabled={deleteMutation.isPending}
            >
              {t("COMMON.CONFIRM")}
            </Button>
            <Button
              variant="secondary"
              size="medium"
              className="xss:!w-full"
              onClick={() => setOpenDeleteConfirmModal(false)}
              disabled={deleteMutation.isPending}
            >
              {t("COMMON.CANCEL")}
            </Button>
          </div>
        }
      >
        <h2 className="text-center pb-10 text-lg">
          {t("COMMON.ARE_YOU_SURE_DELETE_POST")}
        </h2>
      </Modal>

      <div className="mobilescreen:pb-[40px] pb-[40px] 2xl:py-[40px] laptopmain:py-[50px] laptop:py-[40px] lg:py-[40px] py-[40px] border-t border-[#000]">
        <div className="2xl:px-5 px-3 mobilescreen:px-[13px] 2xl:w-[75%] laptopmain:w-[83%] laptop:w-[78%] laptopitm:w-[85%] lg:w-[90%] md:w-[85%] w-[90%] mx-auto relative">
          <CommunityPostMenu
            selectedLanguage={selectedLanguage}
            showDropdown={showDropdown}
            setShowDropdown={setShowDropdown}
            dropdownRef={dropdownRef}
            handleEdit={handleEdit}
            handleDelete={handleDelete}
            isCreator={postData.is_creator}
          />

          <CommunityPostAuthor
            postUser={postUser}
            postData={postData}
            selectedLanguage={selectedLanguage}
            profileUrl={profileUrl}
            router={router}
          />

          <CommunityPostBody
            postData={postData}
            selectedLanguage={selectedLanguage}
            handleTagClick={handleTagClick}
            handleImageClick={handleImageClick}
          />

          <CommunityPostActions
            postData={postData}
            localPostData={localPostData}
            likeMutation={likeMutation}
            handlePostLike={handlePostLike}
            setOpen={setOpen}
            authToken={authToken}
          />

          <div className="pb-12">
            <UserReply
              postId={postData.id}
              isReplyCreated={isReplyCreated}
              onReplyDeleted={refetch}
            />
          </div>
        </div>
      </div>
    </>
  );
}
