<?php
defined('ABSPATH') || die('Cheatin\' uh?');
require_once plugin_dir_path(__FILE__) . '/helpers/Tools.php';
/**
 * Set the ajax action and call for wordpress
 */
class PROD_Classes_ActionController extends PROD_Classes_FrontController
{

    /**
     * All Actions
     * @var array with all form and ajax actions 
     */
    var $actions = array();

    public function getActionsList()
    {
        return array(
            array(
                'name' => 'PROD_Controllers_Settings',
                'actions' => array(
                    'action' => array(
                         'save_initial_data',
                         'save_prod_fields',
                         'delete_feed',
                         'edit_feed',
                         'prod_generate_ai_suggestions',
                         'prod_check_ai_suggestions',
                         'prod_process_ai_chunks',
                         'prod_get_ai_preview_results',
                         'get_woo_products',
                         'get_woo_categories',
                         'prod_batch_optimization',
                         'prod_check_optimization_complete',
                         'prod_finish_optimization_feed',
                         'prod_save_api_key',
                         'prod_submit_onboarding',
                         'prod_save_toggle',
                         'prod_restore_originals'
                    ),
                ),
                'active' => '1',
            ),
            
            array(
                'name' => 'PROD_Controllers_Backup',
                'actions' => array(
                    'action' => array(
                        'prod_settings_backup',
                    ),
                ),
                'active' => '1',
            ),
        );
    }

    /**
     * The hookAjax is loaded as custom hook in hookController class
     *
     * @return void
     */
    public function hookInit()
    {
        /* Only if ajax */
        // if (PROD_Classes_Helpers_Tools::isAjax()) {
        //     $this->getActions();
        // }
    }

	/**
	 * The hookHead is loaded as admin hook in hookController class for script load
	 * Is needed for security check as nonce
	 *
	 * @return void
	 */
    public function hookHead() {
       
        echo '<script>
                  var sqQuery = {
                    "adminurl": "' . admin_url() . '",
                    "ajaxurl": "' . admin_url('admin-ajax.php') . '",
                    "adminposturl": "' . admin_url('post.php') . '",
                    "adminlisturl": "' . admin_url('edit.php') . '",
                    "nonce": "' . wp_create_nonce(_PROD_NONCE_ID_) . '"
                  }
              </script>';
       
    }

    public function hookFronthead() {
        if (prod_Classes_Helpers_Tools::isFrontAdmin()) {
            echo '<script>
                  var sqQuery = {
                    "adminurl": "' . admin_url() . '",
                    "ajaxurl": "' . admin_url('admin-ajax.php') . '",
                    "nonce": "' . wp_create_nonce(_PROD_NONCE_ID_) . '"
                  }
              </script>';

            //   echo '<script type="application/ld+json">
            //   var $schema={
            //       "@context": "' . admin_url() . '",
            //       "name": "local Boss",
               
            //   }
            //   </script>';
        }
    }


    /**
     * The hookSubmit is loaded when action si posted
     *
     * @return void
     */
    public function hookMenu()
    {
        /* Only if post */
        if (!PROD_Classes_Helpers_Tools::isAjax()) {
            $this->getActions();
        }
    }

    /**
     * Get all actions from config.json in core directory and add them in the WP
     */
    public function getActions()
    {

        if (!is_admin()) {
            return;
        }

        
 

        $this->actions = array();
        $cur_action = PROD_Classes_Helpers_Tools::getValue('action');
        $http_referer = PROD_Classes_Helpers_Tools::getValue('_wp_http_referer');
        $prod_nonce = PROD_Classes_Helpers_Tools::getValue('prod_nonce');

        //Let only the logged users to access the actions
        if ($cur_action <> '' && $prod_nonce <> '') {

            //load the actions list for each class
            $actions = $this->getActionsList();

            if(!empty($actions)) {
                foreach ($actions as $block) {
                    if (isset($block['active']) && $block['active'] == 1) {
                        /* if there is a single action */
                        if (isset($block['actions']['action']))
                            /* if there are more actions for the current block */
                        if (!is_array($block['actions']['action'])) {
                            /* add the action in the actions array */
                            if ($block['actions']['action'] == $cur_action) {
                                $this->actions[] = array('class' => $block['name']);
                            }
                        } else {
                            /* if there are more actions for the current block */
                            foreach ($block['actions']['action'] as $action) {
                                /* add the actions in the actions array */
                                if ($action == $cur_action) {
                                    $this->actions[] = array('class' => $block['name']);
                                }
                            }
                        }
                    }
                }
            }

            //If there is an action found in the config.js file
            if (!empty($this->actions)) {

                /* add the actions in WP */
                foreach ($this->actions as $actions) {
                    if (PROD_Classes_Helpers_Tools::isAjax() && !$http_referer) {
                        check_ajax_referer(_PROD_NONCE_ID_, 'prod_nonce');
                        add_action('wp_ajax_' . $cur_action, array(PROD_Classes_ObjController::getClass($actions['class']), 'action'));
                    } else {
                        check_admin_referer($cur_action, 'prod_nonce');

                        PROD_Classes_ObjController::getClass($actions['class'])->action();
                    }
                }
            }
        }

    }

}

add_action('wp_ajax_get_ai_optimization_progress', function() {
    $progress = get_option('ai_optimization_progress', [
        'optimized' => 0,
        'total' => 0,
        'percent' => 0,
    ]);

    wp_send_json_success($progress);
});