require "rails_helper"

RSpec.describe Webhooks::MediamtxHandler do
  let(:user) { User.create!(email: "c@test.com", name: "C", password: "password123", role: "coach") }
  let(:club) { Club.create!(name: "MTX Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") }
  let(:team) { club.teams.create!(name: "T", sport: "volleyball") }
  let!(:match) { team.matches.create!(opponent_name: "Away") }
  let!(:session) do
    StreamSession.create!(
      match: match, user: user, status: "live", platform: "youtube",
      publish_token: "tok", started_at: Time.current
    )
  end

  it "transitions to reconnecting on disconnect" do
    described_class.new(event: "disconnect", session_id: session.id).call
    expect(session.reload.status).to eq("reconnecting")
  end

  it "returns to live on reconnect" do
    session.update!(status: "reconnecting")
    described_class.new(event: "connect", session_id: session.id).call
    expect(session.reload.status).to eq("live")
  end
end
