"use client";

/*
 * The avatar is a raw `<img>`, carried over verbatim from `EventFeedback`:
 * `images.unoptimized` is set, so `next/image` would add nothing here.
 */
import React from "react";
import { MoreHorizontal, Pencil, Trash2 } from "lucide-react";
import { useTranslation } from "react-i18next";
import Stars from "./EventFeedbackStars";
import { type Feedback } from "./useEventFeedback";
import RemoteImage from "@/components/ui/RemoteImage";

/**
 * One review. The menu is the author's own — the parent decides whether to pass
 * `canManage`, since only the signed-in author may edit or delete.
 */
export default function EventFeedbackCard({
  feedback,
  index,
  language,
  canManage,
  openMenuId,
  setOpenMenuId,
  onEdit,
  onDelete,
}: {
  feedback: Feedback;
  index: number;
  language: string;
  canManage: boolean;
  openMenuId: number | null;
  /* A real setter: the toggle is called with an updater function. */
  setOpenMenuId: React.Dispatch<React.SetStateAction<number | null>>;
  onEdit: (feedback: Feedback) => void;
  onDelete: (id: number) => void;
}) {
  const { t } = useTranslation();

  return (
      <article
        key={feedback.id}
        className={`relative pb-5 ${index ? "border-t border-[#00000033]/20" : ""}`}
      >
        <div className="mt-6 flex items-center gap-3">
          <RemoteImage
  src={feedback.user.profile_pic || "/assets/profile/org_profile.svg"}
  alt={feedback.user.nickname || feedback.user.full_name || ""}
  width={59}
  height={59}
  className={"h-[59px] w-[59px] rounded-full border-2 border-primary-5 object-cover"}
/>
          <div>
            <h3 className="text-xl font-bold">
              {feedback.user.nickname || feedback.user.full_name}
            </h3>
            <Stars value={feedback.rating} />
          </div>

          {canManage && (
            <div className="absolute end-0 top-6">
              <button
                type="button"
                className="rounded-full p-2 hover:bg-gray-100"
                onClick={() =>
                  setOpenMenuId((current) =>
                    current === feedback.id ? null : feedback.id
                  )
                }
                aria-label={t("COMMON.FEEDBACK_ACTIONS")}
              >
                <MoreHorizontal className="text-primary-5" />
              </button>
              {openMenuId === feedback.id && (
                <div className="absolute end-0 z-20 w-36 rounded-lg border bg-white shadow-lg">
                  <button
                    type="button"
                    className="flex w-full items-center gap-2 px-4 py-2 hover:bg-gray-50"
                    onClick={() => {
                      onEdit(feedback);
                      setOpenMenuId(null);
                    }}
                  >
                    <Pencil className="h-4 w-4" />
                    {t("COMMON.EDIT_TEXT")}
                  </button>
                  <button
                    type="button"
                    className="flex w-full items-center gap-2 px-4 py-2 text-red-600 hover:bg-gray-50"
                    onClick={() => {
                      onDelete(feedback.id);
                      setOpenMenuId(null);
                    }}
                  >
                    <Trash2 className="h-4 w-4" />
                    {t("COMMON.DELETE")}
                  </button>
                </div>
              )}
            </div>
          )}
        </div>
        <p className="mt-5 text-lg text-secondary-100">
          {(language === "ar" ? feedback.comment_ar : feedback.comment_en) || ""}
        </p>
      </article>
  );
}
