<?php

/**
 * Handles logic for setting and retrieving commercial lead extra prices
 *
 * Class Wiaas_Package_CL_Pricing
 */
class Wiaas_Package_CL_Pricing {

    /**
     * Sets package extra prices
     *
     * @param int $organization_id
     * @param int $package_id
     * @param  array $cl_extras
     */
    public static function set_extras($organization_id, $package_id, $cl_extras) {
        //self::_assign_catalogue_attribute_options($organization_id, $package_id, $cl_extras);

        $has_extras = false;

        foreach ($cl_extras as $extra_type => $cl_extra) {
            unset($cl_extra['type']);
            unset($cl_extra['customer']);

            $has_extras = $has_extras || $cl_extra['visible'];

            $cl_extras[$extra_type] = $cl_extra;
        }

        $package = wc_get_product($package_id);

        $package->add_meta_data('_wiaas_catalogue_'.$organization_id, $has_extras ? 'yes' : 'no', true);
        $package->save_meta_data();

        // Persist package catalogue extras
        update_term_meta(
            $organization_id,
            '_wiaas_cm_extras_'.$package_id,
            $cl_extras);
    }

    /**
     * Retrieves package catalogue extra prices
     *
     * @param int $organization_id
     * @param int $package_id
     *
     * @return array|null
     */
    public static function get_extras($organization_id, $package_id) {
        $cl_extras = get_term_meta(
            $organization_id,
            '_wiaas_cm_extras_'.$package_id,
            true);

        return is_array($cl_extras) ? $cl_extras : null;
    }

    /**
     * Retrieve catalogue package extra prices for provided customer
     *
     * @param int $organization_id
     * @param string $pay_type
     * @param int $package_id
     * @param int $customer_id
     *
     * @return array|null
     */
    public static function get_extras_for_customer($organization_id, $pay_type, $package_id, $customer_id) {
        $cl_extras = self::get_extras($organization_id, $package_id);

        if (! empty($cl_extras)) {
            // retrieve default and customer specific extras
            $cl_pay_type_customer_extras = $cl_extras[$pay_type.'_customer_'.$customer_id];
            $cl_pay_type_extras =  $cl_extras[$pay_type.'_default'];

            // if commercial lead has special prices for this customer then use them
            if (! empty($cl_pay_type_customer_extras) && $cl_pay_type_customer_extras['visible']) {

                return array(
                    'fixed' => floatval($cl_pay_type_customer_extras['fixed']),
                    'services' => floatval($cl_pay_type_customer_extras['services']),
                    'recurrent' => floatval($cl_pay_type_customer_extras['recurrent'])
                );
            }

            // if commercial lead has default price for this package then use it
            if (! empty($cl_pay_type_extras) && $cl_pay_type_extras['visible']) {

                return array(
                    'fixed' => floatval($cl_pay_type_extras['fixed']),
                    'services' => floatval($cl_pay_type_extras['services']),
                    'recurrent' => floatval($cl_pay_type_extras['recurrent'])
                );
            }
        }

        // there are no prices set for this payment type
        return null;
    }
}
