require "rails_helper"

RSpec.describe Teams::Entitlements do
  let(:team) { Team.create!(name: "Test Team", sport: "volleyball") }

  before do
    load Rails.root.join("db/seeds/plans.rb")
    Billing::AssignPlan.call(team: team, plan_slug: "free")
  end

  subject(:ent) { described_class.new(team) }

  it "allows matchlivetv on free" do
    expect(ent.can_stream_on?("matchlivetv")).to be true
  end

  it "denies youtube on free" do
    expect { ent.assert_can_stream_on!("youtube") }.to raise_error(Teams::EntitlementError)
  end

  it "allows youtube on premium full" do
    Billing::AssignPlan.call(team: team, plan_slug: "premium_full")
    expect(ent.can_stream_on?("youtube")).to be true
  end

  it "enforces transmission staff limit on free" do
    team.team_invitations.create!(
      email: "camera@test.com",
      token_digest: SecureRandom.hex(32),
      role: "member",
      staff_kind: "transmission",
      expires_at: 7.days.from_now
    )
    expect { ent.assert_can_invite!(staff_kind: "transmission") }.to raise_error(Teams::EntitlementError) do |e|
      expect(e.code).to eq("staff_limit_reached")
    end
  end

  it "allows regia invite when only transmission slot is used on free" do
    team.team_invitations.create!(
      email: "camera@test.com",
      token_digest: SecureRandom.hex(32),
      role: "member",
      staff_kind: "transmission",
      expires_at: 7.days.from_now
    )
    expect { ent.assert_can_invite!(staff_kind: "regia") }.not_to raise_error
  end
end
