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

declare(strict_types=1);

namespace BookiFlex\Core\Admin\Menu;

use BookiFlex\Core\Admin\CTL\CustomerTableList;
use BookiFlex\Core\Contract\PluginInterface;
use BookiFlex\Core\Contract\RoleCapabilitiesInterface;

defined( 'ABSPATH' ) || exit;

class CustomerMenu extends AbstractMenu
{
    public const PER_PAGE_FIELD = 'bflex_customers__per_page';

    public function register(PluginInterface $plugin): void
    {
        parent::register($plugin);
        $this->plugin->addAction('load-reservations_page_bookiflex-customers', [$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'   => __('Customers per page', 'bookiflex'),
            'default' => CustomerTableList::ITEMS_PER_PAGE_OPTION,
            'option'  => CustomerMenu::PER_PAGE_FIELD,
        ]);

        $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 customers page
        if (strpos($screen->id, 'customers') === false) {
            return $settings;
        }

        $instance = new CustomerTableList($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 column as it should always be visible
            if ($column === 'cb') {
                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',
            __('Customers', 'bookiflex'),
            __('Customers', 'bookiflex'),
            RoleCapabilitiesInterface::MANAGE_CUSTOMERS,
            'bookiflex-customers',
            [$this, 'handler']
        );

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

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

    public function handler(): void
    {
        $this->displayListTable(
            new CustomerTableList($this->plugin),
            'customers',
            __('Customers', 'bookiflex')
        );
    }
}
