<?php

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

/**
 * TODO: This is temporary implemetation and will probably be changed during work on pending wiaas cart task
 * Class Wiaas_Document_API
 */
class Wiaas_Document_API {
	/**
	 * Endpoint namespace.
	 *
	 * @var string
	 */
	private static $namespace = 'wiaas';

	public static function register_routes() {
		register_rest_route( self::$namespace, 'download-package-file', array(
			'methods' => 'GET',
			'permission_callback' => 'is_user_logged_in',
			'callback' => array(__CLASS__, 'download_package_file'),
		) );
	}

	/**
	 * Download package document
	 */
	public static function download_package_file() {
		$document_id = $_GET['document_id'];
		$package_id = $_GET['package_id'];

		$package = wc_get_product($package_id);

		$file = $package->get_file($document_id);

		if ($file) {
			WC_Download_Handler::download_file_force($package->get_file_download_path($document_id), $file->get_name());
		}
	}
}