<?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\Notification;

use BookiFlex\Core\Contract\ExtensionInterface;
use BookiFlex\Core\Contract\PluginInterface;
use BookiFlex\Extensions\Notification\Admin\NotificationSettings;
use BookiFlex\Extensions\Notification\Api\Controller\NotificationController;
use BookiFlex\Extensions\Notification\Listener\ReservationSubscriber;
use BookiFlex\Extensions\Notification\ORM\Schema;
use BookiFlex\Extensions\Notification\Service\Jobs\PollDueNotificationsJob;
use BookiFlex\Extensions\Notification\Service\Jobs\PurgeOldNotificationsJob;
use Symfony\Component\Mailer\EventListener\MessageListener;
use Twig\Environment;
use Twig\Extra\CssInliner\CssInlinerExtension;
use Twig\Loader\FilesystemLoader;


defined( 'ABSPATH' ) || exit;

class Notification implements ExtensionInterface
{
    protected PluginInterface $plugin;

    public function init(PluginInterface $plugin): void
    {
        $this->plugin = $plugin;
        $plugin->getContainer()->register(new ServiceProvider());

        $plugin->addConfig([
//            'cpt' => [
//                NotificationSettings::class,
//            ],
            'schemas' => [
                Schema::class,
            ],
            'controllers' => [
                NotificationController::class,
            ],
            'subscribers' => [
                MessageListener::class,
                ReservationSubscriber::class,
            ],
            'settings' => [[
	            'tab'     => 'notifications',
	            'factory' => new NotificationSettings(),
	            'name'    => __('Notifications', 'bookiflex'),
            ]],
        ]);

        // Configure Twig for notification templates
        $this->configureTwig($plugin);

        // Register polling job handler
        $plugin->addAction(PollDueNotificationsJob::HOOK, [$this, 'handlePollDueNotifications'], 10, 1);

        // Register purge job handler
        $plugin->addAction(PurgeOldNotificationsJob::HOOK, [$this, 'handlePurgeOldNotifications'], 10, 1);
    }

    /**
     * Configure Twig environment for notification extension
     */
    private function configureTwig(PluginInterface $plugin): void
    {
        /** @var Environment $twig */
        $twig = $plugin->getContainer()->get('twig');
        /** @var FilesystemLoader $loader */
        $loader = $twig->getLoader();

        // Add CSS Inliner extension for email styling
        $twig->addExtension(new CssInlinerExtension());

        // Add notification templates path (via jetpack_vendor for both dev and production)
//        $templatesPath = $plugin->getContainer()->get('plugin_dir') . 'src/Extensions/Notification/templates';
        $templatesPath = __DIR__ . '/templates';
        $loader->setPaths([$templatesPath], 'notification');

        // Allow user overrides from wp-content/bookiflex/templates/ (update-safe location)
        $userPath = WP_CONTENT_DIR . '/bookiflex/templates';
        if (is_dir($userPath)) {
            $loader->prependPath($userPath, 'notification');
        }
    }

    /**
     * Handle the recurring notification poll job dispatched by Action Scheduler.
     */
    public function handlePollDueNotifications($args = []): void
    {
        $job = $this->plugin->getContainer()->get(PollDueNotificationsJob::class);
        $job->handle(is_array($args) ? $args : []);
    }

    /**
     * Handle the daily purge job dispatched by Action Scheduler.
     */
    public function handlePurgeOldNotifications($args = []): void
    {
        $job = $this->plugin->getContainer()->get(PurgeOldNotificationsJob::class);
        $job->handle(is_array($args) ? $args : []);
    }

    public function getName(): string
    {
        return 'Notification';
    }

    public function getVersion(): string
    {
        return '1.0.0';
    }

    public function isCompatible(string $pluginVersion): bool
    {
        return version_compare($pluginVersion, '1.0.0', '>=');
    }
}


