<?php
/**
 * BookiFlex Notification Extension
 *
 * @package    BookiFlex
 * @subpackage Notification
 * @author     BookiFlex
 * @copyright  Copyright (c) 2025 BookiFlex
 * @license    GPL-2.0-or-later
 *
 * @link       https://bookiflex.com
 */

declare(strict_types=1);

namespace BookiFlex\Extensions\Reservation;

use BookiFlex\Core\Admin\Lifecycle\AbstractExtensionActivator;
use BookiFlex\Core\Contract\Queue\QueueInterface;
use Psr\Log\LoggerInterface;

defined('ABSPATH') || exit;

/**
 * Reservation extension activator
 *
 * Free extension for manage reservations.
 * Does NOT implement PremiumExtensionInterface - always enabled.
 */
class Activator extends AbstractExtensionActivator
{
	private QueueInterface $queue;
	private ?LoggerInterface $logger;

	public function __construct(QueueInterface $queue, ?LoggerInterface $logger = null)
	{
		$this->queue = $queue;
		$this->logger = $logger;
	}

	// =========================================================================
	// CODE-DEPENDENT METHODS
	// =========================================================================

	/**
	 * @inheritDoc
	 */
	public function uninstall(): void
	{
		$this->disableRuntimeResources();
		$this->logger?->debug('Reservation: Uninstalled');
	}

	// =========================================================================
	// LICENSE-DEPENDENT METHODS
	// =========================================================================

	/**
	 * @inheritDoc
	 *
	 * This is a FREE extension - always enabled regardless of license.
	 */
	public function enableRuntimeResources(): void
	{
		$this->logger?->debug('Reservation: Enabling runtime resources');

		// Cancel existing to ensure clean state
		$this->queue->cancelAll('bookiflex_check_overdue_reservations');

		// Schedule recurring check for overdue reservations (every 10 minutes)
		$this->queue->scheduleRecurring(
			'bookiflex_check_overdue_reservations',
			[],
			600 // 10 minutes
		);

		$this->logger?->debug('Reservation: Runtime resources enabled');
	}

	/**
	 * @inheritDoc
	 */
	public function disableRuntimeResources(): void
	{
		$this->logger?->debug('Reservation: Disabling runtime resources');

		$this->queue->cancelAll('bookiflex_check_overdue_reservations');

		$this->logger?->debug('Reservation: Runtime resources disabled');
	}
}