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

declare(strict_types=1);

namespace BookiFlex\Extensions\NotificationLog\Admin\Menu;

use BookiFlex\Core\Admin\Menu\AbstractMenu;
use BookiFlex\Core\Contract\PluginInterface;
use BookiFlex\Core\Contract\RoleCapabilitiesInterface;
use BookiFlex\Extensions\NotificationLog\Admin\CTL\NotificationLogTableList;

defined('ABSPATH') || exit;

class NotificationLogMenu extends AbstractMenu
{
    public const PER_PAGE_FIELD = 'bflex_notification_log__per_page';

    public function register(PluginInterface $plugin): void
    {
        parent::register($plugin);
        $this->plugin->addFilter(
            'set_screen_option_' . self::PER_PAGE_FIELD,
            [$this, 'filterScreenOptionPerPage'],
            10,
            3
        );
    }

    public function filterScreenOptionPerPage($status, $option, $value): int
    {
        return (int) $value;
    }

    public function registerMenu(): void
    {
        $hook = add_submenu_page(
            'bookiflex-reservations',
            __('Notifications', 'bookiflex'),
            __('Notifications', 'bookiflex'),
            RoleCapabilitiesInterface::MANAGE_RESERVATIONS,
            'bookiflex-notification-log',
            [$this, 'handler']
        );

        $this->plugin->addAction("load-$hook", [$this, 'addScreenOptions']);
    }

    public function addScreenOptions(): void
    {
        add_screen_option('per_page', [
            'label'   => __('Notifications Per Page', 'bookiflex'),
            'default' => NotificationLogTableList::ITEMS_PER_PAGE_OPTION,
            'option'  => self::PER_PAGE_FIELD,
        ]);
    }

    public function handler(): void
    {
        $this->displayListTable(
            new NotificationLogTableList($this->plugin),
            'notification-log',
            __('Notifications', 'bookiflex')
        );
    }
}
