import React from "react"
import { IMessage } from "@/lib/interfaces/ticket/ticket.interface"
import { Paperclip } from "lucide-react"

interface MessageProps {
  message: IMessage
}

const Message: React.FC<MessageProps> = ({ message }) => {
  const isOwner = message.by_owner

  const [date, time] = message.created_at.split(" ")

  return (
    <div className={`flex w-full my-3 ${isOwner ? "justify-start" : "justify-end"}`}>
      <div
        className={`max-w-[70%] rounded-xl p-3 ${
          isOwner
            ? "bg-primary text-primary-foreground rounded-bl-none"
            : "bg-muted rounded-br-none"
        }`}
      >
        {!isOwner && (
          <div className="text-xs font-medium mb-1 text-secondary">
            {message.user_name || "کاربر"}
          </div>
        )}

        <p className="mt-2 text-sm leading-relaxed whitespace-pre-line">
          {message.message}
        </p>

        {message.attachments && message.attachments.length > 0 && (
          <div className="mt-2 my-1 pt-2 border-t border-opacity-30">
            {message.attachments.map((att, index) => (
              <a
                key={index}
                href={att.temporary_url}
                target="_blank"
                rel="noopener noreferrer"
                className="flex items-center text-xs underline break-all"
              >
                <Paperclip className="w-3 h-3 mx-1" />
                {att.file_name}
              </a>
            ))}
          </div>
        )}

        <div
          className={`text-xs mt-2 ${
            isOwner
              ? "text-primary-foreground/70 text-right"
              : "text-muted-foreground/70 text-left"
          }`}
        >
          {date} - {time}
        </div>
      </div>
    </div>
  )
}

export default Message
