<?php

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

/**
 * Class Wiaas_Pricing
 */
class Wiaas_Pricing {

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

    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/class-wiaas-package-cl-pricing.php';
        require_once dirname( __FILE__ ) . '/pricing/wiaas-pricing-functions.php';
    }

    /**
     * Calculates total cost for product
     * @param WC_Product_Simple $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 WC_Product_Bundle $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 prices for wiaas standard package
     * @param WC_Product_Bundle $package
     * @param int $customer_id
     * @param int $commercial_lead_id
     *
     * @return array
     */
    public static function get_standard_package_customer_prices($package, $customer_id, $commercial_lead_id) {

        return self::_get_package_customer_prices(
            $package,
            (100 - Wiaas_Package_Pricing::get_package_pricing_commission($package)) / 100, // commercial lead commission
            self::get_package_total_cost($package), // total cost of package items
            $customer_id,
            $commercial_lead_id);

    }

    /**
     * Calculates customer prices for wiaas addon package
     *
     * @param WC_Product_Bundle $addon_package
     * @param WC_Product_Bundle $parent_package
     * @param int $customer_id
     * @param int $commercial_lead_id
     *
     * @return array
     */
    public static function get_addon_package_customer_price($addon_package, $parent_package, $customer_id, $commercial_lead_id) {

        $parent_total_cost = self::get_package_total_cost($parent_package);
        $parent_cl_commision = (100 - Wiaas_Package_Pricing::get_package_pricing_commission($parent_package)) / 100;

        return self::_get_package_customer_prices(
            $addon_package,
            $parent_cl_commision, // commercial lead commission of parent package
            $parent_total_cost, // total cost of parent package items
            $customer_id,
            $commercial_lead_id);
    }

    /**
     * Calculates customer prices for wiaas option package
     *
     * @param WC_Product_Bundle $option_package
     * @param WC_Product_Bundle $parent_package
     * @param int $customer_id
     * @param int $commercial_lead_id
     *
     * @return array
     */
    public static function get_option_package_customer_price($option_package, $parent_package, $customer_id, $commercial_lead_id) {

        $parent_total_cost = self::get_package_total_cost($parent_package);
        $parent_cl_commision = (100 - Wiaas_Package_Pricing::get_package_pricing_commission($parent_package)) / 100;

        return self::_get_package_customer_prices(
            $option_package,
            $parent_cl_commision, // commercial lead commission of parent package
            $parent_total_cost, // total cost of parent package items
            $customer_id,
            $commercial_lead_id);
    }




    // PRIVATE  SECTION


    /**
     * Calculates customer prices for all package payment types
     *
     * @param WC_Product_Bundle $package
     * @param int $cl_commision
     * @param int $total_cost
     * @param int $customer_id
     * @param int $commercial_lead_id
     *
     * @return array
     */
    private static function _get_package_customer_prices($package, $cl_commision, $total_cost, $customer_id, $commercial_lead_id) {
        $is_same_company_as_cl = $customer_id === $commercial_lead_id;

        $package_prices = Wiaas_Package_Pricing::get_package_prices($package);

        $customer_package_prices = array();

        foreach ($package_prices as $type => $package_price) {
            $cl_package_extras = Wiaas_Package_CL_Pricing::get_extras_for_customer(
                $commercial_lead_id,
                $package_price['id'],
                $package->get_id(),
                $customer_id
            );

            // commercial lead did not set prices for this payment type so it must not be visible for customer
            if (empty($cl_package_extras)) {
                continue;
            }

            $customer_package_prices[] = self::_get_package_customer_price(
                $package_price,
                $total_cost,
                $cl_package_extras,
                $cl_commision,
                $is_same_company_as_cl);
        }

        return $customer_package_prices;
    }

    /**
     * Calculates customer price for single package payment type
     *
     * @param array $package_price
     * @param int $total_cost
     * @param array $cl_package_extras
     * @param int $cl_commision
     * @param bool $is_same_company_as_cl
     *
     * @return array
     */
    private static function _get_package_customer_price($package_price, $total_cost, $cl_package_extras, $cl_commision, $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;

        $customer_package_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_package_price['fixed_extra'] = $package_price['minimal_fixed_price'];
                $customer_package_price['recurrent_extra'] = wiaas_get_recurrent_price_mortage(
                    $package_price['principal_amount'],
                    $package_price['package_pay_period'],
                    $cl_margin,
                    self::INTEREST_RATE);
            } else {
                $customer_package_price['fixed_extra'] = $package_price['minimal_fixed_price'] - $cl_margin;
                $customer_package_price['recurrent_extra'] = 0;
            }
            $customer_package_price['services_extra'] = $package_price['minimal_services_price'];

        } else {

            $customer_package_price['fixed_extra'] = $cl_package_extras['fixed'] + $package_price['minimal_fixed_price'];
            $customer_package_price['recurrent_extra'] = $package_price['package_pay_period'] > 0 ?
                $cl_package_extras['recurrent'] +  wiaas_get_recurrent_price_mortage(
                    $package_price['principal_amount'],
                    $package_price['package_pay_period'],
                    0,
                    self::INTEREST_RATE)
                : 0;
            $customer_package_price['services_extra'] = $cl_package_extras['services'] + $package_price['minimal_services_price'];
        }

        return $customer_package_price;
    }
}

Wiaas_Pricing::init();