<?php

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound -- APD is the established namespace shared with the Pro add-on and retained for backward compatibility.
namespace APD\Api\Controllers;

use APD\Api\Middleware\PermissionGuard;
use APD\Api\RestNamespace;
use APD\Domain\Pricing\PricingEngine;
use WP_REST_Request;
use WP_REST_Response;

defined('ABSPATH') || exit;

class PricingController
{
    public function registerRoutes() : void
    {
        register_rest_route(RestNamespace::NAME, '/pricing/calculate', [
            'methods' => 'POST',
            'permission_callback' => [PermissionGuard::class, 'storefrontPricing'],
            'callback' => [$this, 'calculate'],
        ]);
    }

    public function calculate(WP_REST_Request $request) : WP_REST_Response
    {
        $context = $request->get_json_params();

        return new WP_REST_Response((new PricingEngine())->calculate($context));
    }
}
