<?php
namespace GriffinForms\Includes\Schedulers;

use GriffinForms\Traits\Singleton;

class LogCleanup
{
    use Singleton;

    /**
     * Register cron hook and schedule if not already scheduled.
     */
    public function register()
    {
        // Hook the runner
        add_action('griffinforms_log_cleanup', [$this, 'run']);

        // Schedule daily if not already
        if (!wp_next_scheduled('griffinforms_log_cleanup')) {
            wp_schedule_event(time() + HOUR_IN_SECONDS, 'daily', 'griffinforms_log_cleanup');
        }
    }

    /**
     * Cron runner: prunes logs according to retention setting.
     */
    public function run()
    {
        // Read retention; defer to Log class for actual pruning
        if (!class_exists('GriffinForms\\Log')) {
            return;
        }
        
        $log = \GriffinForms\Log::getInstance();
        if (method_exists($log, 'pruneByRetention')) {
            $log->pruneByRetention();
        }
    }
}