"use client";

import Image from "next/image";
import { getDefaultProfileImage } from "@/lib/helpers";
import { type Reply } from "./replyDetailTypes";

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

/**
 * A reply's author row: avatar, name and relative timestamp.
 */
export default function ReplyAuthorRow({
  reply,
  onNavigateToProfile,
}: {
  reply: Reply;
  selectedLanguage: string;
  onNavigateToProfile: (user: Reply["user"]) => void;
  onNavigateToReply: (replyId: number) => void;
}) {

  return (
  <div className="contents">
    <Image
      src={
        reply.user.profile_pic ||
        getDefaultProfileImage(
          reply.user.gender_display?.value_en,
          asset("profile/male_profile.svg"),
          asset("profile/dummyimg.png"),
          asset("profile/org_profile.svg")
        )
      }
      alt=""
      width={59}
      height={59}
      unoptimized
      className="col-start-1 row-span-2 h-10 w-10 shrink-0 cursor-pointer rounded-full object-cover"
      onClick={(e) => {
        e.preventDefault();
        e.stopPropagation();
        onNavigateToProfile(reply.user);
      }}
      onKeyDown={(e) => {
        if (e.key === "Enter" || e.key === " ") {
          e.preventDefault();
          onNavigateToProfile(reply.user);
        }
      }}
      role="button"
      tabIndex={0}
    />
    {/* A `<button>` inside the heading rather than an `<h3 role="button">` —
        see the same change in `ChildReplyRow` for why the heading is worth
        keeping. */}
    <h3 className="col-start-2 min-w-0 rounded-t-2xl bg-[#F0F2F5] px-3 pb-1 pt-2 text-sm font-bold text-[#202124] sm:text-base">
      <button
        type="button"
        className="cursor-pointer !whitespace-normal text-start hover:underline [overflow-wrap:anywhere]"
        onClick={() => onNavigateToProfile(reply.user)}
      >
        {reply.user.full_name}
      </button>
    </h3>
  </div>
  );
}
