<?php
/**
 * filtersAdmin.php
 *
 * @since      1.0.0
 * @author     POP <info@popapi.io>

 * @license    http://opensource.org/licenses/gpl-2.0.php GPL v2
 *
 * Copyright 2025 POP <info@popapi.io>
 *
 * 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 \WooPoPToSdIPec\Resources();

//
//  array(
//      'filter'        => '',
//      'callback'      => '',
//      'priority'      => '',
//      'accepted_args' => '',
//  )
//
return apply_filters('woopopsdi-filters_admin', array(
    'admin' => array(
        'action' => array(
            /**
             * Fatture SdI PEC Actions Login
             */
            array(
                'filter'   => 'wc_el_inv-before_closing_settings_form',
                'callback' => 'WooPoPToSdIPec\\Filters::saveAction',
                'priority' => 10,
            ),

            /**
             * 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,
            ),
            array(
                'filter'   => 'admin_enqueue_scripts',
                'callback' => array($resources, 'localizeScript'),
                'priority' => 40,
            ),

            /**
             * Ajax Modal dialog container
             */
            array(
                'filter'   => 'admin_footer',
                'callback' => function () {
                    echo "<!-- modal -->\n<div class='wsdipec_ajax_modal close'><div class='modal_content'><div class='modal_card close'></div><span class='close'></span></div></div>\n<!-- /modal -->";
                },
                'priority' => PHP_INT_MAX,
            ),

            /**
             * Cron Jobs on order status change
             */
            array(
                'filter'        => 'woocommerce_order_status_changed',
                'callback'      => 'WooPoPToSdIPec\\Jobs::changeStatus',
                'priority'      => 10,
                'accepted_args' => 3,
            ),

            /**
             * Fatture SdI PEC Actions in column billing_address
             */
            array(
                'filter'   => 'wc_el_inv-after_billing_address_wc-orders_custom_column',
                'callback' => 'WooPoPToSdIPec\\Filters::actionsInOrder',
                'priority' => 30,
            ),

            /**
             * Expired smtp pass notice
             */
            array(
                'filter'   => 'wc_el_inv-plugin_header_notice',
                'callback' => function () {
                    $currentSmtpExpiredPassDate = get_option('wsdipec_smtp_pass_expiration', null);
                    $smtpConnected  = get_option('wsdipec_smtp_connected', null);
                    if ($smtpConnected && $currentSmtpExpiredPassDate) {
                        $currentDate = date('Y-m-d');
                        $dateDiff    = (strtotime($currentSmtpExpiredPassDate) - strtotime($currentDate)) / (60 * 60 * 24);

                        if ($dateDiff <= 15 && $dateDiff > 0) {
                            echo sprintf('<div class="pop-notice notice-warning"><p><span class="dashicons dashicons-warning"></span> %s %s. %s</p></div>',
                                esc_html__('Warning: Your password will expire on', WP_FATT_SDI_PEC_TEXTDOMAIN),
                                esc_attr(date('d/m/Y', strtotime($currentSmtpExpiredPassDate))),
                                esc_html__('Please consider updating it soon.', WP_FATT_SDI_PEC_TEXTDOMAIN)
                            );
                        } elseif ($dateDiff <= 0) {
                            echo sprintf('<div class="pop-notice notice-error"><p><span class="dashicons dashicons-no"></span> %s %s. %s</p></div>',
                                esc_html__('Your password expired on', WP_FATT_SDI_PEC_TEXTDOMAIN),
                                esc_attr(date('d/m/Y', strtotime($currentSmtpExpiredPassDate))),
                                esc_html__('Please update it immediately.', WP_FATT_SDI_PEC_TEXTDOMAIN)
                            );
                        }
                    }
                },
                'priority' => 30,
            ),

            /**
             * Expired smtp pass emails notice
             */
            array(
                'filter'   => 'woopopsdi-saved_connection_ok',
                'callback' => 'WooPoPToSdIPec\\Functions\\schedulePasswordExpiryNotifications',
                'priority' => 10,
            ),
            array(
                'filter'   => 'popSdiPec_send_password_expiry_email_7_days',
                'callback' => 'WooPoPToSdIPec\\Functions\\sendEmailNotification',
                'priority' => 10,
            ),
            array(
                'filter'   => 'popSdiPec_send_password_expiry_email_1_day',
                'callback' => 'WooPoPToSdIPec\\Functions\\sendEmailNotification',
                'priority' => 10,
            ),
        ),
        'filter' => array(
            /**
             * Fatture SdI PEC Actions Title
             */
            array(
                'filter'        => 'wc_el_inv-xml_order_list_table_actions',
                'callback'      => function ($output, $id, $item) {
                    $choiceType = isset($item['choice_type']) && '' !== $item['choice_type'] ? $item['choice_type'] : 'invoice';
                    if ('receipt' === $choiceType) {
                        return $output . sprintf('<p class="wsdipec_error">%s</p>',
                                __('SdI does not manage receipts, unable to send the xml', WP_FATT_SDI_PEC_TEXTDOMAIN));
                    }

                    $text = __('Fatture SdI PEC', WP_FATT_SDI_PEC_TEXTDOMAIN);

                    return $output . "<h4>{$text}:</h4>";
                },
                'priority'      => 5,
                'accepted_args' => 3,
            ),

            /**
             * Fatture SdI PEC Actions Import
             */
            array(
                'filter'        => 'wc_el_inv-xml_order_list_table_actions',
                'callback'      => 'WooPoPToSdIPec\\Filters::importAction',
                'priority'      => 20,
                'accepted_args' => 3,
            ),

            /**
             * Fatture SdI PEC Actions in order
             */
            array(
                'filter'   => 'woocommerce_admin_order_data_after_order_details',
                'callback' => 'WooPoPToSdIPec\\Filters::actionsInOrder',
                'priority' => 30,
            ),

            /**
             * Fatture SdI PEC Actions in refund order
             */
            array(
                'filter'   => 'wc_el_inv-after_refund_order_item_td',
                'callback' => 'WooPoPToSdIPec\\Filters::refundActions',
                'priority' => 30,
            ),
        ),
    ),
));
