import Foundation

/// Single message inside a direct chat dialog.
struct ChatMessage: Identifiable, Codable, Sendable, Equatable {
    let id: String
    var fromPublicKey: String
    var toPublicKey: String
    var text: String
    var timestamp: Int64
    var deliveryStatus: DeliveryStatus
    var isRead: Bool
    var attachments: [MessageAttachment]

    /// Desktop parity: stores the decrypted plainKeyAndNonce (latin1) for on-demand
    /// attachment download & decryption. Computed during message decryption in
    /// `handleIncomingMessage()` and during outgoing send in `sendMessageWithAttachments()`.
    /// `nil` for messages without attachments or legacy persisted messages.
    var attachmentPassword: String?

    func isFromMe(myPublicKey: String) -> Bool {
        fromPublicKey == myPublicKey
    }

    var date: Date {
        Date(timeIntervalSince1970: Double(timestamp) / 1000.0)
    }
}
