<?php
/**
 * filtersAdmin.php
 *
 * @since      1.0.0
 * @author     alfiopiccione <alfio.piccione@gmail.com>

 * @license    http://opensource.org/licenses/gpl-2.0.php GPL v2
 *
 * Copyright (C) 2025 alfiopiccione <alfio.piccione@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

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

$resources = new \WooPoPToFattureInCloudStock\Resources();
$wcTabs    = new \WooPoPToFattureInCloudStock\WooCommerce\Tabs();

//
//  array(
//      'filter'        => '',
//      'callback'      => '',
//      'priority'      => '',
//      'accepted_args' => '',
//  )
//
return apply_filters('wfc_ms-filters_admin', array(
    'admin' => array(
        'action' => array(
            /**
             * Add get products action
             */
            array(
                'filter'   => 'wfc-login_action_extra_section',
                'callback' => 'WooPoPToFattureInCloudStock\\Filters::getProductsAction',
                'priority' => 20,
            ),

            /**
             * Admin enqueue scripts
             */
            array(
                'filter'   => 'admin_enqueue_scripts',
                'callback' => array($resources, 'enqueue'),
                'priority' => 20,
            ),
            array(
                'filter'   => 'admin_enqueue_scripts',
                'callback' => array($resources, 'register'),
                'priority' => 30,
            ),

            /**
             * Add product panel data @since 1.0.0
             */
            array(
                'filter'   => 'woocommerce_product_options_stock',
                'callback' => function () use ($wcTabs) {
                    global $product_object;
                    if ($product_object instanceof WC_Product) {
                        if ('simple' === $product_object->get_type()) {
                            $value = get_post_meta($product_object->get_id(), '_manage_product_stock', true);
                            $wcTabs->wooCommerceProductDataPanel(null, $value);
                        }
                    }
                },
                'priority' => 30,
            ),

            /**
             * Add product variation meta @since 1.0.0
             * Save product variation meta @since 1.0.0
             */
            array(
                'filter'        => 'woocommerce_variation_options_inventory',
                'callback'      => function ($loop, $variation_data, $variation) use ($wcTabs) {
                    $value = null;
                    if ($variation instanceof \WP_Post) {
                        $value = get_post_meta($variation->ID, '_manage_product_stock', true);
                    }
                    $wcTabs->wooCommerceProductDataPanel($loop, $value);
                },
                'priority'      => 10,
                'accepted_args' => 3,
            ),
            array(
                'filter'        => 'woocommerce_save_product_variation',
                'callback'      => 'WooPoPToFattureInCloudStock\\WooCommerce\\Tabs::storeVariationProductData',
                'priority'      => 10,
                'accepted_args' => 2,
            ),

            /**
             * Store product meta data @since 1.0.0
             */
            array(
                'filter'   => 'woocommerce_process_product_meta',
                'callback' => array($wcTabs, 'storeProductData'),
                'priority' => 20,
                'accepted_args' => 2,
            ),

            /**
             * Restock refunded item @since 1.0.0
             *
             * Refunded - Sync Fic stock whit WC stock
             */
            array(
                'filter'   => 'woocommerce_restock_refunded_item',
                'callback' => function ($productID, $oldStock, $newStock, $order, $product) {
                    $productFicID = get_post_meta($productID, '_manage_product_stock', true) ?: null;
                    if (true === apply_filters('wfc_ms-restock_refunded_item', true) &&
                        $productFicID &&
                        $product instanceof WC_Product
                    ) {
                        $stockStatus = $product->get_stock_status();
                        $args        = array(
                            'data' => array(
                                'id'            => $productFicID,
                                'in_stock'      => apply_filters('wfc_ms-restock_refunded_fic_product_in_stock_param', true, $stockStatus),
                                'stock_initial' => apply_filters('wfc_ms-restock_refunded_fic_product_item_new_stock', $newStock, $product->get_id()),
                            ),
                        );

                        // Sync Fic stock whit WC stock
                        $updateFicProduct = WooPoPToFattureInCloudStock\Functions\fattureInCloudModifyProduct(
                            $productFicID,
                            $args,
                            'get'
                        );

                        if ($updateFicProduct->ApiException && 200 !== $updateFicProduct->code) {
                            // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                            throw new \ErrorException(
                                "ApiException > fattureInCloudModifyProduct: " . esc_html($updateFicProduct->message),
                                esc_attr($updateFicProduct->code)
                            );
                        }
                    }
                },
                'priority' => 20,
            ),
        ),
        'filter' => array(),
    ),
));
