import Foundation

/// WebRTC signaling packet (0x1B / 27).
/// Carries SDP offer/answer and ICE candidate payloads.
enum WebRTCSignalType: Int, Sendable {
    case offer = 0
    case answer = 1
    case iceCandidate = 2
}

struct PacketWebRTC: Packet {
    static let packetId = 0x1B

    var signalType: WebRTCSignalType = .offer
    var sdpOrCandidate: String = ""
    /// Sender's public key — server validates via DeviceService (no session auth needed).
    var publicKey: String = ""
    /// Sender's device ID — server checks publicKey↔deviceId binding.
    var deviceId: String = ""

    func write(to stream: Stream) {
        stream.writeInt8(signalType.rawValue)
        stream.writeString(sdpOrCandidate)
        stream.writeString(publicKey)
        stream.writeString(deviceId)
    }

    mutating func read(from stream: Stream) {
        signalType = WebRTCSignalType(rawValue: stream.readInt8()) ?? .offer
        sdpOrCandidate = stream.readString()
        publicKey = stream.readString()
        deviceId = stream.readString()
    }
}
