<?php

namespace PloverCRM\Modules\Automations\Endpoints;

use PloverCRM\Core\Api\AbstractEndpoint;
use PloverCRM\Modules\Automations\Services\InternalEmailPreviewService;
use WP_REST_Request;

class AutomationsEmailPreviewEndpoint extends AbstractEndpoint
{
    public function getRoute(): string
    {
        return '/automations/email-preview';
    }

    public function getMethod()
    {
        return 'POST';
    }

    public function handle(WP_REST_Request $request)
    {
        $payload = $request->get_json_params();

        if (!is_array($payload)) {
            $payload = $request->get_params();
        }

        $preview = (new InternalEmailPreviewService($this->container))->preview($payload);

        return $this->success($preview);
    }
}
