require "rails_helper"

RSpec.describe Recordings::AccessPolicy do
  let!(:club) { Club.create!(name: "Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") }
  let!(:team) { club.teams.create!(name: "Team", sport: "volleyball") }
  let!(:user) { User.create!(email: "coach@test.com", name: "Coach", password: "password123", role: "coach") }
  let!(:match) { team.matches.create!(opponent_name: "Rival", scheduled_at: 1.day.from_now) }
  let!(:session) do
    StreamSession.create!(match: match, user: user, platform: "matchlivetv", status: "ended")
  end
  let!(:recording) do
    Recording.create!(
      stream_session: session,
      team: team,
      status: "ready",
      privacy_status: "unlisted",
      expires_at: 10.days.from_now,
      storage_key: "key"
    )
  end

  it "allows unlisted replay without login" do
    expect(described_class.new(recording, viewer: nil).allowed?).to be true
  end

  it "allows public replay without login" do
    recording.update!(privacy_status: "public")
    expect(described_class.new(recording, viewer: nil).allowed?).to be true
  end

  it "denies expired replay" do
    recording.update!(expires_at: 1.day.ago)
    expect(described_class.new(recording, viewer: nil).allowed?).to be false
  end
end
