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

declare(strict_types=1);

 namespace BookiFlex\Extensions\Payment\Admin\Menu;

use BookiFlex\Core\Contract\PluginInterface;
use BookiFlex\Core\Contract\RoleCapabilitiesInterface;
use BookiFlex\Extensions\Payment\Admin\CTL\PaymentTableList;
use BookiFlex\Core\Admin\Menu\AbstractMenu;

defined( 'ABSPATH' ) || exit;

class PaymentMenu extends AbstractMenu
{
    public const PER_PAGE_FIELD = 'bflex_payments__per_page';

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

    /**
     * Filter screen option per page value
     */
    public function filterScreenOptionPerPage($status, $option, $value): int
    {
        return (int) $value;
    }

    /**
     * Screen options setup - this is crucial for Screen Options to work
     */
    public function screenOption(): void
    {
        // Add screen options
        add_screen_option('per_page', [
            'label'   => __('Payments Per Page', 'bookiflex'),
            'default' => PaymentTableList::ITEMS_PER_PAGE_OPTION,
            'option'  => PaymentMenu::PER_PAGE_FIELD,
        ]);

        // Add filter for screen options to handle column visibility
        $this->plugin->addFilter('screen_settings', [$this, 'screenSettings'], 10, 2);

        // Enable screen options for this page
        $this->plugin->addFilter('screen_options_show_screen', '__return_true');
    }

    /**
     * Add column visibility options to screen settings
     */
    public function screenSettings($settings, $screen): string
    {
        // Check if we're on the payments page
        if (strpos($screen->id, 'payments') === false) {
            return $settings;
        }

        $instance = new PaymentTableList($this->plugin);
        $columns = $instance->get_columns();
        $hidden = $instance->get_hidden_columns();

        $settings .= '<fieldset class="metabox-prefs">';
        $settings .= '<legend>' . __('Columns', 'bookiflex') . '</legend>';

        foreach ($columns as $column => $title) {
            // Skip checkbox and actions columns as they should always be visible
            if (in_array($column, ['cb', 'actions'])) {
                continue;
            }

            $checked = !in_array($column, $hidden) ? 'checked="checked"' : '';
            $settings .= sprintf(
                '<label for="%1$s-hide"><input class="hide-column-tog" name="%1$s-hide" type="checkbox" id="%1$s-hide" value="%1$s" %2$s>%3$s</label>',
                $column,
                $checked,
	            wp_strip_all_tags($title)
            );
        }

        $settings .= '</fieldset>';

        return $settings;
    }

    public function registerMenu(): void
    {
        $hook = add_submenu_page(
            'bookiflex-reservations',
            __('Payments', 'bookiflex'),
            __('Payments', 'bookiflex'),
            RoleCapabilitiesInterface::MANAGE_PAYMENTS,
            'bookiflex-payments',
            [$this, 'handler']
        );
        $this->plugin->addAction("load-$hook", [$this, 'addScreenOptions']);
    }

    public function addScreenOptions(): void
    {
//		get_current_screen()->add_help_tab(
//			[
//				'id'      => 'overview',
//				'title'   => __('Overview', 'bookiflex'),
//				'content' => '<p>' . __('Payment description', 'bookiflex') . '</p>',
//			]
//		);
//
//		get_current_screen()->set_help_sidebar(
//			'<p><strong>' . __('For more information:', 'bookiflex') . '</strong></p>' .
//                    '<p>' . __('<a href="#">Payment documentation</a>', 'bookiflex') . '</p>' .
//                    '<p>' . __('<a href="#">Support</a>', 'bookiflex') . '</p>'
//		);

        add_screen_option('per_page', [
            'label'   => __('Payments Per Page', 'bookiflex'),
            'default' => 5,
            'option'  => self::PER_PAGE_FIELD
        ]);
    }

    public function handler(): void
    {
        $this->displayListTable(
            new PaymentTableList($this->plugin),
            'payments',
            __('Payments', 'bookiflex')
        );
    }
}
