module Billing
  module Stripe
    module PlanChangeKind
      module_function

      def classify(from_plan_slug:, from_interval:, to_plan_slug:, to_interval:)
        from_tier = Plan.tier(from_plan_slug)
        to_tier = Plan.tier(to_plan_slug)
        from_interval = PriceCatalog.normalize_interval(from_interval)
        to_interval = PriceCatalog.normalize_interval(to_interval)

        return :upgrade if to_tier > from_tier
        return :downgrade if to_tier < from_tier

        if from_interval == to_interval
          :same
        elsif to_interval == "yearly"
          :upgrade
        else
          :downgrade
        end
      end

      def upgrade?(**kwargs)
        classify(**kwargs) == :upgrade
      end

      def downgrade?(**kwargs)
        classify(**kwargs) == :downgrade
      end
    end
  end
end
