<?php

namespace Fab\Api;

use Fab\Interfaces\JIT_Interface;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Notice API for handling notice dismissal
 *
 * @package    Fab
 * @subpackage Fab/Api
 */
class Notice extends Api implements JIT_Interface {

    /**
     * Registers REST API routes for notice dismissal.
     *
     * @return void
     */
    public function register_routes() {
        // Action Notice.
        \register_rest_route(
            'fab/v1',
            '/notice/action',
            array(
                array(
                    'methods'             => \WP_REST_Server::CREATABLE,
                    'permission_callback' => function () {
                        return defined( 'FAB_REST_API_USER_IS_ADMIN' ) && FAB_REST_API_USER_IS_ADMIN;
                    },
                    'callback'            => array( $this, 'action_notice' ),
                ),
            )
        );

        // Dismiss notice.
        \register_rest_route(
            'fab/v1',
            '/notice/dismiss',
            array(
                array(
                    'methods'             => \WP_REST_Server::CREATABLE,
                    'permission_callback' => function () {
                        return defined( 'FAB_REST_API_USER_IS_ADMIN' ) && FAB_REST_API_USER_IS_ADMIN;
                    },
                    'callback'            => array( $this, 'dismiss_notice' ),
                ),
            )
        );
    }

    /**
     * Handles the REST API request for action notices.
     *
     * @param WP_REST_Request $request Full details about the request.
     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
     */
    public function action_notice( $request ) {
        $nonce = $request->get_header( 'X-WP-Nonce' );
        if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
            return new \WP_Error(
                'rest_cookie_invalid_nonce',
                __( 'Nonce verification failed', 'floating-awesome-button' ),
                array( 'status' => 403 )
            );
        }

        // Action the notice.
        do_action( 'fab_notice_action', $request );

        return new \WP_REST_Response(
            array(
                'success'   => true,
                'notice_id' => $request->get_param( 'notice_id' ),
                'action'    => $request->get_param( 'action' ),
                'message'   => __( 'Notice action performed successfully', 'floating-awesome-button' ),
            ),
            200
        );
    }

    /**
     * Handles the REST API request for dismissing notices.
     *
     * @param WP_REST_Request $request Full details about the request.
     * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
     */
    public function dismiss_notice( $request ) {
        $nonce = $request->get_header( 'X-WP-Nonce' );
        if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
            return new \WP_Error(
                'rest_cookie_invalid_nonce',
                __( 'Nonce verification failed', 'floating-awesome-button' ),
                array( 'status' => 403 )
            );
        }

        $notice_id = $request->get_param( 'notice_id' );
        if ( ! $notice_id ) {
            return new \WP_Error(
                'invalid_notice_id',
                __( 'Invalid notice ID', 'floating-awesome-button' ),
                array( 'status' => 400 )
            );
        }

        update_option( sprintf( 'fab_admin_notice_%s', $notice_id ), true );

        return new \WP_REST_Response(
            array(
                'success'   => true,
                'notice_id' => $notice_id,
                'message'   => __( 'Notice dismissed successfully', 'floating-awesome-button' ),
            ),
            200
        );
    }

    /*
    |--------------------------------------------------------------------------
    | Fulfill implemented interface contracts
    |--------------------------------------------------------------------------
     */

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        // Register routes.
        add_action( 'rest_api_init', array( __CLASS__, 'jit_register_routes' ) );
    }
}
