import type { ChatThreadMessage } from "@/mocks/dshnChatThreads";
import ChatInvoiceBubble from "./ChatInvoiceBubble";
import { useWeb } from "../WebContext";
import { cn } from "../WebUI";

export default function ChatMessageRow({
  message,
  approved,
  showActions,
  onApprove,
  onContinue,
}: {
  message: ChatThreadMessage;
  approved: boolean;
  showActions: boolean;
  onApprove: () => void;
  onContinue: () => void;
}) {
  const { t } = useWeb();

  if (message.type === "system") {
    return (
      <div className="flex justify-center py-0.5">
        <span className="max-w-[85%] rounded-full bg-background-200 px-4 py-1.5 text-center text-[11.5px] leading-snug text-foreground-600">
          {message.text}
        </span>
      </div>
    );
  }

  if (message.type === "invoice" && message.invoice) {
    return (
      <ChatInvoiceBubble
        invoice={message.invoice}
        time={message.time}
        approved={approved}
        showActions={showActions}
        onApprove={onApprove}
        onContinue={onContinue}
      />
    );
  }

  const mine = message.from === "me";
  return (
    <div className={cn("flex", mine ? "justify-start" : "justify-end")}>
      <div className={cn("flex max-w-[70%] min-w-0 flex-col gap-1", mine ? "items-start" : "items-end")}>
        {message.type === "image" ? (
          <div className="h-[190px] w-[260px] overflow-hidden rounded-[16px] border border-background-200 bg-background-200">
            <img src={message.image} alt="مرفق" className="h-full w-full object-cover object-top" />
          </div>
        ) : message.type === "file" ? (
          <div className="flex w-[260px] items-center gap-3 rounded-[16px] border border-background-200 bg-background-100 p-3">
            <span className="flex h-11 w-11 shrink-0 items-center justify-center rounded-[10px] bg-background-200">
              <i className="ri-file-pdf-2-line text-[22px] leading-none text-danger-600" />
            </span>
            <div className="min-w-0">
              <p className="text-[13px] font-medium leading-snug text-foreground-950">{message.fileName}</p>
              <p className="text-[11px] text-foreground-500">{t("attachment")}</p>
            </div>
          </div>
        ) : (
          <div
            className={cn(
              "rounded-[18px] px-4 py-2.5 text-[13.5px] leading-relaxed",
              mine
                ? "rounded-tr-[6px] bg-primary-500 text-background-50"
                : "rounded-tl-[6px] border border-background-200 bg-background-100 text-foreground-900",
            )}
          >
            {message.text}
          </div>
        )}
        <span className="px-1 text-[10.5px] text-foreground-400">{message.time}</span>
      </div>
    </div>
  );
}