<?php

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

class MDSPBE_Rest_Api {

    function __construct() {
        add_action( 'rest_api_init', [ $this, 'rest_api_init' ] );
    }

    public function rest_api_init() {

        // Endpoint to search products
        register_rest_route('mdspbe/v1', '/get_ajax_options', array(
            'methods' => 'GET',
            'callback' => [ $this, 'get_ajax_options' ],
            'permission_callback' => function () {
                $nonce = isset( $_SERVER['HTTP_X_WP_NONCE'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_WP_NONCE'] ) ) : '';
                return is_user_logged_in() && wp_verify_nonce( $nonce, 'wp_rest' );
            },
            'args'                => array(
                'term' => array(
                    'required' => true,
                    'sanitize_callback' => 'sanitize_text_field',
                ),
                'attrId' => array(
                    'required' => true,
                    'sanitize_callback' => 'sanitize_text_field',
                ),
            ),
        ));

        // Endpoint to save all the local server settings
        register_rest_route('mdspbe/v1', '/update_create', array(
            'methods' => 'PUT',
            'callback' => [ $this, 'update_create' ],
            'permission_callback' => [ $this, 'permission' ],
            'args'                => array(
                'data' => array(
                    'required' => true,
                ),
            ),
        ));

        // Endpoint to add a new option to select html elements (like categories)
        register_rest_route('mdspbe/v1', '/add_option', array(
            'methods' => 'PUT',
            'callback' => [ $this, 'add_option' ],
            'permission_callback' => [ $this, 'permission' ],
            'args'                => array(
                'data' => array(
                    'required' => true,
                ),
            ),
        ));

        // Endpoint to remove options from select html elements (like categories)
        register_rest_route('mdspbe/v1', '/remove_options', array(
            'methods' => 'PUT',
            'callback' => [ $this, 'remove_options' ],
            'permission_callback' => [ $this, 'permission' ],
            'args'                => array(
                'data' => array(
                    'required' => true,
                ),
            ),
        ));

        // Endpoint to toggle fields
        register_rest_route('mdspbe/v1', '/toggle_fields', array(
            'methods' => 'PUT',
            'callback' => [ $this, 'toggle_fields' ],
            'permission_callback' => [ $this, 'permission' ],
            'args'                => array(
                'data' => array(
                    'required' => true,
                ),
            ),
        ));

        // Endpoint to get print tag data
        register_rest_route('mdspbe/v1', '/get_print_tag_data', array(
            'methods' => 'POST',
            'callback' => [ $this, 'get_print_tag_data' ],
            'permission_callback' => [ $this, 'permission' ],
            'args'                => array(
                'data' => array(
                    'required' => true,
                ),
            ),
        ));
    }

    public function get_ajax_options( WP_REST_Request $request ) {
        
        $term = $request->get_param( 'term' );
        $attr_id = $request->get_param( 'attrId' );

        $response = mdspbe_get_ajax_options( $term, $attr_id );

        return $response;
    }

    public function update_create( WP_REST_Request $request ) {
        
        $data = $request->get_param( 'data' );
        $response = mdspbe_update_create_products( $data );

        return rest_ensure_response( $response );
    }

    public function add_option( WP_REST_Request $request ) {
        
        $data = $request->get_param( 'data' );

        $edit_opts = mdspbe_get_attr_edit_options();
        $add_option_callback = $edit_opts[$data['id']]['add']['callback'];
        if ( function_exists( $add_option_callback ) ) {

            if ( isset( $edit_opts[$data['id']]['callbacks_param'] ) ) {
                $response = $add_option_callback( $data, $edit_opts[$data['id']]['callbacks_param'] );
            } else {
                $response = $add_option_callback( $data );
            }

            return rest_ensure_response( $response );
        }

        return new WP_Error( 'invalid_callback', esc_html__( 'Invalid callback',  'mds-product-bulk-editor-for-woocommerce' ) );

    }

    public function remove_options( WP_REST_Request $request ) {
        
        $data = $request->get_param( 'data' );

        $edit_opts = mdspbe_get_attr_edit_options();
        $remove_options_callback = $edit_opts[$data['id']]['remove']['callback'];
        if ( function_exists( $remove_options_callback ) ) {

            if ( isset( $edit_opts[$data['id']]['callbacks_param'] ) ) {
                $response = $remove_options_callback( $data, $edit_opts[$data['id']]['callbacks_param'] );
            } else {
                $response = $remove_options_callback( $data );
            }

            return rest_ensure_response( $response );
        }

        return new WP_Error( 'invalid_callback', esc_html__( 'Invalid callback',  'mds-product-bulk-editor-for-woocommerce' ) );

    }

    public function toggle_fields( WP_REST_Request $request ) {
        
        $data = $request->get_param( 'data' );

        $active_attributes = array_map( 'sanitize_text_field', array_keys( $data ) );

        $attributes = mdspbe_get_products_attributes();
        $validated_attributes = [];
        foreach ( $active_attributes as $attribute_id ) {
            if ( isset( $attributes[$attribute_id] ) ) {
                $validated_attributes[] = $attribute_id;
            }
        }

        update_user_meta( get_current_user_id(), '_mdspbe_active_attributes', $validated_attributes );

        return rest_ensure_response( true );

    }

    public function get_print_tag_data( WP_REST_Request $request ) {
        
        $data = $request->get_param( 'data' );
        $products_data = mdspbe_get_products_data_for_print_tags( $data );

        return rest_ensure_response( $products_data );

    }

    public function permission() {
        if ( current_user_can( 'edit_products' ) ) {
            return true;
        }

        return false;
    }

}

new MDSPBE_Rest_Api();




