<?php
namespace ZenCommunity\Api;
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
use WP_REST_Request;
use WP_REST_Response;
use WP_REST_Server;
use ZenCommunity\Database\Models\ProcessLog;
use ZenCommunity\Addons\AcademyLms\Api\Common\Permissions;
use ZenCommunity\Exceptions\ZencommunityException;
class BgProcess extends Common\Base {

    public function init() : void {
        register_rest_route( $this->namespace, '/bg/process/(?P<pid>[A-Za-z0-9-,.]+)',  [
            [
                'methods'             => WP_REST_Server::READABLE,
                'callback'            => [ $this, 'process' ],
                'permission_callback' => [ Common\Permissions::class, 'even_guest_user_can' ],
            ],
        ] );
    }

    public function process( WP_REST_Request $request ) : WP_REST_Response {
        try {
            return new WP_REST_Response( ProcessLog::dispatch( (string) $request->get_param( 'pid' ) ), 200 );
        }
        catch( ZencommunityException $e ) {
            return $e->as_rest_response();
        }
    }
}