<?php

namespace GriffinForms\Admin\App\Html\Modals\FormNotifications;

trait Requests
{
    public function registerRestRoutes(): void
    {
        register_rest_route('griffinforms/v1', '/forms/(?P<form_id>\d+)/notifications', [
            'methods'             => \WP_REST_Server::CREATABLE,
            'callback'            => [$this, 'saveFormNotifications'],
            'permission_callback' => [$this, 'canSaveFormNotifications'],
            'args'                => [
                'form_id'   => [
                    'type'              => 'integer',
                    'required'          => true,
                    'sanitize_callback' => 'absint',
                ],
                'item_data' => [
                    'type'     => 'string',
                    'required' => true,
                ],
            ],
        ]);
    }

    public function canSaveFormNotifications(): bool
    {
        return current_user_can('manage_options');
    }
}
