<?php
/**
 * Settings page tab helpers.
 *
 * @package Maca_MenuList
 */

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Settings tab definitions.
 *
 * @return array<string, array{label: string, content_modes: string[]}>
 */
function maca_menulist_get_settings_tabs() {
    $tabs = array(
        'restaurant' => array(
            'label'         => __('Restaurant', 'maca-restu-pro'),
            'content_modes' => array('files', 'dishes'),
        ),
        'menu'       => array(
            'label'         => __('Menu & language', 'maca-restu-pro'),
            'content_modes' => array('files', 'dishes'),
        ),
        'dishes'     => array(
            'label'         => __('Online menu & footer', 'maca-restu-pro'),
            'content_modes' => array('dishes'),
        ),
        'orders'     => array(
            'label'         => __('Table ordering', 'maca-restu-pro'),
            'content_modes' => array('dishes'),
        ),
        'hours'      => array(
            'label'         => __('Opening hours', 'maca-restu-pro'),
            'content_modes' => array('files', 'dishes'),
        ),
        'exceptions' => array(
            'label'         => __('Exceptions', 'maca-restu-pro'),
            'content_modes' => array('files', 'dishes'),
        ),
        'pdf'        => array(
            'label'         => __('PDF menus', 'maca-restu-pro'),
            'content_modes' => array('files'),
        ),
    );

    if (function_exists('maca_menulist_guest_ui_strings_available') && maca_menulist_guest_ui_strings_available()) {
        $tabs = array_merge(
            array_slice($tabs, 0, 2, true),
            array(
                'guest-texts' => array(
                    'label'         => __('Guest UI texts', 'maca-restu-pro'),
                    'content_modes' => array('files', 'dishes'),
                ),
            ),
            array_slice($tabs, 2, null, true)
        );
    }

    /**
     * Filter settings tabs (e.g. limit tabs for booking hosts).
     *
     * @param array<string, array{label: string, content_modes: string[]}> $tabs Tab definitions.
     */
    return apply_filters('maca_menulist_settings_tabs', $tabs);
}

/**
 * Active settings tab from GET/POST.
 *
 * @param string $content_mode Current content mode (files|dishes).
 * @return string Tab slug.
 */
function maca_menulist_get_active_settings_tab($content_mode = '') {
    $tabs = maca_menulist_get_settings_tabs();
    $ids  = array_keys($tabs);

    if (isset($_GET['tab'])) {
        $tab = sanitize_key(wp_unslash($_GET['tab']));
    } elseif (isset($_POST['maca_settings_tab'])) {
        $tab = sanitize_key(wp_unslash($_POST['maca_settings_tab']));
    } else {
        $tab = 'restaurant';
    }

    if ($tab === 'booking') {
        $tab = 'hours';
    }

    if (!in_array($tab, $ids, true)) {
        $tab = 'restaurant';
    }

    if ($content_mode === '') {
        $content_mode = maca_menulist_get_content_mode();
    }

    if (!maca_menulist_settings_tab_visible_for_mode($tab, $content_mode)) {
        foreach ($ids as $candidate) {
            if (maca_menulist_settings_tab_visible_for_mode($candidate, $content_mode)) {
                return $candidate;
            }
        }
    }

    return $tab;
}

/**
 * Whether a tab applies to the current content mode.
 *
 * @param string $tab          Tab slug.
 * @param string $content_mode files|dishes.
 * @return bool
 */
function maca_menulist_settings_tab_visible_for_mode($tab, $content_mode) {
    $tabs = maca_menulist_get_settings_tabs();
    if (!isset($tabs[ $tab ])) {
        return false;
    }

    return in_array($content_mode, $tabs[ $tab ]['content_modes'], true);
}

/**
 * Render settings tab navigation.
 *
 * @param string $active_tab   Active tab slug.
 * @param string $content_mode Current content mode.
 */
function maca_menulist_render_settings_tab_nav($active_tab, $content_mode) {
    $base_url = admin_url('admin.php?page=maca-restu-settings');
    ?>
    <nav class="nav-tab-wrapper maca-settings-tabs" aria-label="<?php esc_attr_e('Settings sections', 'maca-restu-pro'); ?>">
        <?php foreach (maca_menulist_get_settings_tabs() as $tab_id => $tab_def) : ?>
            <?php
            $visible = maca_menulist_settings_tab_visible_for_mode($tab_id, $content_mode);
            $classes = 'nav-tab';
            if ($active_tab === $tab_id) {
                $classes .= ' nav-tab-active';
            }
            if (!$visible) {
                $classes .= ' maca-settings-tab--hidden';
            }
            ?>
            <a href="<?php echo esc_url(add_query_arg('tab', $tab_id, $base_url)); ?>"
               class="<?php echo esc_attr($classes); ?>"
               data-settings-tab="<?php echo esc_attr($tab_id); ?>"
               data-content-modes="<?php echo esc_attr(implode(',', $tab_def['content_modes'])); ?>"
               <?php echo !$visible ? ' hidden' : ''; ?>>
                <?php echo esc_html($tab_def['label']); ?>
            </a>
        <?php endforeach; ?>
    </nav>
    <?php
}

/**
 * Payment settings tab definitions (settings area, excluding guide).
 *
 * @return array<string, string> slug => label
 */
function maca_menulist_get_payments_settings_tabs() {
    return array(
        'flow'       => __('Payment flow', 'maca-restu-pro'),
        'tips'       => __('Tips', 'maca-restu-pro'),
        'staff'      => __('Staff screens', 'maca-restu-pro'),
        'providers'  => __('Stripe & Swish', 'maca-restu-pro'),
    );
}

/**
 * Active payments settings sub-tab.
 *
 * @return string
 */
function maca_menulist_get_active_payments_settings_tab() {
    $allowed = array_keys(maca_menulist_get_payments_settings_tabs());

    if (isset($_GET['tab'])) {
        $tab = sanitize_key(wp_unslash($_GET['tab']));
    } elseif (isset($_POST['maca_payments_tab'])) {
        $tab = sanitize_key(wp_unslash($_POST['maca_payments_tab']));
    } else {
        return 'flow';
    }

    if ($tab === 'settings') {
        return 'flow';
    }

    if ($tab === 'guide') {
        return 'guide';
    }

    return in_array($tab, $allowed, true) ? $tab : 'flow';
}
