#!/usr/bin/env ruby
# frozen_string_literal: true

# Example: quicproquo Ruby SDK demo.
#
# Usage:
#   ruby demo.rb --server 127.0.0.1:5001 --ca-cert ca.pem \
#     --user alice --pass secret --recipient bob --message "hello"

require "optparse"
require_relative "../lib/quicproquo"

options = {
  server: "127.0.0.1:5001",
  ca_cert: "ca.pem",
  message: "hello from Ruby SDK!",
}

OptionParser.new do |opts|
  opts.banner = "Usage: demo.rb [options]"
  opts.on("--server ADDR", "Server address") { |v| options[:server] = v }
  opts.on("--ca-cert PATH", "CA certificate") { |v| options[:ca_cert] = v }
  opts.on("--user NAME", "Username") { |v| options[:user] = v }
  opts.on("--pass PASSWORD", "Password") { |v| options[:pass] = v }
  opts.on("--recipient NAME", "Recipient") { |v| options[:recipient] = v }
  opts.on("--message TEXT", "Message") { |v| options[:message] = v }
end.parse!

QuicProQuo::Client.open(options[:server], ca_cert: options[:ca_cert]) do |client|
  puts "Connected to #{options[:server]}"

  client.login(options[:user], options[:pass])
  puts "Logged in as #{options[:user]}"

  client.send(options[:recipient], options[:message])
  puts "Sent message to #{options[:recipient]}"

  puts "Waiting for messages (5s)..."
  messages = client.receive(timeout_ms: 5000)
  messages.each { |msg| puts "  received: #{msg}" }

  puts "Done."
end
