<?php

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


trait ScheduleTrait {
    // Fire if the notification is enabled
    public static function istkr_add_wp_schedule_event($event, $options)
    {
        global $WP_CRON_SCHEDULE_INTERVALS;

        $action = $options['action'];
        $recurrence = $options['notifications_email_recurrence'];

        $time = new DateTime();
        $time->add($WP_CRON_SCHEDULE_INTERVALS[$recurrence]);

        if (!wp_next_scheduled($action)) {
            wp_schedule_event($time->getTimestamp(), $recurrence, $action, array($event));
        }

        update_option($event, $options);
    }

    // Fire if the notification is disabled or on plugin deactivation (uninstalling)
    public static function istkr_delete_wp_schedule_event($event)
    {
        $options = get_option($event);

        if (!$options) {
            error_log('Options not found for event ' . $event);
            echo json_encode([
                'success' => false,
                'error' => __('Internal error', 'istkr')
            ]);
            wp_die();
        }

        if (!array_key_exists('action', $options)) {
            error_log('Action not found in options for event ' . $event);
            echo json_encode([
                'success' => false,
                'error' => __('Internal error', 'istkr')
            ]);
            wp_die();
        }

        if (wp_next_scheduled($options['action'], array($event))) {
            wp_clear_scheduled_hook($options['action'], array($event));
        }

        delete_option($event);
    }

    public static function istkr_change_notifications_status()
    {
        try {
            verify_nonce($_POST);

            $email = isset($_POST['email']) ? sanitize_email($_POST['email']) : null;
            $status = isset($_POST['status']) ? sanitize_text_field($_POST['status']) : null;
            $send_test_email = isset($_POST['send_test_email']) ? (bool) sanitize_text_field($_POST['send_test_email']) : null;
            $recurrence = isset($_POST['recurrence']) ? sanitize_text_field($_POST['recurrence']) : null;

            if (!$email) {
                echo json_encode([
                    'success' => false,
                    'error'   => __('Email was not passed', 'istkr'),
                ]);
                wp_die();
            }

            if (!$status) {
                echo json_encode([
                    'success' => false,
                    'error'   => __('Notification status was not passed', 'istkr'),
                ]);
                wp_die();
            }

            $notificator = new ISTKR_Notificator(new self());

            // Validate field recurrence only if notifications are turn on
            if ($status == 'enable' && (!isset($recurrence) || !array_key_exists($recurrence, $notificator->email_recurrences))) {
                echo json_encode([
                    'success' => false,
                    'error' => __('Invalid value received', 'istkr')
                ]);
                wp_die();
            }
            
            $event = $notificator->istkr_build_unique_event_name();

            $options = [
                'notifications_email' => $email,
                'notifications_email_recurrence' => $recurrence,
                'action' => $notificator->action,
            ];

            switch ($status) {
                case 'enable':
                    if ($send_test_email === true) {
                        $notificator->istkr_send_test_email($options);
                    }
                    self::istkr_add_wp_schedule_event($event, $options);
                    break;
                case 'disable':
                    self::istkr_delete_wp_schedule_event($event);
                    break;
            }
            echo  json_encode([
                'success' => true,
            ]);
            wp_die();
        } catch (Exception $e) {
            echo json_encode([
                'success' => false,
                'error'   => $e->getMessage(),
            ]);
            wp_die();
        }
    }
}
