require "rails_helper"

RSpec.describe Billing::Stripe::ChangePlan do
  let(:club) { Club.create!(name: "Billing Club", sport: "volleyball", primary_color: "#e53935", secondary_color: "#ffffff") }

  before do
    load Rails.root.join("db/seeds/plans.rb")
    Billing::AssignPlan.call(
      club: club,
      plan_slug: "premium_light",
      status: "active",
      stripe_attrs: {
        stripe_customer_id: "cus_test",
        stripe_subscription_id: "sub_test_123",
        billing_interval: "monthly",
        current_period_end: 1.month.from_now
      }
    )
    allow(MatchLiveTv).to receive(:stripe_enabled?).and_return(true)
    allow(MatchLiveTv).to receive(:stripe_premium_light_monthly_price_id).and_return("price_light_m")
    allow(MatchLiveTv).to receive(:stripe_premium_light_yearly_price_id).and_return("price_light_y")
    allow(MatchLiveTv).to receive(:stripe_premium_full_monthly_price_id).and_return("price_full_m")
    allow(MatchLiveTv).to receive(:stripe_premium_full_yearly_price_id).and_return("price_full_y")
  end

  it "upgrade: always_invoice e addebito immediato" do
    stripe_item = double(id: "si_test", price: double(id: "price_light_m"))
    stripe_sub = double(
      id: "sub_test_123",
      customer: "cus_test",
      status: "active",
      cancel_at_period_end: false,
      items: double(data: [stripe_item])
    )
    allow(stripe_sub).to receive(:current_period_start).and_return(Time.current.to_i)
    allow(stripe_sub).to receive(:current_period_end).and_return(1.month.from_now.to_i)
    allow(stripe_item).to receive(:current_period_start).and_return(Time.current.to_i)
    allow(stripe_item).to receive(:current_period_end).and_return(1.month.from_now.to_i)

    invoice = double(id: "in_up", status: "paid", amount_paid: 1500, currency: "eur")
    allow(Stripe::Subscription).to receive(:retrieve).with("sub_test_123").and_return(stripe_sub)
    allow(Stripe::Subscription).to receive(:update).and_return(stripe_sub)
    allow(Stripe::Invoice).to receive(:list).and_return(double(data: [invoice]))
    allow(Billing::Stripe::RecordPayment).to receive(:call)

    result = described_class.call(club: club, plan_slug: "premium_full", interval: "monthly")

    expect(Stripe::Subscription).to have_received(:update).with(
      "sub_test_123",
      hash_including(proration_behavior: "always_invoice", payment_behavior: "error_if_incomplete")
    )
    expect(result).to be_upgrade
    expect(club.reload.subscription.plan.slug).to eq("premium_full")
  end

  it "downgrade: programma ScheduleDowngrade senza cambiare piano subito" do
    Billing::AssignPlan.call(
      club: club,
      plan_slug: "premium_full",
      status: "active",
      stripe_attrs: { stripe_subscription_id: "sub_test_123", billing_interval: "monthly" }
    )

    allow(Billing::Stripe::ScheduleDowngrade).to receive(:call).and_return(
      Billing::Stripe::ChangePlanResult.new(
        kind: :downgrade,
        plan_slug: "premium_light",
        interval: "monthly",
        effective_at: 1.month.from_now
      )
    )

    result = described_class.call(club: club, plan_slug: "premium_light", interval: "monthly")

    expect(Billing::Stripe::ScheduleDowngrade).to have_received(:call)
    expect(result).to be_downgrade
    expect(club.reload.subscription.plan.slug).to eq("premium_full")
  end
end
