<?php
/**
 * filters.php
 *
 * @author     POP <info@popapi.io>
 * @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.
}

$jobs = new \WooPoPToSdIPec\Jobs();

//
//  array(
//      'filter'        => '',
//      'callback'      => '',
//      'priority'      => '',
//      'accepted_args' => '',
//  )
//
return apply_filters('woopopsdi-filters', array(
    'inc' => array(
        'action' => array(
            /**
             * Twice daily Cron Jobs Upload
             */
            array(
                'filter'   => 'init',
                'callback' => 'WooPoPToSdIPec\\Jobs::twiceDailyUploadInvoice',
                'priority' => 10,
            ),

            /**
             * Cron Jobs Upload
             */
            array(
                'filter'        => array(
                    'WooPoPToSdIPecJobsScheduledUpload',
                ),
                'callback'      => function ($id, $provider) {
                    \WooPoPToSdIPec\Jobs::uploadInvoice($id, $provider);
                },
                'priority'      => 10,
                'accepted_args' => 2,
            ),
        ),
        'filter' => array(
            array(
                'filter'   => 'wc_el_inv-diagnostic_logs',
                'callback' => function ($logs) {
                    $config                 = \WooPoPToSdIPec\Functions\getLogConfig();
                    $logs[$config['key']] = $config;

                    return $logs;
                },
                'priority' => 10,
            ),

            /**
             * WFC Options
             */
            array(
                'filter'   => 'allowed_options',
                'callback' => function ($allowedOptions) {
                    $data         = ! empty($_POST['wc_el_inv_options']) ? $_POST['wc_el_inv_options'] : array();
                    $addonOptions = \WooPoPToSdIPec\Functions\filterInput($_POST, 'wsdipec_options',
                        FILTER_SANITIZE_SPECIAL_CHARS);

                    if (isset($addonOptions) && '1' === $addonOptions) {

                        // Active Cron Job (for status)
                        if (! empty($data) &&
                            isset($data['wc_el_inv-settings-wsdipec_scheduled_order_status']) &&
                            '' !== $data['wc_el_inv-settings-wsdipec_scheduled_order_status']
                        ) {
                            update_option('wsdipec_scheduled_order_status',
                                $data['wc_el_inv-settings-wsdipec_scheduled_order_status']);
                        } else {
                            // Default "completed"
                            update_option('wsdipec_scheduled_order_status', 'completed');
                        }

                        // Active Cron Job (single event)
                        if (! empty($data) &&
                            isset($data['wc_el_inv-settings-wsdipec_scheduled_events']) &&
                            'on' === $data['wc_el_inv-settings-wsdipec_scheduled_events']
                        ) {
                            if (get_option('wsdipec_scheduled_events') !== 'on') {
                                update_option('wsdipec_scheduled_events', 'on');
                            }
                        } else {
                            update_option('wsdipec_scheduled_events', 'off');
                        }

                        // Active Cron Job (twice daily)
                        if (! empty($data) &&
                            isset($data['wc_el_inv-settings-wsdipec_scheduled_twicedaily_events']) &&
                            'on' === $data['wc_el_inv-settings-wsdipec_scheduled_twicedaily_events']
                        ) {
                            if (get_option('wsdipec_scheduled_twicedaily_events') !== 'on') {
                                update_option('wsdipec_scheduled_twicedaily_events', 'on');
                            }
                        } else {
                            update_option('wsdipec_scheduled_twicedaily_events', 'off');
                        }

                        // Event scheduled time
                        if (! empty($data) &&
                            isset($data['wc_el_inv-settings-wsdipec_scheduled_events_time']) &&
                            '' !== $data['wc_el_inv-settings-wsdipec_scheduled_events_time']
                        ) {
                            update_option('wsdipec_scheduled_events_time',
                                $data['wc_el_inv-settings-wsdipec_scheduled_events_time']);
                        } else {
                            update_option('wsdipec_scheduled_events_time', '');
                        }

                        // Send an email for failure to create document
                        if (! empty($data) &&
                            isset($data['wc_el_inv-settings-wsdipec_error_sending_invoice']) &&
                            'on' === $data['wc_el_inv-settings-wsdipec_error_sending_invoice']
                        ) {
                            if (get_option('wsdipec_error_sending_invoice') !== 'on') {
                                update_option('wsdipec_error_sending_invoice', 'on');
                            }
                        } else {
                            update_option('wsdipec_error_sending_invoice', 'off');
                        }

                        // SMTP Pec To
                        if (! empty($data) &&
                            isset($data['wc_el_inv-settings-wsdipec_smtp_pec_to'])
                        ) {
                            update_option('wsdipec_smtp_pec_to', trim($data['wc_el_inv-settings-wsdipec_smtp_pec_to']));
                        }

                        // Slack integration
                        if (! empty($data) &&
                            isset($data['wc_el_inv-settings-wsdipec_slack_integration']) &&
                            'on' === $data['wc_el_inv-settings-wsdipec_slack_integration']
                        ) {
                            if (get_option('wsdipec_slack_integration') !== 'on') {
                                update_option('wsdipec_slack_integration', 'on');
                                \WooPoPToSdIPec\Functions\slack(
                                    WP_FATT_SDI_NAME . __(' Slack integration is active!', WP_FATT_SDI_PEC_TEXTDOMAIN),
                                    false,
                                    ':white_check_mark:'
                                );
                            }
                        } else {
                            update_option('wsdipec_slack_integration', 'off');
                        }
                        // Slack API token
                        if (! empty($data) &&
                            isset($data['wc_el_inv-settings-wsdipec_slack_integration_token']) &&
                            '' !== $data['wc_el_inv-settings-wsdipec_slack_integration_token']
                        ) {
                            update_option('wsdipec_slack_integration_token',
                                $data['wc_el_inv-settings-wsdipec_slack_integration_token']);
                        } else {
                            update_option('wsdipec_slack_integration_token', '');
                        }
                        // Slack Channel
                        if (! empty($data) &&
                            isset($data['wc_el_inv-settings-wsdipec_slack_integration_channel']) &&
                            '' !== $data['wc_el_inv-settings-wsdipec_slack_integration_channel']
                        ) {
                            update_option('wsdipec_slack_integration_channel',
                                $data['wc_el_inv-settings-wsdipec_slack_integration_channel']);
                        } else {
                            update_option('wsdipec_slack_integration_channel', '');
                        }
                    }

                    return $allowedOptions;
                },
                'priority' => PHP_INT_MAX,
            ),
        ),
    ),
));
