require "rails_helper"

RSpec.describe "Public regia", type: :request do
  let!(:user) { User.create!(email: "regia@test.it", name: "U", password: "password123", role: "coach") }
  let!(:club) { Club.create!(name: "C", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") }
  let!(:team) { club.teams.create!(name: "T", sport: "volleyball") }
  let!(:match) { team.matches.create!(opponent_name: "Opp", sport: "volleyball") }
  let!(:session) { StreamSession.create!(match: match, user: user, platform: "matchlivetv", status: "live") }

  it "mostra la pagina regia con token valido" do
    token = Sessions::RegiaAccess.new(session).issue_token!
    get public_regia_path(token)
    expect(response).to have_http_status(:ok)
    expect(response.body).to include("Condividi link regia")
    expect(response.body).not_to include("Aruba")
  end

  it "aggiorna il punteggio via API" do
    token = Sessions::RegiaAccess.new(session).issue_token!
    session.create_score_state!(home_sets: 0, away_sets: 0, home_points: 0, away_points: 0, current_set: 1)
    patch public_regia_score_path(token), params: { cmd: "home_point" }
    expect(response).to have_http_status(:ok)
    expect(session.score_state.reload.home_points).to eq(1)
  end
end
