#ifndef STAN_MATH_PRIM_PROB_PARETO_LCCDF_HPP
#define STAN_MATH_PRIM_PROB_PARETO_LCCDF_HPP

#include <stan/math/prim/meta.hpp>
#include <stan/math/prim/err.hpp>
#include <stan/math/prim/fun/as_column_vector_or_scalar.hpp>
#include <stan/math/prim/fun/as_array_or_scalar.hpp>
#include <stan/math/prim/fun/constants.hpp>
#include <stan/math/prim/fun/exp.hpp>
#include <stan/math/prim/fun/log.hpp>
#include <stan/math/prim/fun/max_size.hpp>
#include <stan/math/prim/fun/promote_scalar.hpp>
#include <stan/math/prim/fun/size.hpp>
#include <stan/math/prim/fun/size_zero.hpp>
#include <stan/math/prim/fun/to_ref.hpp>
#include <stan/math/prim/fun/value_of.hpp>
#include <stan/math/prim/functor/operands_and_partials.hpp>
#include <cmath>

namespace stan {
namespace math {

template <typename T_y, typename T_scale, typename T_shape>
return_type_t<T_y, T_scale, T_shape> pareto_lccdf(const T_y& y,
                                                  const T_scale& y_min,
                                                  const T_shape& alpha) {
  using T_partials_return = partials_return_t<T_y, T_scale, T_shape>;
  using T_y_ref = ref_type_if_t<!is_constant<T_y>::value, T_y>;
  using T_y_min_ref = ref_type_if_t<!is_constant<T_scale>::value, T_scale>;
  using T_alpha_ref = ref_type_if_t<!is_constant<T_shape>::value, T_shape>;
  using std::isinf;
  static const char* function = "pareto_lccdf";
  check_consistent_sizes(function, "Random variable", y, "Scale parameter",
                         y_min, "Shape parameter", alpha);
  if (size_zero(y, y_min, alpha)) {
    return 0;
  }

  T_y_ref y_ref = y;
  T_y_min_ref y_min_ref = y_min;
  T_alpha_ref alpha_ref = alpha;

  const auto& y_col = as_column_vector_or_scalar(y_ref);
  const auto& y_min_col = as_column_vector_or_scalar(y_min_ref);
  const auto& alpha_col = as_column_vector_or_scalar(alpha_ref);

  const auto& y_arr = as_array_or_scalar(y_col);
  const auto& y_min_arr = as_array_or_scalar(y_min_col);
  const auto& alpha_arr = as_array_or_scalar(alpha_col);

  ref_type_t<decltype(value_of(y_arr))> y_val = value_of(y_arr);
  ref_type_t<decltype(value_of(y_min_arr))> y_min_val = value_of(y_min_arr);
  ref_type_t<decltype(value_of(alpha_arr))> alpha_val = value_of(alpha_arr);

  check_nonnegative(function, "Random variable", y_val);
  check_positive_finite(function, "Scale parameter", y_min_val);
  check_positive_finite(function, "Shape parameter", alpha_val);

  operands_and_partials<T_y_ref, T_y_min_ref, T_alpha_ref> ops_partials(
      y_ref, y_min_ref, alpha_ref);

  if (sum(promote_scalar<int>(y_val < y_min_val))) {
    return ops_partials.build(0.0);
  }
  if (sum(promote_scalar<int>(isinf(y_val)))) {
    return ops_partials.build(negative_infinity());
  }

  const auto& log_quot = to_ref_if<(!is_constant_all<T_y>::value
                                    || !is_constant_all<T_shape>::value)>(
      log(y_min_val / y_val));

  T_partials_return P = sum(alpha_val * log_quot);

  size_t N = max_size(y, y_min, alpha);
  if (!is_constant_all<T_y, T_scale>::value) {
    const auto& alpha_div_y_min = to_ref_if<(
        !is_constant_all<T_y>::value && !is_constant_all<T_scale>::value)>(
        alpha_val / y_min_val);
    if (!is_constant_all<T_y>::value) {
      ops_partials.edge1_.partials_ = -alpha_div_y_min * exp(log_quot);
    }
    if (!is_constant_all<T_scale>::value) {
      ops_partials.edge2_.partials_
          = alpha_div_y_min * N / max_size(y_min, alpha);
    }
  }
  if (!is_constant_all<T_shape>::value) {
    if (is_vector<T_shape>::value) {
      using Log_quot_scalar = partials_return_t<T_y, T_scale>;
      using Log_quot_array = Eigen::Array<Log_quot_scalar, Eigen::Dynamic, 1>;
      if (is_vector<T_y>::value || is_vector<T_scale>::value) {
        ops_partials.edge3_.partials_
            = forward_as<Log_quot_array>(std::move(log_quot));
      } else {
        ops_partials.edge3_.partials_ = Log_quot_array::Constant(
            N, 1, forward_as<Log_quot_scalar>(log_quot));
      }
    } else {
      forward_as<internal::broadcast_array<T_partials_return>>(
          ops_partials.edge3_.partials_)
          = log_quot * N / max_size(y, y_min);
    }
  }
  return ops_partials.build(P);
}

}  // namespace math
}  // namespace stan
#endif
