import Foundation

/// Action for push notification subscription.
enum PushNotificationAction: Int {
    case subscribe = 0
    case unsubscribe = 1
}

/// Token type for push notification registration.
/// Server parity: im.rosetta.packet.runtime.TokenType
enum PushTokenType: Int {
    case fcm = 0        // FCM token (iOS + Android)
    case voipApns = 1   // VoIP APNs token (iOS only)
}

/// PushNotification packet (0x10) — registers or unregisters APNs/FCM token on server.
/// Sent after successful handshake to enable push notifications.
/// Server stores tokens at device level (PushToken entity linked to Device).
struct PacketPushNotification: Packet {
    static let packetId = 0x10

    var notificationsToken: String = ""
    var action: PushNotificationAction = .subscribe
    var tokenType: PushTokenType = .fcm
    var deviceId: String = ""

    func write(to stream: Stream) {
        stream.writeString(notificationsToken)
        stream.writeInt8(action.rawValue)
        stream.writeInt8(tokenType.rawValue)
        stream.writeString(deviceId)
    }

    mutating func read(from stream: Stream) {
        notificationsToken = stream.readString()
        action = PushNotificationAction(rawValue: stream.readInt8()) ?? .subscribe
        tokenType = PushTokenType(rawValue: stream.readInt8()) ?? .fcm
        deviceId = stream.readString()
    }
}
