"use client";

import { useTranslation } from "react-i18next";
import { BsThreeDots } from "react-icons/bs";
import { MdDelete, MdEdit } from "react-icons/md";

/**
 * The post owner's edit/delete menu, pinned to the card's top corner.
 *
 * Which corner depends on the writing direction, which is why the side is
 * chosen from the language rather than with a logical property.
 *
 * One component for what were two near-identical copies (`CommunityDetailPage`
 * and `InnerPostCard`), which had drifted: only the card's copy carried an
 * `aria-label`, so on the detail page the trigger was announced as an unnamed
 * button. The two visual variants — the plain card and the orange "idea" card
 * — differ only in hover and icon colour, so those arrive as classes.
 *
 * `z-10` on the wrapper is load-bearing: the card's link is a stretched
 * `absolute inset-0` overlay, and a positioned sibling without a z-index
 * would lose the click to it.
 */
export default function CommunityPostMenu({
  selectedLanguage,
  triggerClassName = "hover:bg-gray-100",
  iconClassName = "text-primary-5 text-lg h-[30px] w-[30px]",
  showDropdown,
  setShowDropdown,
  dropdownRef,
  handleEdit,
  handleDelete,
  isCreator,
}: {
  selectedLanguage: string;
  showDropdown: boolean;
  setShowDropdown: React.Dispatch<React.SetStateAction<boolean>>;
  dropdownRef: React.RefObject<HTMLDivElement | null>;
  handleEdit: (event: React.MouseEvent) => void;
  handleDelete: (event: React.MouseEvent) => void;
  /* `is_creator` from the post payload — only the owner sees the menu. */
  isCreator?: boolean;
  /** Hover colour for the trigger, which differs between the two card styles. */
  triggerClassName?: string;
  /** Icon colour, likewise. */
  iconClassName?: string;
}) {
  const { t } = useTranslation();

  return (
  <div
    className={`absolute top-4 z-10 ${
      selectedLanguage === "ar" ? "left-4" : "right-4"
    }`}
    ref={dropdownRef}
  >
    {isCreator && (
      <button
        onClick={(e) => {
          e.preventDefault();
          e.stopPropagation();
          setShowDropdown(!showDropdown);
        }}
        className={`p-2 rounded-full transition-colors duration-200 ${triggerClassName}`}
        aria-label={t("COMMON.POST_OPTIONS")}
      >
        <BsThreeDots className={iconClassName} aria-hidden="true" />
      </button>
    )}

    {/* Dropdown Menu */}
    {showDropdown && (
      <div
        className={`absolute top-full mt-2 w-32 bg-white border border-gray-200 rounded-lg shadow-lg z-20 ${
          selectedLanguage === "ar" ? "left-0" : "right-0"
        }`}
      >
        <button
          onClick={handleEdit}
          className="flex items-center gap-2 w-full px-4 py-2 text-left text-gray-700 hover:bg-gray-50 rounded-t-lg transition-colors duration-200"
        >
          <MdEdit className="text-primary-5" />
          <span className="text-sm">{t("COMMON.EDIT_TEXT")}</span>
        </button>
        <button
          onClick={handleDelete}
          className="flex items-center gap-2 w-full px-4 py-2 text-left text-red-600 hover:bg-gray-50 rounded-b-lg transition-colors duration-200"
        >
          <MdDelete className="text-red-500" />
          <span className="text-sm">{t("COMMON.DELETE")}</span>
        </button>
      </div>
    )}
  </div>
  );
}
