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

declare(strict_types=1);

namespace BookiFlex\Extensions\Cart;

use BookiFlex\Core\Admin\Lifecycle\AbstractExtensionActivator;
use BookiFlex\Core\Contract\Queue\QueueInterface;
use BookiFlex\Extensions\Cart\Service\Jobs\CleanupExpiredCartsJob;
use Psr\Log\LoggerInterface;

defined( 'ABSPATH' ) || exit;

class Activator extends AbstractExtensionActivator
{
    public function __construct(
        private readonly QueueInterface $queue,
        private readonly ?LoggerInterface $logger = null
    ) {
    }

    public function getSchemas(): array
    {
        return [
            ORM\Schema\CartSchema::class,
            ORM\Schema\CartItemSchema::class,
        ];
    }

    public function enableRuntimeResources(): void
    {
        $this->queue->cancelAll(CleanupExpiredCartsJob::HOOK);

        $this->queue->scheduleRecurring(
            CleanupExpiredCartsJob::HOOK,
            [],
            30 * DAY_IN_SECONDS,
            self::nextNightTime(3, 45)
        );

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

    public function disableRuntimeResources(): void
    {
        $this->queue->cancelAll(CleanupExpiredCartsJob::HOOK);

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