import Foundation

struct RecentSearch: Codable, Equatable, Sendable {
    let publicKey: String
    var title: String
    var username: String
    /// Verification level from server (0 = not verified).
    var verified: Int = 0

    // MARK: - Migration: ignore unknown keys from old persisted data
    enum CodingKeys: String, CodingKey {
        case publicKey, title, username, verified
    }

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

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        publicKey = try container.decode(String.self, forKey: .publicKey)
        title = try container.decode(String.self, forKey: .title)
        username = try container.decode(String.self, forKey: .username)
        verified = (try? container.decode(Int.self, forKey: .verified)) ?? 0
    }
}
