<?php
/**
 * User Events Cleanup Scheduler.
 * Schedules and runs daily cleanup of old user event records.
 *
 * @package PRECO
 * @since 1.0.0
 */

namespace PRECO\Includes\Admin\EventsCleanup;

use PRECO\Includes\Frontend\UserEvents\UserEventsTable;

defined( 'ABSPATH' ) || exit;

/**
 * User Events Cleanup Scheduler class.
 */
class UserEventsCleanupScheduler {

	/**
	 * Constructor.
	 * Registers activation and cleanup hooks.
	 *
	 * @since v.1.0.0
	 */
	public function __construct() {
		// Register cleanup cron action.
		add_action( 'preco_cleanup_user_events', array( $this, 'cleanup_old_events' ) );

		// Ensure cron job is scheduled.
		add_action( 'init', array( $this, 'schedule_cleanup_cron' ) );
	}

	/**
	 * Schedule cleanup cron job if not already scheduled.
	 *
	 * @since v.1.0.0
	 * @return void
	 */
	public function schedule_cleanup_cron() {
		if ( ! wp_next_scheduled( 'preco_cleanup_user_events' ) ) {
			wp_schedule_event( time(), 'daily', 'preco_cleanup_user_events' );
		}
	}

	/**
	 * Cleanup old user events.
	 * Runs daily via WP-Cron.
	 *
	 * @since v.1.0.0
	 * @return void
	 */
	public function cleanup_old_events() {
		UserEventsTable::cleanup_old_records();
	}
}
