<?php

class Wiaas_Cart_API {
	/**
	 * Endpoint namespace.
	 *
	 * @var string
	 */
	private static $namespace = 'wiaas';

	private static $rest_base = 'cart';

	public static function register_routes() {
		register_rest_route( self::$namespace, '/' . self::$rest_base . '/count', array(
			'methods' => WP_REST_Server::READABLE,
			'callback' => array(__CLASS__, 'get_cart_count'),
			'permission_callback' => 'is_user_logged_in',
		) );

		register_rest_route( self::$namespace, '/' . self::$rest_base . '/items', array(
			array(
				'methods' => WP_REST_Server::READABLE,
				'callback' => array(__CLASS__, 'get_cart_items'),
				'permission_callback' => 'is_user_logged_in',
			),
			array(
				'methods' => WP_REST_Server::CREATABLE,
				'callback' => array(__CLASS__, 'add_package_to_cart'),
				'permission_callback' => 'is_user_logged_in',
				'args' => array(
					'package_id' => array(
						'description' => __( 'Wiaas package ID.', 'wiaas' ),
						'type'        => 'integer',
						'required' => true,
						'sanitize_callback' => 'absint',
					),
					'cl_id' => array(
						'description' => __( 'Commercial lead ID.', 'wiaas' ),
						'type'        => 'integer',
						'required' => true,
						'sanitize_callback' => 'absint',
					),
					'price_id' => array(
						'description' => __( 'Selected price ID for Wiaas package.', 'wiaas' ),
						'type'        => 'string',
						'enum' => array_keys(Wiaas_Package_Pricing::get_available_pay_types()),
						'required' => true,
						'sanitize_callback' => 'sanitize_key',
					),
					'options_ids' => array(
						'description' => __( 'Wiaas package options IDs.', 'wiaas' ),
						'type'        => 'array',
						'items' => array(
							'type'       => 'integer',
							'sanitize_callback' => 'absint',
						)
					),
					'addons_ids' => array(
						'description' => __( 'Wiaas package addons IDs.', 'wiaas' ),
						'type'        => 'array',
						'items' => array(
							'type'       => 'integer',
							'sanitize_callback' => 'absint',
						)
					)
				)
			),
		) );

		register_rest_route( self::$namespace, '/' . self::$rest_base . '/items/(?P<key>[\w-]+)', array(
			'args'   => array(
				'key' => array(
					'description' => __( 'Unique key identifier for cart package item.', 'wiaas' ),
					'type'        => 'string',
					'sanitize_callback' => 'sanitize_key',
				),
			),
			array(
				'methods' => WP_REST_Server::DELETABLE,
				'callback' => array(__CLASS__, 'remove_package_from_cart'),
				'permission_callback' => 'is_user_logged_in',
			),
			array(
				'methods' => WP_REST_Server::EDITABLE,
				'callback' => array(__CLASS__, 'update_package_quantity'),
				'permission_callback' => 'is_user_logged_in',
				'args' => array(
					'quantity' => array(
						'description' => __( 'New quantity cart package item.', 'wiaas' ),
						'type'        => 'integer',
						'sanitize_callback' => 'absint',
					)
				)
			)
		) );

		register_rest_route(self::$namespace, '/' .  self::$rest_base . '/documents', array(
			array(
				'methods' => WP_REST_Server::READABLE,
				'callback' => array(__CLASS__, 'get_cart_documents'),
				'permission_callback' => 'is_user_logged_in',
			),
			array(
				'methods' => WP_REST_Server::CREATABLE,
				'callback' => array(__CLASS__, 'upload_cart_document'),
				'permission_callback' => 'is_user_logged_in',
				'args' => array(
					'doc_type' => array(
						'description' => __( 'Category of uploaded document.', 'wiaas' ),
						'type'        => 'string',
						'sanitize_callback' => 'sanitize_key',
						'required' => true
					),
					'package_key' => array(
						'description' => __( 'Unique key identifier for cart package item.', 'wiaas' ),
						'type'        => 'string',
						'sanitize_callback' => 'sanitize_key',
						'required' => true
					),
					'type' => array(
						'default' => 'wiaas_doc'
					)
				)
			)
		));

		register_rest_route(self::$namespace, '/' .  self::$rest_base . '/items/(?P<key>[\w-]+)/documents/(?P<type>[\w-]+)', array(
			'args'   => array(
				'key' => array(
					'description' => __( 'Unique key identifier for cart package item.', 'wiaas' ),
					'type'        => 'string',
					'sanitize_callback' => 'sanitize_key',
				),
				'type' => array(
					'description' => __( 'Cart document Type.', 'wiaas' ),
					'type'        => 'string',
					'sanitize_callback' => 'sanitize_key',
				),
			),
			array(
				'methods' => WP_REST_Server::READABLE,
				'callback' => array(__CLASS__, 'download_cart_document'),
				'permission_callback' => 'is_user_logged_in',
				'args' => array(
					'document_key' => array(
						'description' => __( 'Unique key identifier for cart document.', 'wiaas' ),
						'type'        => 'string',
						'sanitize_callback' => 'sanitize_key',
						'required' => true
					),
				)
			),
		));

		register_rest_route( self::$namespace, '/' . self::$rest_base . '/checkout', array(
			'methods' => WP_REST_Server::CREATABLE,
			'callback' => array(__CLASS__, 'checkout'),
			'permission_callback' => 'is_user_logged_in',
			'args' => array(
				'vat' => array(
					'description' => __( 'Vat Code for new order.', 'wiaas' ),
					'type'        => 'string',
					'required' => true,
					'sanitize_callback' => 'sanitize_key',
				),
				'company' => array(
					'description' => __( 'Company name for new order.', 'wiaas' ),
					'type'        => 'string',
					'required' => true,
					'sanitize_callback' => 'sanitize_key',
				),
				'reference' => array(
					'description' => __( 'Location details for new order.', 'wiaas' ),
					'type'        => 'string',
					'sanitize_callback' => 'sanitize_key',
				),
				'tender' => array(
					'description' => __( 'Invoice reference for new order.', 'wiaas' ),
					'type'        => 'string',
					'sanitize_callback' => 'sanitize_key',
				),
				'project_id' => array(
					'description' => __( 'Order project ID for new order.', 'wiaas' ),
					'type'        => 'integer',
					'sanitize_callback' => 'absint',
				),
				'delivery_address_id' => array(
					'description' => __( 'ID of delivery address for new order.', 'wiaas' ),
					'type'        => 'integer',
					'required' => true,
					'sanitize_callback' => 'absint',
				),
				'billing_address_id' => array(
					'description' => __( 'ID of billing address for new order.', 'wiaas' ),
					'type'        => 'integer',
					'required' => true,
					'sanitize_callback' => 'absint',
				),
			)
		) );
	}

	/**
	 * Get cart count
	 * @return WP_REST_Response
	 */
	public static function get_cart_count() {
		return rest_ensure_response(array(
			'count' => Wiaas_Cart::get_cart_packages_count(),
		));
	}

	/**
	 * Get cart items
	 *
	 * @return WP_REST_Response
	 */
	public static function get_cart_items() {
		return rest_ensure_response(array(
			'items' => Wiaas_Cart::get_cart_packages(),
		));
	}


	/**
	 * @param WP_REST_Request $request Request data.
	 *
	 * @return WP_REST_Response
	 */
	public static function add_package_to_cart($request) {

		$success = Wiaas_Cart::add_package_to_cart(
			$request['package_id'],
			$request['price_id'],
			$request['cl_id'],
			$request['addons_ids'],
			$request['options_ids']
		);

		if (!$success) {
			return (wc_notice_count('error') > 0) ?
				wiaas_api_cart_error_notices() :
				wiaas_api_notice('PACKAGE_ALREADY_IN_CART', 'error');
		}

		return wiaas_api_notice('PACKAGE_ADDED', 'success');
	}

	/**
	 * @param WP_REST_Request $request Request data.
	 *
	 * @return WP_REST_Response
	 */
	public static function remove_package_from_cart($request) {

		$success = Wiaas_Cart::remove_package_from_cart($request['key']);

		if (!$success) {
			return (wc_notice_count('error') > 0) ?
				wiaas_api_cart_error_notices() :
				wiaas_api_notice('INVALID_PACKAGE_FOR_REMOVE', 'error');
		}

		return wiaas_api_notice('PACKAGE_REMOVED_FROM_CART', 'success');
	}

	/**
	 * Update package quantity in cart
	 * @return WP_REST_Response
	 */
	public static function update_package_quantity($request) {

		$success = Wiaas_Cart::update_package_quantity($request['key'], $request['quantity']);

		if (!$success) {
			return (wc_notice_count('error') > 0) ?
				wiaas_api_cart_error_notices() :
				wiaas_api_notice('QUANTITY_NOT_UPDATED', 'error');
		}

		return wiaas_api_notice('QUANTITY_UPDATED', 'success');
	}

	/**
	 * Retrive cart documents info
	 *
	 * @return mixed|WP_REST_Response
	 */
	public static function get_cart_documents() {
		return rest_ensure_response( Wiaas_Cart::get_cart_documents());
	}

	/**
	 * Upload document to cart
	 * @param WP_REST_Request $request
	 *
	 * @return mixed|WP_REST_Response
	 */
	public static function upload_cart_document($request) {
		$success = Wiaas_Cart::upload_cart_document($request['doc_type'], $request['package_key']);

		if (!$success) {
			return (wc_notice_count('error') > 0) ?
				wiaas_api_cart_error_notices() :
				wiaas_api_generate_error('UPLOAD_ERROR');
		}

		return wiaas_api_notice('FILE_UPLOADED', 'success');
	}

	/**
	 * Download cart document
	 * @param WP_REST_Request $request
	 */
	public static function download_cart_document($request) {
		Wiaas_Document_Download::download_cart_document(
			$request['key'],
			$request['type'],
			$request['document_key']);
	}


	/**
	 * @param WP_REST_Request $request Request data.
	 *
	 * @return WP_REST_Response
	 */
	public static function checkout($request) {

		Wiaas_Checkout::process_checkout($request->get_body_params());

		if (wc_notice_count('error') > 0) {
			return wiaas_api_cart_error_notices();
		}

		return wiaas_api_notice('ORDER_PLACED', 'success');
	}
}