<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * Class Wiaas_Pricing
 */
class Wiaas_Pricing {

	/**
	 * Wiaas interest rate
	 */
	const INTEREST_RATE = 0.58;

	const COMMERCIAL_LEAD_NAME = 'Coor Service Management';

	public static function init() {
		require_once dirname( __FILE__ ) . '/pricing/class-wiaas-product-pricing.php';
		require_once dirname( __FILE__ ) . '/pricing/class-wiaas-package-pricing.php';
		require_once dirname( __FILE__ ) . '/pricing/wiaas-pricing-functions.php';
	}

	/**
	 * Calculates total cost for product
	 * @param $product
	 *
	 * @return float
	 */
	public static function get_product_total_cost($product) {
		$product_price = Wiaas_Product_Pricing::get_product_price($product);

		return $product_price['is_recurring'] ?
			$product_price['price'] * $product_price['pay_period'] :
			$product_price['price'];
	}

	/**
	 * Calculates total cost for package
	 * @param $package
	 *
	 * @return float
	 */
	public static function get_package_total_cost($package) {

		$total_cost_per_category = array();

		$bundled_items = $package->get_bundled_items();
		foreach ($bundled_items as $bundled_item) {

			$product = $bundled_item->product;
			$product_cat = Wiaas_Product_Category::get_category($product);

			if (!isset($total_cost_per_category[$product_cat])) {
				$total_cost_per_category[$product_cat] = 0;
			}

			$total_item_cost = self::get_product_total_cost($product) * $bundled_item->get_quantity();

			if (Wiaas_Product_Category::is_installation($product)) {
				$total_cost_per_category[$product_cat] = $total_cost_per_category[$product_cat] < $total_item_cost ?
					$total_item_cost :
					$total_cost_per_category[$product_cat];
			} else {
				$total_cost_per_category[$product_cat] += $total_item_cost;
			}
		}

		return array_sum(array_values($total_cost_per_category));
	}

	/**
	 * Calculates customer price for wiaas standard package
	 * @param $package
	 *
	 * @return array
	 */
	public static function get_standard_package_customer_prices($package) {

		$is_same_company_as_cl = self::_is_customer_same_company_as_cl();
		$package_prices = Wiaas_Package_Pricing::get_package_prices($package);
		$cl_commision = (100 - Wiaas_Package_Pricing::get_package_pricing_commission($package)) / 100;
		$total_cost = self::get_package_total_cost($package);

		$customer_package_prices = array();

		foreach ($package_prices as $type => $package_price) {
			$customer_package_prices[] = self::_get_package_customer_price(
												$package_price,
												$cl_commision,
												$total_cost,
												$is_same_company_as_cl);
		}

		return $customer_package_prices;

	}

	/**
	 * Calculates customer price for wiaas addon package
	 * @param $addon_package
	 * @param $parent_package
	 *
	 * @return array
	 */
	public static function get_addon_package_customer_price($addon_package, $parent_package) {

		$is_same_company_as_cl = self::_is_customer_same_company_as_cl();
		$parent_total_cost = self::get_package_total_cost($parent_package);
		$parent_cl_commision = (100 - Wiaas_Package_Pricing::get_package_pricing_commission($parent_package)) / 100;
		$addon_package_prices = Wiaas_Package_Pricing::get_package_prices($addon_package);

		$customer_package_prices = array();

		foreach ($addon_package_prices as $type => $addon_package_price) {
			$customer_package_prices[] = self::_get_package_customer_price(
				$addon_package_price,
				$parent_cl_commision,
				$parent_total_cost,
				$is_same_company_as_cl);
		}

		return $customer_package_prices;
	}

	/**
	 * Calculates customer price for wiaas option package
	 * @param $option_package
	 * @param $parent_package
	 *
	 * @return array
	 */
	public static function get_option_package_customer_price($option_package, $parent_package) {

		$is_same_company_as_cl = self::_is_customer_same_company_as_cl();
		$parent_total_cost = self::get_package_total_cost($parent_package);
		$parent_cl_commision = (100 - Wiaas_Package_Pricing::get_package_pricing_commission($parent_package)) / 100;
		$option_package_prices = Wiaas_Package_Pricing::get_package_prices($option_package);

		$customer_package_prices = array();

		foreach ($option_package_prices as $type => $option_package_price) {
			$customer_package_prices[] = self::_get_package_customer_price(
				$option_package_price,
				$parent_cl_commision,
				$parent_total_cost,
				$is_same_company_as_cl);
		}

		return $customer_package_prices;
	}

	// PRIVATE

	/**
	 * Determines if customer and commercial lead are in the same company
	 * For now this is hardcoded and we have only one CL
	 *
	 * TODO: This should be changed after customer leads are handled
	 * @return bool
	 */
	private static function _is_customer_same_company_as_cl() {
		$current_user = wp_get_current_user();
		$user_organization = Wiaas_User_Organization::get_user_organization($current_user->ID);
		$is_same_company_as_cl = $user_organization->name === self::COMMERCIAL_LEAD_NAME;

		return $is_same_company_as_cl;
	}

	/**
	 * Calculates customer price for wiaas package
	 * @param $package_price
	 * @param $cl_commision
	 * @param $total_cost
	 * @param $is_same_company_as_cl
	 *
	 * @return array
	 */
	private static function _get_package_customer_price($package_price, $cl_commision, $total_cost, $is_same_company_as_cl) {

		$package_total_margin = wiaas_get_price_margin(
			$package_price['minimal_fixed_price'],
			$package_price['principal_amount'],
			$total_cost);
		$cl_margin = $package_total_margin > 0 ? $package_total_margin * $cl_commision : 0;

		$cl_package_prices = array(
			'fixed_extra' => 0,
			'recurrent_extra' => 0,
			'services_extra' => 0
		);

		$interest_rate = self::INTEREST_RATE;

		$customer_price = array(
			'id' => $package_price['id'],
			'payment_type' => $package_price['payment_type'],
			'max_contract_period' => $package_price['max_contract_period'],
			'package_pay_period' => $package_price['package_pay_period'],
			'period_unit' => $package_price['period_unit'],
			'services_contract_period' => $package_price['services_contract_period'],

			'fixed_extra' => 0,
			'recurrent_extra' => 0,
			'services_extra' => 0
		);

		if ($is_same_company_as_cl) {

			if ($package_price['package_pay_period'] > 0) {
				$customer_price['fixed_extra'] = $package_price['minimal_fixed_price'];
				$customer_price['recurrent_extra'] = wiaas_get_recurrent_price_mortage(
					$package_price['principal_amount'],
					$package_price['package_pay_period'],
					$cl_margin,
					$interest_rate);
			} else {
				$customer_price['fixed_extra'] = $package_price['minimal_fixed_price'] - $cl_margin;
				$customer_price['recurrent_extra'] = 0;
			}
			$customer_price['services_extra'] = $package_price['minimal_services_price'];

		} else {

			$customer_price['fixed_extra'] = $cl_package_prices['fixed_extra'] + $package_price['minimal_fixed_price'];
			$customer_price['recurrent_extra'] = $package_price['package_pay_period'] > 0 ?
				$cl_package_prices['recurrent_extra'] +  wiaas_get_recurrent_price_mortage(
					$package_price['principal_amount'],
					$package_price['package_pay_period'],
					0,
					$interest_rate)
				: 0;
			$customer_price['services_extra'] = $cl_package_prices['services_extra'] + $package_price['minimal_services_price'];
		}

		return $customer_price;
	}
}

Wiaas_Pricing::init();