<?php
/**
 * Admin UI: edit guest UI strings (local + English).
 *
 * @package Maca_Menu_List
 */

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

/**
 * Enqueue guest strings admin assets on settings screen.
 *
 * @param string $hook Current admin page hook.
 */
function maca_menulist_enqueue_admin_guest_strings_assets($hook) {
    if (
        strpos($hook, 'maca-restu-settings') === false
        || !function_exists('maca_menulist_guest_ui_strings_available')
        || !maca_menulist_guest_ui_strings_available()
    ) {
        return;
    }

    wp_enqueue_script(
        'maca-menulist-admin-guest-strings',
        MACA_MENULIST_PLUGIN_URL . 'assets/js/admin-guest-strings.js',
        array(),
        MACA_MENULIST_VERSION,
        true
    );
}

add_action('admin_enqueue_scripts', 'maca_menulist_enqueue_admin_guest_strings_assets');

/**
 * Render Guest UI texts settings tab panel.
 */
function maca_menulist_render_admin_guest_strings_panel() {
    if (!function_exists('maca_menulist_guest_ui_strings_available') || !maca_menulist_guest_ui_strings_available()) {
        return;
    }

    $registry   = maca_menulist_get_guest_string_registry();
    $overrides  = maca_menulist_get_guest_string_overrides();
    $file_code = maca_menulist_guest_translation_file_code();
    $bundle    = maca_menulist_get_guest_translation_catalog($file_code);
    $local_name = maca_menulist_get_local_language_label();

    $groups = array();

    foreach ($registry as $row) {
        $group = $row['group'] !== '' ? $row['group'] : __('Other', 'maca-restu-pro');

        if (!isset($groups[ $group ])) {
            $groups[ $group ] = array();
        }

        $groups[ $group ][] = $row;
    }

    ksort($groups, SORT_NATURAL | SORT_FLAG_CASE);
    ?>
    <section class="maca-settings-section maca-settings-tab-panel" id="maca-settings-guest-texts" data-settings-tab="guest-texts">
        <h2 class="maca-settings-section-title"><?php esc_html_e('Guest UI texts', 'maca-restu-pro'); ?></h2>
        <p class="maca-settings-section-desc description">
            <?php
            echo esc_html__(
                'Override system texts shown to guests (buttons, booking form, order status, etc.). Leave a field empty to use the bundled translation. English column is used when guests choose the English flag.',
                'maca-restu-pro'
            );
            ?>
        </p>
        <p class="description">
            <strong><?php esc_html_e('Local language:', 'maca-restu-pro'); ?></strong>
            <?php echo esc_html($local_name); ?>
            (<?php echo esc_html($file_code); ?>)
        </p>

        <div class="maca-guest-strings-toolbar">
            <label class="screen-reader-text" for="maca-guest-strings-search"><?php esc_html_e('Search guest texts', 'maca-restu-pro'); ?></label>
            <input type="search" id="maca-guest-strings-search" class="regular-text" placeholder="<?php esc_attr_e('Search…', 'maca-restu-pro'); ?>" autocomplete="off">
            <label for="maca-guest-strings-group" class="screen-reader-text"><?php esc_html_e('Filter by group', 'maca-restu-pro'); ?></label>
            <select id="maca-guest-strings-group">
                <option value=""><?php esc_html_e('All groups', 'maca-restu-pro'); ?></option>
                <?php foreach (array_keys($groups) as $group_name) : ?>
                    <option value="<?php echo esc_attr($group_name); ?>"><?php echo esc_html($group_name); ?></option>
                <?php endforeach; ?>
            </select>
            <label class="maca-guest-strings-filter-custom">
                <input type="checkbox" id="maca-guest-strings-custom-only" value="1">
                <?php esc_html_e('Custom overrides only', 'maca-restu-pro'); ?>
            </label>
        </div>

        <div class="maca-guest-strings-wrap">
            <?php foreach ($groups as $group_name => $rows) : ?>
                <details class="maca-guest-strings-group" data-group="<?php echo esc_attr($group_name); ?>" open>
                    <summary class="maca-guest-strings-group-title">
                        <?php echo esc_html($group_name); ?>
                        <span class="maca-guest-strings-count"><?php echo esc_html((string) count($rows)); ?></span>
                    </summary>
                    <table class="widefat striped maca-guest-strings-table">
                        <thead>
                            <tr>
                                <th scope="col" class="maca-col-reference"><?php esc_html_e('English (reference)', 'maca-restu-pro'); ?></th>
                                <th scope="col" class="maca-col-local"><?php echo esc_html($local_name); ?></th>
                                <th scope="col" class="maca-col-en"><?php esc_html_e('English (guest flag)', 'maca-restu-pro'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($rows as $row) : ?>
                                <?php
                                $msgid         = $row['msgid'];
                                $slug          = $row['string_id'];
                                $default_local = isset($bundle[ $msgid ]) ? $bundle[ $msgid ] : $msgid;
                                $value_local   = isset($overrides[ $msgid ]['local']) ? $overrides[ $msgid ]['local'] : '';
                                $value_en      = isset($overrides[ $msgid ]['en']) ? $overrides[ $msgid ]['en'] : '';
                                $has_custom    = $value_local !== '' || $value_en !== '';
                                $search_blob   = strtolower($msgid . ' ' . $default_local . ' ' . $value_local . ' ' . $value_en);
                                ?>
                                <tr class="maca-guest-strings-row<?php echo $has_custom ? ' maca-guest-strings-row--custom' : ''; ?>"
                                    data-group="<?php echo esc_attr($group_name); ?>"
                                    data-search="<?php echo esc_attr($search_blob); ?>"
                                    data-custom="<?php echo $has_custom ? '1' : '0'; ?>">
                                    <td class="maca-col-reference">
                                        <code class="maca-guest-strings-msgid"><?php echo esc_html($msgid); ?></code>
                                    </td>
                                    <td class="maca-col-local">
                                        <label class="screen-reader-text" for="maca_guest_text_<?php echo esc_attr($slug); ?>_local">
                                            <?php echo esc_html($local_name); ?>
                                        </label>
                                        <textarea
                                            name="maca_guest_text[<?php echo esc_attr($slug); ?>][local]"
                                            id="maca_guest_text_<?php echo esc_attr($slug); ?>_local"
                                            rows="2"
                                            class="large-text"
                                            placeholder="<?php echo esc_attr($default_local); ?>"
                                        ><?php echo esc_textarea($value_local); ?></textarea>
                                    </td>
                                    <td class="maca-col-en">
                                        <label class="screen-reader-text" for="maca_guest_text_<?php echo esc_attr($slug); ?>_en">
                                            <?php esc_html_e('English (guest flag)', 'maca-restu-pro'); ?>
                                        </label>
                                        <textarea
                                            name="maca_guest_text[<?php echo esc_attr($slug); ?>][en]"
                                            id="maca_guest_text_<?php echo esc_attr($slug); ?>_en"
                                            rows="2"
                                            class="large-text"
                                            placeholder="<?php echo esc_attr($msgid); ?>"
                                        ><?php echo esc_textarea($value_en); ?></textarea>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </details>
            <?php endforeach; ?>
        </div>
        <p class="description maca-guest-strings-footer-note">
            <?php
            echo esc_html(
                sprintf(
                    /* translators: %d: number of strings */
                    _n('%d string in total.', '%d strings in total.', count($registry), 'maca-restu-pro'),
                    count($registry)
                )
            );
            ?>
        </p>
    </section>
    <?php
}
