<?php
// Security: Prevent direct access to plugin files.
defined( 'ABSPATH' ) || exit;

// Load premium methods trait if present (excluded from free ZIP).
if ( file_exists( __DIR__ . '/class-iotc-cron-premium__premium_only.php' ) ) {
    require_once __DIR__ . '/class-iotc-cron-premium__premium_only.php';
}

// Fallback empty trait for free version.
if ( ! trait_exists( 'IOTC_Cron_Premium' ) ) {
    trait IOTC_Cron_Premium {}
}

class IOTC_Cron {

    use IOTC_Cron_Premium;

    public function __construct() {
        add_filter( 'cron_schedules', array( $this, 'add_cron_intervals' ) );
        if ( method_exists( $this, 'run_monthly_scan__premium_only' ) ) {
            add_action( 'iotc_db_orphan_tables_monthly_scan', array( $this, 'run_monthly_scan__premium_only' ) );
            add_action( 'iotc_automatic_cleanup_job', array( $this, 'run_automatic_cleanup__premium_only' ) );
        }
    }

    /**
     * Activation hook: schedule the monthly cron job and the cleanup job if enabled.
     */
    public static function activate() {
        self::reschedule_report_cron();
        self::reschedule_optimization_cron();
    }

    /**
     * Deactivation hook: remove the cron jobs
     */
    public static function deactivate() {
        wp_clear_scheduled_hook( 'iotc_db_orphan_tables_monthly_scan' );
        wp_clear_scheduled_hook( 'iotc_automatic_cleanup_job' );
    }

    /**
     * Add custom intervals to cron schedules
     */
    public function add_cron_intervals( $schedules ) {
        if ( ! isset( $schedules['weekly'] ) ) {
            $schedules['weekly'] = array(
                'interval' => 7 * DAY_IN_SECONDS,
                'display'  => __( 'Once Weekly', 'intouch-orphan-table-cleaner' )
            );
        }
        if ( ! isset( $schedules['monthly'] ) ) {
            $schedules['monthly'] = array(
                'interval' => 30 * DAY_IN_SECONDS,
                'display'  => __( 'Once Monthly', 'intouch-orphan-table-cleaner' )
            );
        }

        $cron_settings = get_option( 'iotc_cron_settings', array() );
        if ( isset( $cron_settings['frequency'] ) && $cron_settings['frequency'] === 'custom' && ! empty( $cron_settings['days'] ) ) {
            $days = absint( $cron_settings['days'] );
            if ( $days < 1 ) $days = 1;

            /* translators: %d: number of days */
            $display_label = sprintf( __( 'Every %d Days', 'intouch-orphan-table-cleaner' ), $days );

            $schedules['iotc_custom'] = array(
                'interval' => $days * DAY_IN_SECONDS,
                'display'  => $display_label
            );
        }

        if ( isset( $cron_settings['report_frequency'] ) && $cron_settings['report_frequency'] === 'custom' && ! empty( $cron_settings['report_days'] ) ) {
            $report_days = absint( $cron_settings['report_days'] );
            if ( $report_days < 1 ) $report_days = 1;

            /* translators: %d: number of days */
            $report_display_label = sprintf( __( 'Every %d Days', 'intouch-orphan-table-cleaner' ), $report_days );

            $schedules['iotc_report_custom'] = array(
                'interval' => $report_days * DAY_IN_SECONDS,
                'display'  => $report_display_label
            );
        }

        return $schedules;
    }

    /**
     * Reschedule the orphan tables report email cron job based on settings
     */
    public static function reschedule_report_cron() {
        wp_clear_scheduled_hook( 'iotc_db_orphan_tables_monthly_scan' );

        $cron_settings = get_option( 'iotc_cron_settings', array(
            'report_enabled'   => 'yes',
            'report_frequency' => 'monthly',
            'report_days'      => 30
        ) );

        $report_enabled = isset( $cron_settings['report_enabled'] ) ? $cron_settings['report_enabled'] : 'yes';

        if ( $report_enabled === 'yes' ) {
            $frequency = isset( $cron_settings['report_frequency'] ) ? $cron_settings['report_frequency'] : 'monthly';
            if ( $frequency === 'custom' ) {
                $frequency = 'iotc_report_custom';
            }
            wp_schedule_event( time() + 60, $frequency, 'iotc_db_orphan_tables_monthly_scan' );
        }
    }

    /**
     * Reschedule the optimization cron job based on settings
     */
    public static function reschedule_optimization_cron() {
        wp_clear_scheduled_hook( 'iotc_automatic_cleanup_job' );

        $cron_settings = get_option( 'iotc_cron_settings', array(
            'enabled'   => 'no',
            'frequency' => 'monthly',
            'days'      => 7
        ) );

        if ( $cron_settings['enabled'] === 'yes' ) {
            $frequency = $cron_settings['frequency'];
            if ( $frequency === 'custom' ) {
                $frequency = 'iotc_custom';
            }
            wp_schedule_event( time() + 60, $frequency, 'iotc_automatic_cleanup_job' );
        }
    }
}
