import Foundation

struct CallIceServer: Equatable, Sendable {
    var url: String = ""
    var username: String = ""
    var credential: String = ""
    var transport: String = ""
}

/// ICE servers packet (0x1C / 28).
/// Server returns TURN/STUN configuration for call setup.
struct PacketIceServers: Packet {
    static let packetId = 0x1C

    var iceServers: [CallIceServer] = []

    func write(to stream: Stream) {
        stream.writeInt16(iceServers.count)
        for server in iceServers {
            stream.writeString(server.url)
            stream.writeString(server.username)
            stream.writeString(server.credential)
            stream.writeString(server.transport)
        }
    }

    mutating func read(from stream: Stream) {
        let count = max(stream.readInt16(), 0)
        var parsed: [CallIceServer] = []
        parsed.reserveCapacity(count)

        for _ in 0..<count {
            parsed.append(
                CallIceServer(
                    url: stream.readString(),
                    username: stream.readString(),
                    credential: stream.readString(),
                    transport: stream.readString()
                )
            )
        }

        iceServers = parsed
    }
}
