"use client";

import { type PostData } from "./InnerPost";
import Image from "next/image";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";
import ContactAuthorButton from "./ContactAuthorButton";

const asset = (path: string) => `/assets/${path}`;

/**
 * A post's like and reply bar.
 *
 * The like count prefers the optimistic local copy over the fetched one, so
 * the number moves the moment the heart is pressed.
 */
export default function CommunityPostActions({
  postData,
  localPostData,
  likeMutation,
  handlePostLike,
  setOpen,
  authToken,
}: {
  postData: NonNullable<PostData["data"]>;
  localPostData: PostData["data"] | null;
  likeMutation: { isPending: boolean };
  handlePostLike: (postId: string) => void;
  setOpen: (open: boolean) => void;
  /* Replying needs a session; the bar renders differently without one. */
  authToken?: string;
}) {
  const { t } = useTranslation();

  return (
  <div className="flex gap-4 items-center pb-8 border-b border-[#00000033]/20">
    <button
      disabled={likeMutation.isPending}
      onClick={() =>
        handlePostLike(String(localPostData?.id || postData.id))
      }
      className="flex items-center gap-[9px] text-gray-600 border border-primary-5 bg-[#170A510F]/5 rounded-3xl 2xl:px-5 px-3 xsmall:px-3 h-[36px] text-center justify-center"
    >
      <Image
        src={asset("community/purplethumb.svg")}
        alt={t("COMMON.LIKE")}
        width={20}
        height={20}
        className="w-5 h-5"
      />
      <span className="text-sm text-primary-5">
        {localPostData?.likes_count ?? postData.likes_count}
      </span>
    </button>
    <button
      onClick={() => {
        if (!authToken) {
          toast.info(t("COMMON.LOGIN_TO_REPLY"));
          return;
        }
        setOpen(true);
      }}
      className="flex items-center gap-[9px] text-gray-600 border border-primary-5 bg-[#170A510F]/5 rounded-3xl 2xl:px-5 px-3 xsmall:px-3 h-[36px] text-center justify-center"
    >
      <Image
        src={asset("community/message.svg")}
        alt={t("COMMON.REPLY")}
        width={20}
        height={20}
      />
      <span className="text-sm text-primary-5">
        {postData.replies_count || 0}
      </span>
    </button>
    {/* «للتواصل» (PDF p.26) — the post page lacked it entirely. */}
    <ContactAuthorButton post={postData} />
  </div>
  );
}
