require "rails_helper"

RSpec.describe Public::BillingHelper, type: :helper do
  before do
    load Rails.root.join("db/seeds/plans.rb")
    allow(MatchLiveTv).to receive(:stripe_enabled?).and_return(true)
    allow(MatchLiveTv).to receive(:stripe_premium_light_yearly_price_id).and_return("py")
    allow(MatchLiveTv).to receive(:stripe_premium_light_monthly_price_id).and_return("pm")
    allow(MatchLiveTv).to receive(:stripe_premium_full_yearly_price_id).and_return("fy")
    allow(MatchLiveTv).to receive(:stripe_premium_full_monthly_price_id).and_return("fm")
  end

  let(:premium_light) { Plan["premium_light"] }
  let(:premium_full) { Plan["premium_full"] }

  it "propone due opzioni checkout da free" do
    action = helper.plan_billing_action(
      current_slug: "free",
      target_plan: premium_light,
      stripe_subscription_active: false
    )
    expect(action[:kind]).to eq(:checkout_options)
    expect(action[:intervals]).to eq(%w[monthly yearly])
  end

  it "segna piano corrente quando non ci sono altri intervalli" do
    allow(MatchLiveTv).to receive(:stripe_premium_light_monthly_price_id).and_return(nil)
    action = helper.plan_billing_action(
      current_slug: "premium_light",
      target_plan: premium_light,
      stripe_subscription_active: true,
      current_interval: "yearly"
    )
    expect(action[:kind]).to eq(:current)
    expect(action[:label]).to include("€40/anno")
  end

  it "propone cambio intervallo sullo stesso piano" do
    action = helper.plan_billing_action(
      current_slug: "premium_light",
      target_plan: premium_light,
      stripe_subscription_active: true,
      current_interval: "yearly"
    )
    expect(action[:kind]).to eq(:interval_switch)
    expect(action[:intervals]).to eq(["monthly"])
  end

  it "propone upgrade con opzioni intervallo" do
    action = helper.plan_billing_action(
      current_slug: "premium_light",
      target_plan: premium_full,
      stripe_subscription_active: true,
      current_interval: "yearly"
    )
    expect(action[:kind]).to eq(:change_options)
    expect(action[:intervals].size).to eq(2)
  end

  it "stile annuale pieno e mensile outline" do
    expect(helper.plan_interval_button_class("yearly")).to include("btn-primary")
    expect(helper.plan_interval_button_class("yearly")).not_to include("btn-outline")
    expect(helper.plan_interval_button_class("monthly")).to include("btn-outline")
    expect(helper.plan_interval_button_class("monthly")).not_to include("btn-primary")
  end

  it "mostra prima il pulsante annuale" do
    expect(helper.plan_intervals_for_display(%w[monthly yearly])).to eq(%w[yearly monthly])
  end
end
