<?php

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


class Wiaas_Package {

	public static function init() {
		require_once dirname( __FILE__ ) . '/package/class-wiaas-package-addon.php';
		require_once dirname( __FILE__ ) . '/package/class-wiaas-package-option-groups.php';

		require_once dirname( __FILE__ ) . '/package/class-wiaas-package-type.php';
		require_once dirname( __FILE__ ) . '/package/class-wiaas-package-status.php';

		require_once dirname( __FILE__ ) . '/package/wiaas-package-functions.php';

		add_filter('woocommerce_rest_prepare_product_object', array(__CLASS__, 'transform_rest_package'), 999, 3);
	}

	/**
	 * Transform package json response with wiaas information
	 * @param $response
	 * @param $package
	 * @param $request
	 *
	 * @return array
	 */
	public static function transform_rest_package($response, $package, $request) {
		$data = $response->get_data();

		$data = self::_append_country_info($data, $package, $request);

		// append package reference field
		$data['reference'] = ! empty($data['tags']) ? $data['tags'][0]['name'] : '';

		if (isset($request['id'])) {
			$data = self::_append_package_prices($data, $package, $request);

			$data = self::_append_documents_info($data, $package, $request);

			$data = self::_append_additional_packages($data, $package, $request);
		}

		$response->set_data($data);

		return $response;
	}

	/**
	 * Append package documents
	 * @param $data
	 * @param $package
	 * @param $request
	 *
	 * @return array
	 */
	private static function _append_documents_info($data, $package, $request) {

		$data['documents'] =  array_map(function($doc) {
			unset($doc['url']);
			unset($doc['version']);
			return $doc;
		}, wiaas_get_standard_package_documents($package, true));

		return $data;
	}

	/**
	 * Append package country and currency info
	 * @param $data
	 * @param $package
	 * @param $request
	 *
	 * @return array
	 */
	private static function _append_country_info($data, $package, $request) {
		$package_country = Wiaas_Countries::get_package_country($package);

		if (isset($package_country)) {
			$data['country'] = $package_country['name'];
			$data['country_code'] = $package_country['code'];
			$data['currency'] = $package_country['currency'];
		}

		return $data;
	}

	/**
	 * Append package addons and options
	 * @param $data
	 * @param $package
	 * @param $request
	 *
	 * @return array
	 */
	private static function _append_additional_packages($data, $package, $request) {
        $customer_id = wiaas_get_current_user_organization_id();
		$commercial_lead_id = absint($request['shop_id']);

		$data['additional_packages'] = array();
		$addons = Wiaas_Package_Addon::get_package_addons($package);
		foreach ($addons as $addon) {
			$data['additional_packages'][] = array(
				'id' => $addon->get_id(),
				'name' => $addon->get_name(),
				'description' => $addon->get_description(),
				'prices' => Wiaas_Pricing::get_addon_package_customer_price($addon, $package, $customer_id, $commercial_lead_id),
			);
		}

		$data['groups'] = array();
		$option_groups = Wiaas_Package_Option_Groups::get_package_option_groups($package);
		foreach ($option_groups as $option_group) {
			$data['groups'][$option_group['id']] = array(
				'id' => $option_group['id'],
				'name' => $option_group['name'],
				'default' => $option_group['default'],
				'options' => array()
			);
			$default_option_id = (int) $option_group['default'];

			foreach ($option_group['options'] as $option_package) {
				$data['groups'][$option_group['id']]['options'][] = array(
					'id' => $option_package->get_id(),
					'name' => $option_package->get_name(),
					'description' => $option_package->get_description(),
					'default' => $default_option_id === $option_package->get_id(),
					'prices' => Wiaas_Pricing::get_option_package_customer_price($option_package, $package, $customer_id, $commercial_lead_id),
				);
			}
		}

		return $data;
	}

	/**
	 * Append configured package prices on package json response
	 * @param $data
	 * @param $package
	 * @param $request
	 *
	 * @return array
	 */
	private static function _append_package_prices($data, $package, $request) {
        $customer_id = wiaas_get_current_user_organization_id();
        $commercial_lead_id = absint($request['shop_id']);

        $data['prices'] = Wiaas_Pricing::get_standard_package_customer_prices($package, $customer_id, $commercial_lead_id);

		return $data;
	}
}

Wiaas_Package::init();