# frozen_string_literal: true

require "tasks/sample_data/addressing"
require "tasks/sample_data/logging"

module SampleData
  class PaymentMethodFactory
    include Logging
    include Addressing

    def create_samples(enterprises)
      log "Creating payment methods:"
      distributors = enterprises.select(&:is_distributor)
      distributors.each do |enterprise|
        create_payment_methods(enterprise)
      end
      create_api_method(distributors)
    end

    private

    def create_payment_methods(enterprise)
      return if enterprise.payment_methods.present?

      log "- #{enterprise.name}"
      create_cash_method(enterprise)
    end

    def create_cash_method(enterprise)
      create_payment_method(
        Spree::PaymentMethod::Check,
        enterprise,
        "Cash on collection",
        "Pay on collection!",
        ::Calculator::FlatRate.new
      )
    end

    def create_payment_method(provider_class, enterprise, name, description, calculator)
      payment_method = provider_class.new(
        name:,
        description:,
        environment: Rails.env,
        distributor_ids: [enterprise.id]
      )
      payment_method.calculator = calculator
      payment_method.save!
    end

    def create_api_method(enterprises)
      log "Creating api payment method"
      Spree::PaymentMethod::Check.create!(
        name: "api_payment_method.name",
        description: "api_payment_method.description",
        environment: Rails.env,
        distributors: enterprises,
        display_on: "back_end"
      )
    end
  end
end
