<?php
/**
 * Scheduled task: update expired rewards.
 *
 * @package Awesomemotive\Rewardswp\Tasks
 * @since 1.4.0
 */

namespace Awesomemotive\Rewardswp\Tasks;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Awesomemotive\Rewardswp\Interfaces\ScheduledTaskInterface;
use Awesomemotive\Rewardswp\Services\TaskSchedulerService;

/**
 * Class UpdateExpiredRewardsTask
 *
 * Cancels rewards that have passed their expiration date.
 * Processes in batches of 50 with pagination follow-ups.
 *
 * @since 1.4.0
 */
class UpdateExpiredRewardsTask implements ScheduledTaskInterface {
	/**
	 * Task scheduler service instance.
	 *
	 * @since 1.4.0
	 *
	 * @var TaskSchedulerService
	 */
	private TaskSchedulerService $service;

	/**
	 * Constructor.
	 *
	 * @since 1.4.0
	 *
	 * @param TaskSchedulerService $service Task scheduler service instance.
	 */
	public function __construct( TaskSchedulerService $service ) {
		$this->service = $service;
	}

	/**
	 * {@inheritDoc}
	 */
	public function get_recurrence(): string {
		return 'hourly';
	}

	/**
	 * {@inheritDoc}
	 */
	public function register_hooks(): void {
		add_action( 'rewardswp_task_update_expired_rewards', [ $this, 'handle_page' ], 10, 1 );
	}

	/**
	 * {@inheritDoc}
	 */
	public function run(): void {
		$this->service->task_update_expired_rewards( 1 );
	}

	/**
	 * Handle a specific page of expired rewards.
	 *
	 * @since 1.4.0
	 *
	 * @param int $page Page number to process.
	 *
	 * @return void
	 */
	public function handle_page( int $page = 1 ): void {
		$this->service->task_update_expired_rewards( $page );
	}
}
