<?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 POP
 *
 * 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.
}

$orderProvider    = new \WooPoPCozmosLabsPMS\Providers\OrdersProvider();
$productsProvider = new \WooPoPCozmosLabsPMS\Providers\ProductsProvider();
$taxesProvider    = new \WooPoPCozmosLabsPMS\Providers\TaxesProvider();

//
//  array(
//      'filter'        => ''
//      'callback'      => ''
//      'priority'      => ''
//      'accepted_args' => ''
//  )
//
$filters = array(
    'admin' => array(
        'action' => array(

            // save POP order meta after the payment has been created
            array(
                'filter'        => 'pms_payment_insert',
                'callback'      => array($orderProvider, 'on_payment_insert'),
                'priority'      => 100,
                'accepted_args' => 2,
            ),

            // save some payment meta after the payment is updated (e.g. payment update from admin)
            array(
                'filter'        => 'pms_payment_update',
                'callback'      => array($orderProvider, 'on_payment_admin_edit'),
                'priority'      => 100,
                'accepted_args' => 3,
            ),

            // save the admin payment form
            // lower priority to save the form before showing the fields
            array(
                'filter'        => 'pms_payment_edit_form_field',
                'callback'      => array($orderProvider, 'pms_payment_save_form'),
                'priority'      => 9,
                'accepted_args' => 2,
            ),

            // adding fields to the admin payment edit form
            array(
                'filter'        => 'pms_payment_edit_form_field',
                'callback'      => array($orderProvider, 'payment_edit_form_field'),
                'priority'      => 99,
                'accepted_args' => 2,
            ),

            // adding fields to the admin payment edit form
            array(
                'filter'        => 'pms_payment_add_new_form_field',
                'callback'      => array($orderProvider, 'payment_edit_form_field'),
                'priority'      => 99,
                'accepted_args' => 0,
            ),

            // registering the products provider for PMS
            array(
                'filter'        => 'wc_el_inv-enqueue_orders_provider',
                'callback'      => array($orderProvider, 'enqueue'),
                'priority'      => 10,
                'accepted_args' => 1,
            ),

            // registering the products provider for PMS
            array(
                'filter'        => 'wc_el_inv-enqueue_products_provider',
                'callback'      => array($productsProvider, 'enqueue'),
                'priority'      => 10,
                'accepted_args' => 1,
            ),

            // registering the taxes provider for PMS
            array(
                'filter'        => 'wc_el_inv-enqueue_taxes_provider',
                'callback'      => array($taxesProvider, 'enqueue'),
                'priority'      => 10,
                'accepted_args' => 1,
            ),
        ),
        'filter' => array()
    ),
);

add_filter('wc_el_inv-filters_admin', function ($filtersAdmin) use ($filters) {

    $filtersAdmin['admin']['action'] = array_merge($filtersAdmin['admin']['action'], $filters['admin']['action']);
    $filtersAdmin['admin']['filter'] = array_merge($filtersAdmin['admin']['filter'], $filters['admin']['filter']);

    return $filtersAdmin;
}, 10, 1);
