# frozen_string_literal: true

module Api
  module Admin
    # This serializer relies on `object` to respond to `#balance_value`. That's done in
    # `CustomersWithBalanceQuery` due to the fact that ActiveRecord maps the DB result set's
    # columns to instance methods. This way, the `balance_value` alias on that class ends up being
    # `object.balance_value` here.
    class CustomerWithBalanceSerializer < CustomerSerializer
      attributes :balance, :balance_status, :available_credit, :available_credit_url

      delegate :balance_value, :credit_value, to: :object

      def balance
        Spree::Money.new(balance_value, currency: CurrentConfig.get(:currency)).to_s
      end

      def balance_status
        if balance_value.positive?
          "credit_owed"
        elsif balance_value.negative?
          "balance_due"
        else
          ""
        end
      end

      def available_credit
        Spree::Money.new(object.credit_value).to_s
      end

      def available_credit_url
        admin_customer_customer_account_transaction_index_path(object.id)
      end
    end
  end
end
