<?php

/**
 * Base directory for wiaas document uploads
 * @return string
 */
function wiaas_documents_base_dir() {
	return '/woocommerce_uploads';
}

/**
 * Upload directory for wiaas document uploads
 * @return string
 */
function wiaas_documents_upload_dir() {
	$wp_uploads     = wp_upload_dir();
	$wp_uploads_dir = $wp_uploads['basedir'];

	return $wp_uploads_dir . wiaas_documents_base_dir();
}

/**
 * Retrieve upload path for wiaas document version
 * @param string $version
 *
 * @return string
 */
function wiaas_get_document_version_path($version) {
	return wiaas_documents_upload_dir() . '/' . $version;
}

/**
 * Retrieve if wiaas document version exists
 * @param string $version
 *
 * @return bool
 */
function wiaas_document_version_exists($version) {
	return file_exists(wiaas_get_document_version_path($version));
}

/**
 * Retrieve wiaas document version from file path
 * (that is relative path from wiaas document upload dir)
 * @param string $path
 *
 * @return string
 */
function wiaas_get_doc_version_from_path($path) {
	return str_replace(wiaas_documents_upload_dir() . '/', '', $path);
}

/**
 * Retrieve filename for wiaas document version
 * @param string $version
 *
 * @return string
 */
function wiaas_get_doc_version_filename($version) {
	return pathinfo( $version, PATHINFO_FILENAME );
}

/**
 * Retrieve extension for wiaas document version
 * @param string $version
 *
 * @return string
 */
function wiaas_get_doc_version_extension($version) {
	return pathinfo( $version, PATHINFO_EXTENSION );
}

/**
 * Attach wiaas document to post object
 * @param int $object_id
 * @param array $document_ids
 */
function wiaas_attach_documents_to_object($object_id, $document_ids) {
	update_post_meta($object_id, '_wiaas_attached_documents', $document_ids);
}

/**
 * Retrieve wiaas documents ids for post object
 * @param int $object_id
 *
 * @return array
 */
function wiaas_get_object_attached_documents($object_id) {
	$document_ids = get_post_meta($object_id, '_wiaas_attached_documents', true);
	return isset($document_ids) && !empty($document_ids) ? $document_ids : array();
}

/**
 * Retrieve all documents for single standard wiaas package
 *
 * this includes:
 * - package linked documents
 * - addons linked documents
 * - options linked documents
 * - all documents attacked to bundled products of package, addons and options
 *
 * @param int $package
 * @param bool $only_visible
 *
 * @return array
 */
function wiaas_get_standard_package_documents($package, $only_visible = false) {
	// retrieve addons and option packages ids
	$all_package_ids = array_merge(
		Wiaas_Package_Addon::get_package_addons_ids($package),
		Wiaas_Package_Option_Groups::get_package_option_ids($package),
		array($package->get_id())
	);

	// retrieve all bundled products from all relevant packages
	$all_product_ids = array_values(
		WC_PB_DB::query_bundled_items(array(
			'return' => 'id=>product_id',
			'bundle_id' => $all_package_ids
		))
	);

	$all_object_ids = array_unique(array_merge($all_package_ids, $all_product_ids));

	// retrieve document ids from all packages and products
	$document_ids = array();
	foreach ($all_object_ids as $object_id) {
		$document_ids = array_merge(
			$document_ids,
			wiaas_get_object_attached_documents($object_id));
	}
	$document_ids = array_unique($document_ids);

	// filter only visible if needed and  retrieve documents info
	$documents = array();
	foreach ($document_ids as $document_id) {
		if ($only_visible && Wiaas_Document::is_doc_visible($document_id) || !$only_visible) {
			$documents[] = Wiaas_Document::get_doc_info($document_id);
		}
	}

	return $documents;
}

/**
 * Retrieve all documents for single order standard package item
 *
 * @param WC_Order $order
 * @param int $package_item_id
 *
 * @return array
 */
function wiaas_get_standard_package_order_item_documents($order, $package_item_id) {

	$order_items = $order->get_items( 'line_item' );
	$package_order_item = $order->get_item($package_item_id);

	// retrieve package order item addons and options
	$all_package_order_items = array_merge(
		wiaas_get_order_item_addons($order_items, $package_order_item),
		wiaas_get_order_item_options($order_items, $package_order_item),
		array( $package_order_item )
	);
	$all_order_items = $all_package_order_items;

	// retrieve bundled product items for each package
	foreach ($all_package_order_items as $item) {
		$all_order_items = array_merge(
			$all_order_items,
			wc_pb_get_bundled_order_items($item, $order)
		);
	}


	$order_documents = array();
	foreach ($all_order_items as $order_item) {
		$order_documents = array_merge(
			$order_documents,
			isset($order_item['wiaas_documents']) ? $order_item['wiaas_documents'] : array()
		);
	}

	return array_map(function($doc) {
		// append document extension and name information
		$doc['extension'] = wiaas_get_doc_version_extension($doc['version']);
		$doc['name'] = isset($doc['name']) ? $doc['name'] : wiaas_get_doc_version_filename($doc['version']);

		return $doc;
	}, $order_documents);
}