import Foundation

/// Navigation payload for opening a direct chat.
struct ChatRoute: Hashable {
    let publicKey: String
    let title: String
    let username: String
    let verified: Int

    init(publicKey: String, title: String, username: String, verified: Int) {
        self.publicKey = publicKey
        self.title = title
        self.username = username
        self.verified = verified
    }

    init(dialog: Dialog) {
        self.init(
            publicKey: dialog.opponentKey,
            title: dialog.opponentTitle,
            username: dialog.opponentUsername,
            verified: dialog.verified
        )
    }

    init(user: SearchUser) {
        self.init(
            publicKey: user.publicKey,
            title: user.title,
            username: user.username,
            verified: user.verified
        )
    }

    init(recent: RecentSearch) {
        self.init(
            publicKey: recent.publicKey,
            title: recent.title,
            username: recent.username,
            verified: recent.verified
        )
    }

    init(groupDialogKey: String, title: String, description: String = "") {
        self.init(
            publicKey: groupDialogKey,
            title: title,
            username: description,
            verified: 0
        )
    }

    var isSavedMessages: Bool {
        publicKey == SessionManager.shared.currentPublicKey
    }

    var isGroup: Bool {
        DatabaseManager.isGroupDialogKey(publicKey)
    }

    var isSystemAccount: Bool {
        SystemAccounts.isSystemAccount(publicKey)
    }

    var displayTitle: String {
        if isSavedMessages {
            return "Saved Messages"
        }
        if !title.isEmpty {
            return title
        }
        if !username.isEmpty {
            return "@\(username)"
        }
        return String(publicKey.prefix(12))
    }
}
