<?php
/**
 * Admin: guest payments (Stripe / Swish) preparation and setup guide.
 *
 * @package Maca_Menu_List
 */

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

/**
 * Payments admin screen.
 */
class Maca_MenuList_Admin_Payments {

    /**
     * Constructor.
     */
    public function __construct() {
        add_action('admin_menu', array($this, 'register_menu'), 98);
        add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts'));
    }

    /**
     * Register submenu (dish mode + table orders).
     */
    public function register_menu() {
        if (!maca_menulist_is_dishes_mode()) {
            return;
        }

        if (function_exists('maca_menulist_pro_has_feature') && !maca_menulist_pro_has_feature('table_orders')) {
            return;
        }

        add_submenu_page(
            'maca-restu',
            __('Betalningar', 'maca-restu-pro'),
            __('Betalningar', 'maca-restu-pro'),
            maca_menulist_admin_required_cap(),
            'maca-restu-payments',
            array($this, 'render_page')
        );
    }

    /**
     * @param string $hook Hook suffix.
     */
    public function enqueue_scripts($hook) {
        if (strpos($hook, 'maca-restu-payments') === false) {
            return;
        }

        if (function_exists('maca_menulist_load_textdomain')) {
            maca_menulist_load_textdomain();
        }

        wp_enqueue_style(
            'maca-menulist-admin-payments',
            MACA_MENULIST_PLUGIN_URL . 'assets/css/admin-payments.css',
            array(),
            MACA_MENULIST_VERSION
        );

        wp_enqueue_script(
            'maca-menulist-admin-payments-pos-warning',
            MACA_MENULIST_PLUGIN_URL . 'assets/js/admin-payments-pos-warning.js',
            array('jquery'),
            MACA_MENULIST_VERSION,
            true
        );

        $guide_url = admin_url('admin.php?page=maca-restu-payments&tab=guide');

        wp_localize_script(
            'maca-menulist-admin-payments-pos-warning',
            'macaMenulistPaymentsPosWarning',
            array(
                'title' => __('Cash register laws and online payment', 'maca-restu-pro'),
                'intro' => __(
                    'In many countries — including Sweden — businesses that accept cash, card, or electronic payments (such as Swish) may be required to use a certified cash register or POS system. Rules vary by country, region, and turnover.',
                    'maca-restu-pro'
                ),
                'notPos' => __(
                    'maca Restu Pro is not a certified cash register or POS. Guest payments via Stripe or Swish in maca Restu do not replace your legal duty to register sales in your certified cash system where the law requires it.',
                    'maca-restu-pro'
                ),
                'doNotEnable' => __(
                    'If you are subject to cash register requirements, you should not enable online guest payments in maca Restu Pro unless you also register those sales in your certified POS (for example via integration) or otherwise comply with applicable law.',
                    'maca-restu-pro'
                ),
                'yourResponsibility' => __(
                    'It is solely your responsibility to investigate and follow the rules in your country and region — including cash registers, VAT, official receipts, refunds, and payment regulations. maca Development does not provide legal or tax advice.',
                    'maca-restu-pro'
                ),
                'receipts' => __(
                    'Order and payment confirmation emails from maca Restu are not official tax receipts. Your POS or cash register issues the legal receipt when required.',
                    'maca-restu-pro'
                ),
                'swedenExtra' => __(
                    'Example (Sweden): Swish and card payments must normally be registered in a certified cash register when the turnover threshold applies. Plan how online payments from maca Restu are booked in your POS before you enable them.',
                    'maca-restu-pro'
                ),
                'guideLinkText' => __('Open payments setup guide', 'maca-restu-pro'),
                'guideUrl' => $guide_url,
                'ackLabel' => __(
                    'I understand and accept that enabling online payments is my responsibility, that I must comply with applicable cash register and tax rules, and that I have read all of the information above.',
                    'maca-restu-pro'
                ),
                'scrollHint' => __('Scroll to the end to enable the confirmation checkbox.', 'maca-restu-pro'),
                'cancel' => __('Cancel', 'maca-restu-pro'),
                'continue' => __('Continue', 'maca-restu-pro'),
            )
        );
    }

    /**
     * Render settings or guide tab.
     */
    public function render_page() {
        if (!maca_menulist_user_can_manage_plugin()) {
            return;
        }

        require_once MACA_MENULIST_PLUGIN_DIR . 'includes/markdown.php';

        $tab = maca_menulist_get_active_payments_settings_tab();

        if ($tab !== 'guide' && isset($_POST['maca_menulist_payments_save']) && maca_menulist_user_can_manage_payments_settings()) {
            check_admin_referer('maca_menulist_payments_settings');

            $saved = maca_menulist_payments_save_settings_from_request();
            if (is_wp_error($saved)) {
                add_settings_error('maca_payments', 'error', $saved->get_error_message());
            } else {
                add_settings_error(
                    'maca_payments',
                    'success',
                    __('Payment settings saved.', 'maca-restu-pro'),
                    'updated'
                );
            }
        }

        settings_errors('maca_payments');

        $base_url = admin_url('admin.php?page=maca-restu-payments');
        $checklist = maca_menulist_payments_get_checklist();
        $webhook_stripe = maca_menulist_payments_webhook_url('stripe');
        $webhook_swish = maca_menulist_payments_webhook_url('swish');
        ?>
        <div class="wrap maca-payments-wrap<?php echo esc_attr(maca_menulist_admin_demo_readonly_wrap_class()); ?>">
            <h1><?php esc_html_e('Guest payments', 'maca-restu-pro'); ?></h1>

            <?php maca_menulist_render_admin_demo_readonly_notice(); ?>

            <p>
                <a class="button" href="<?php echo esc_url(admin_url('admin.php?page=maca-restu-payment-history')); ?>">
                    <?php esc_html_e('Open payment history', 'maca-restu-pro'); ?>
                </a>
            </p>

            <?php if (!maca_menulist_payments_guest_flow_enabled()) : ?>
                <div class="notice notice-warning inline maca-payments-notice">
                    <p>
                        <?php esc_html_e(
                            'Enable at least one guest payment method below, and make sure table ordering with prices is on. Staff can always mark orders as paid on the waiter screen.',
                            'maca-restu-pro'
                        ); ?>
                    </p>
                </div>
            <?php endif; ?>

            <nav class="nav-tab-wrapper maca-payments-tabs" aria-label="<?php esc_attr_e('Payments', 'maca-restu-pro'); ?>">
                <?php foreach (maca_menulist_get_payments_settings_tabs() as $tab_id => $tab_label) : ?>
                    <a href="<?php echo esc_url(add_query_arg('tab', $tab_id, $base_url)); ?>"
                       class="nav-tab <?php echo $tab === $tab_id ? 'nav-tab-active' : ''; ?>"
                       data-payments-tab="<?php echo esc_attr($tab_id); ?>">
                        <?php echo esc_html($tab_label); ?>
                    </a>
                <?php endforeach; ?>
                <a href="<?php echo esc_url(add_query_arg('tab', 'guide', $base_url)); ?>"
                   class="nav-tab <?php echo $tab === 'guide' ? 'nav-tab-active' : ''; ?>">
                    <?php esc_html_e('Setup guide', 'maca-restu-pro'); ?>
                </a>
            </nav>

            <div class="maca-payments-layout">
                <aside class="maca-payments-sidebar">
                    <h2><?php esc_html_e('Checklist', 'maca-restu-pro'); ?></h2>
                    <ul class="maca-payments-checklist">
                        <?php foreach ($checklist as $item) : ?>
                            <li class="<?php echo !empty($item['done']) ? 'is-done' : 'is-pending'; ?>">
                                <span class="maca-payments-check" aria-hidden="true"></span>
                                <span class="maca-payments-check-label"><?php echo esc_html($item['label']); ?></span>
                                <?php if (!empty($item['hint'])) : ?>
                                    <span class="maca-payments-check-hint"><?php echo esc_html($item['hint']); ?></span>
                                <?php endif; ?>
                            </li>
                        <?php endforeach; ?>
                    </ul>

                    <h3><?php esc_html_e('Webhook URLs (this site)', 'maca-restu-pro'); ?></h3>
                    <p class="description"><?php esc_html_e('Copy into Stripe or your Swish partner portal when the guide asks for a callback URL.', 'maca-restu-pro'); ?></p>
                    <label class="maca-payments-webhook-label"><?php esc_html_e('Stripe', 'maca-restu-pro'); ?></label>
                    <code class="maca-payments-webhook-url"><?php echo esc_html($webhook_stripe); ?></code>
                    <label class="maca-payments-webhook-label"><?php esc_html_e('Swish', 'maca-restu-pro'); ?></label>
                    <code class="maca-payments-webhook-url"><?php echo esc_html($webhook_swish); ?></code>
                </aside>

                <div class="maca-payments-main">
                    <?php
                    if ($tab === 'guide') {
                        $this->render_guide_tab();
                    } else {
                        $this->render_settings_tab($webhook_stripe, $webhook_swish);
                    }
                    ?>
                </div>
            </div>
        </div>
        <?php
    }

    /**
     * Settings form.
     *
     * @param string $webhook_stripe Stripe webhook URL.
     * @param string $webhook_swish  Swish webhook URL.
     */
    private function render_settings_tab($webhook_stripe, $webhook_swish) {
        $settings_tab = maca_menulist_get_active_payments_settings_tab();
        if ($settings_tab === 'guide') {
            $settings_tab = 'flow';
        }
        ?>
        <form method="post" action="" class="maca-payments-settings-form" data-active-tab="<?php echo esc_attr($settings_tab); ?>">
            <?php wp_nonce_field('maca_menulist_payments_settings'); ?>
            <input type="hidden" name="maca_menulist_payments_save" value="1">
            <input type="hidden" name="maca_payments_tab" id="maca_payments_tab" value="<?php echo esc_attr($settings_tab); ?>">

            <div class="maca-payments-settings-panel <?php echo $settings_tab === 'flow' ? 'is-active' : ''; ?>" data-payments-tab="flow">
                <?php $this->render_payments_flow_panel(); ?>
            </div>

            <div class="maca-payments-settings-panel maca-payments-online-dependent <?php echo $settings_tab === 'tips' ? 'is-active' : ''; ?>" data-payments-tab="tips">
                <?php $this->render_payments_tips_panel(); ?>
            </div>

            <div class="maca-payments-settings-panel <?php echo $settings_tab === 'staff' ? 'is-active' : ''; ?>" data-payments-tab="staff">
                <?php $this->render_payments_staff_panel(); ?>
            </div>

            <div class="maca-payments-settings-panel maca-payments-online-dependent <?php echo $settings_tab === 'providers' ? 'is-active' : ''; ?>" data-payments-tab="providers">
                <?php $this->render_payments_providers_panel($webhook_stripe, $webhook_swish); ?>
            </div>

            <input type="hidden" name="maca_menulist_online_payment_pos_ack" id="maca_online_payment_pos_ack" value="0">

            <?php
            if (maca_menulist_user_can_manage_payments_settings()) {
                submit_button(__('Save payment settings', 'maca-restu-pro'));
            }
            ?>
        </form>

        <?php $this->render_online_payment_pos_warning_modal(); ?>
        <?php
    }

    /**
     * Modal: cash register / online payment acknowledgment (shown before enabling Stripe or Swish).
     */
    private function render_online_payment_pos_warning_modal() {
        ?>
        <div id="maca-pos-warning-modal" class="maca-pos-warning-modal" hidden aria-hidden="true">
            <div class="maca-pos-warning-modal__backdrop" data-maca-pos-warning-close></div>
            <div class="maca-pos-warning-modal__dialog" role="dialog" aria-modal="true" aria-labelledby="maca-pos-warning-title">
                <h2 id="maca-pos-warning-title" class="maca-pos-warning-modal__title"></h2>
                <div class="maca-pos-warning-modal__scroll" id="maca-pos-warning-scroll" tabindex="0">
                    <div class="maca-pos-warning-modal__body" id="maca-pos-warning-body"></div>
                    <p class="maca-pos-warning-modal__scroll-hint" id="maca-pos-warning-scroll-hint"></p>
                    <label class="maca-pos-warning-modal__ack" id="maca-pos-warning-ack-wrap">
                        <input type="checkbox" id="maca-pos-warning-ack" disabled>
                        <span id="maca-pos-warning-ack-label"></span>
                    </label>
                </div>
                <div class="maca-pos-warning-modal__actions">
                    <button type="button" class="button" id="maca-pos-warning-cancel" data-maca-pos-warning-close></button>
                    <button type="button" class="button button-primary" id="maca-pos-warning-continue" disabled></button>
                </div>
            </div>
        </div>
        <?php
    }

    /**
     * Guest payment flow settings.
     */
    private function render_payments_flow_panel() {
        $primary     = maca_menulist_payments_primary_provider();
        $timing      = maca_menulist_payments_timing();
        $use_online = maca_menulist_payments_use_online();
        ?>
        <h2><?php esc_html_e('Guest payment flow', 'maca-restu-pro'); ?></h2>
        <p class="description">
            <?php esc_html_e('Guests choose how to pay after sending an order (or when it has been served). Your staff confirm manual payments on the waiter screen.', 'maca-restu-pro'); ?>
        </p>

        <table class="form-table" role="presentation">
            <tr>
                <th scope="row"><?php esc_html_e('Pay at table', 'maca-restu-pro'); ?></th>
                <td>
                    <label>
                        <input type="checkbox" name="maca_menulist_payments_allow_manual" value="1"
                            <?php checked(maca_menulist_payments_allow_manual()); ?>>
                        <?php esc_html_e('Guest can choose to pay the server (staff marks order as paid)', 'maca-restu-pro'); ?>
                    </label>
                </td>
            </tr>
            <tr>
                <th scope="row"><?php esc_html_e('Split the bill', 'maca-restu-pro'); ?></th>
                <td>
                    <label>
                        <input type="checkbox" name="maca_menulist_payments_allow_bill_split" value="1"
                            <?php checked(maca_menulist_payments_allow_bill_split()); ?>>
                        <?php esc_html_e('Guests can split the bill equally or pay a fixed amount per person until the bill is paid (online or with server)', 'maca-restu-pro'); ?>
                    </label>
                </td>
            </tr>
            <tr>
                <th scope="row"><?php esc_html_e('When guests can pay', 'maca-restu-pro'); ?></th>
                <td>
                    <fieldset>
                        <label>
                            <input type="radio" name="maca_menulist_payments_timing" value="at_order"
                                <?php checked($timing, 'at_order'); ?>>
                            <?php esc_html_e('At order — payment choice right after the order is sent', 'maca-restu-pro'); ?>
                        </label>
                        <br>
                        <label>
                            <input type="radio" name="maca_menulist_payments_timing" value="after_meal"
                                <?php checked($timing, 'after_meal'); ?>>
                            <?php esc_html_e('After meal — when the order is marked served', 'maca-restu-pro'); ?>
                        </label>
                    </fieldset>
                    <p class="description">
                        <?php esc_html_e('Applies to both online and pay-at-table choices. Manual pay-at-table can be selected as soon as timing allows.', 'maca-restu-pro'); ?>
                    </p>
                </td>
            </tr>
            <tr>
                <th scope="row"><?php esc_html_e('Online payment', 'maca-restu-pro'); ?></th>
                <td>
                    <label>
                        <input type="checkbox" class="maca-online-payment-trigger" name="maca_menulist_payments_use_online" value="1"
                            <?php checked($use_online); ?>>
                        <?php esc_html_e('Use online payment (Stripe and/or Swish)', 'maca-restu-pro'); ?>
                    </label>
                    <p class="description">
                        <?php esc_html_e('Required before guests can pay online. Unlocks Stripe & Swish settings and tips. You must confirm cash register responsibilities when enabling.', 'maca-restu-pro'); ?>
                    </p>

                    <div class="maca-payments-online-options" <?php echo $use_online ? '' : ' hidden'; ?>>
                        <fieldset class="maca-payments-online-fieldset">
                            <legend class="screen-reader-text"><?php esc_html_e('Online payment options', 'maca-restu-pro'); ?></legend>
                            <label>
                                <input type="checkbox" class="maca-payments-online-input" name="maca_menulist_payments_enabled" value="1"
                                    <?php checked(get_option('maca_menulist_payments_enabled', false)); ?>
                                    <?php disabled(!$use_online); ?>>
                                <?php esc_html_e('Enable Stripe checkout for guests', 'maca-restu-pro'); ?>
                            </label>
                            <br>
                            <label>
                                <input type="checkbox" class="maca-payments-online-input" name="maca_menulist_payments_allow_online" value="1"
                                    <?php checked(get_option('maca_menulist_payments_allow_online', '1') === '1'); ?>
                                    <?php disabled(!$use_online); ?>>
                                <?php esc_html_e('Show “Pay online” when Stripe is configured', 'maca-restu-pro'); ?>
                            </label>
                            <p style="margin-top:12px;">
                                <label for="maca_menulist_payments_primary_provider">
                                    <?php esc_html_e('Primary provider', 'maca-restu-pro'); ?>
                                </label><br>
                                <select id="maca_menulist_payments_primary_provider" class="maca-payments-online-input"
                                    name="maca_menulist_payments_primary_provider" <?php disabled(!$use_online); ?>>
                                    <?php foreach (maca_menulist_payments_get_providers() as $provider) : ?>
                                        <option value="<?php echo esc_attr($provider->get_id()); ?>"
                                            <?php selected($primary, $provider->get_id()); ?>>
                                            <?php echo esc_html($provider->get_label()); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                            </p>
                            <p class="description">
                                <?php esc_html_e('Shown first to guests when both providers are configured. API keys under Stripe & Swish.', 'maca-restu-pro'); ?>
                            </p>
                        </fieldset>
                    </div>
                </td>
            </tr>
        </table>
        <?php
    }

    /**
     * Tip settings panel.
     */
    private function render_payments_tips_panel() {
        $this->render_payments_online_disabled_notice();
        $use_online = maca_menulist_payments_use_online();
        ?>
        <h2><?php esc_html_e('Tips', 'maca-restu-pro'); ?></h2>
        <p class="description"><?php esc_html_e('Optional tip before the guest pays online (included in Stripe checkout).', 'maca-restu-pro'); ?></p>

        <div class="maca-payments-online-tips-options" <?php echo $use_online ? '' : ' hidden'; ?>>
        <table class="form-table" role="presentation">
            <tr>
                <th scope="row"><?php esc_html_e('Tips', 'maca-restu-pro'); ?></th>
                <td>
                    <label>
                        <input type="checkbox" class="maca-payments-online-input" name="maca_menulist_tips_enabled" value="1"
                            <?php checked(get_option('maca_menulist_tips_enabled', '0') === '1'); ?>
                            <?php disabled(!$use_online); ?>>
                        <?php esc_html_e('Let guests add a tip when paying', 'maca-restu-pro'); ?>
                    </label>
                    <fieldset style="margin-top:12px;">
                        <legend class="screen-reader-text"><?php esc_html_e('Tip options', 'maca-restu-pro'); ?></legend>
                        <label>
                            <input type="radio" class="maca-payments-online-input" name="maca_menulist_tips_mode" value="percent"
                                <?php checked(maca_menulist_tips_mode(), 'percent'); ?>
                                <?php disabled(!$use_online); ?>>
                            <?php esc_html_e('Preset percentages', 'maca-restu-pro'); ?>
                        </label>
                        <br>
                        <label>
                            <input type="radio" class="maca-payments-online-input" name="maca_menulist_tips_mode" value="fixed"
                                <?php checked(maca_menulist_tips_mode(), 'fixed'); ?>
                                <?php disabled(!$use_online); ?>>
                            <?php esc_html_e('Preset fixed amounts', 'maca-restu-pro'); ?>
                        </label>
                    </fieldset>
                    <p>
                        <label for="maca_tips_presets"><?php esc_html_e('Preset values', 'maca-restu-pro'); ?></label><br>
                        <input type="text" class="regular-text maca-payments-online-input" id="maca_tips_presets" name="maca_menulist_tips_presets"
                            value="<?php echo esc_attr((string) get_option('maca_menulist_tips_presets', maca_menulist_tips_mode() === 'fixed' ? '0,20,50,100' : '0,5,10,15')); ?>"
                            <?php disabled(!$use_online); ?>>
                    </p>
                    <p class="description">
                        <?php esc_html_e('Comma-separated. For percentages use e.g. 0,5,10,15. For fixed amounts use e.g. 0,20,50,100 (same currency as menu prices).', 'maca-restu-pro'); ?>
                    </p>
                    <label>
                        <input type="checkbox" class="maca-payments-online-input" name="maca_menulist_tips_allow_none" value="1"
                            <?php checked(maca_menulist_tips_allow_none()); ?>
                            <?php disabled(!$use_online); ?>>
                        <?php esc_html_e('Include “no tip” option', 'maca-restu-pro'); ?>
                    </label>
                    <br>
                    <label>
                        <input type="checkbox" class="maca-payments-online-input" name="maca_menulist_tips_allow_custom" value="1"
                            <?php checked(maca_menulist_tips_allow_custom()); ?>
                            <?php disabled(!$use_online); ?>>
                        <?php esc_html_e('Allow custom tip amount', 'maca-restu-pro'); ?>
                    </label>
                </td>
            </tr>
        </table>
        </div>
        <?php
    }

    /**
     * Staff screen payment visibility.
     */
    private function render_payments_staff_panel() {
        $visibility = maca_menulist_staff_payment_visibility();
        ?>
        <h2><?php esc_html_e('Staff payment info', 'maca-restu-pro'); ?></h2>
        <p class="description">
            <?php esc_html_e('When an order is paid or pending, staff can see amount, time, and payment reference on the order board.', 'maca-restu-pro'); ?>
        </p>

        <table class="form-table" role="presentation">
            <tr>
                <th scope="row"><?php esc_html_e('Show on', 'maca-restu-pro'); ?></th>
                <td>
                    <fieldset>
                        <label>
                            <input type="radio" name="maca_menulist_staff_payment_visibility" value="waiter"
                                <?php checked($visibility, 'waiter'); ?>>
                            <?php esc_html_e('Waiter page only', 'maca-restu-pro'); ?>
                        </label>
                        <br>
                        <label>
                            <input type="radio" name="maca_menulist_staff_payment_visibility" value="kitchen"
                                <?php checked($visibility, 'kitchen'); ?>>
                            <?php esc_html_e('Kitchen page only', 'maca-restu-pro'); ?>
                        </label>
                        <br>
                        <label>
                            <input type="radio" name="maca_menulist_staff_payment_visibility" value="both"
                                <?php checked($visibility, 'both'); ?>>
                            <?php esc_html_e('Both waiter and kitchen', 'maca-restu-pro'); ?>
                        </label>
                    </fieldset>
                    <p class="description">
                        <?php esc_html_e('“Mark as paid” is hidden on the waiter screen when the guest has already paid online.', 'maca-restu-pro'); ?>
                    </p>
                </td>
            </tr>
        </table>
        <?php
    }

    /**
     * Stripe and Swish provider credentials.
     *
     * @param string $webhook_stripe Stripe webhook URL.
     * @param string $webhook_swish  Swish webhook URL.
     */
    private function render_payments_providers_panel($webhook_stripe, $webhook_swish) {
        $this->render_payments_online_disabled_notice();
        $use_online = maca_menulist_payments_use_online();
        ?>
        <h2><?php esc_html_e('Payment providers', 'maca-restu-pro'); ?></h2>
        <p class="description">
            <?php esc_html_e('Enter API keys here. Use the setup guide tab for step-by-step instructions.', 'maca-restu-pro'); ?>
        </p>

        <div class="maca-payments-provider-cards" <?php echo $use_online ? '' : ' hidden'; ?>>
        <?php foreach (maca_menulist_payments_get_providers() as $provider) : ?>
            <div class="maca-payments-provider-card" id="maca-provider-<?php echo esc_attr($provider->get_id()); ?>">
                <h3><?php echo esc_html($provider->get_label()); ?></h3>
                <p class="description"><?php echo esc_html($provider->get_description()); ?></p>
                <p>
                    <a href="<?php echo esc_url(admin_url('admin.php?page=maca-restu-payments&tab=guide#' . $provider->get_id())); ?>">
                        <?php
                        printf(
                            /* translators: %s: provider name */
                            esc_html__('Open step-by-step guide for %s', 'maca-restu-pro'),
                            esc_html($provider->get_label())
                        );
                        ?>
                    </a>
                </p>

                <table class="form-table" role="presentation">
                    <?php
                    foreach ($provider->get_admin_fields() as $field) {
                        $this->render_provider_field($provider, $field);
                    }
                    ?>
                </table>

                <?php if ($provider->get_id() === 'stripe') : ?>
                    <p class="description">
                        <?php esc_html_e('Stripe webhook URL for this site:', 'maca-restu-pro'); ?>
                        <code><?php echo esc_html($webhook_stripe); ?></code>
                    </p>
                <?php elseif ($provider->get_id() === 'swish') : ?>
                    <p class="description">
                        <?php esc_html_e('Swish callback URL for this site:', 'maca-restu-pro'); ?>
                        <code><?php echo esc_html($webhook_swish); ?></code>
                    </p>
                <?php endif; ?>

                <p>
                    <?php if ($provider->is_configured()) : ?>
                        <span class="maca-payments-status maca-payments-status--ok"><?php esc_html_e('Credentials saved', 'maca-restu-pro'); ?></span>
                    <?php else : ?>
                        <span class="maca-payments-status maca-payments-status--warn"><?php esc_html_e('Not fully configured', 'maca-restu-pro'); ?></span>
                    <?php endif; ?>
                </p>
            </div>
        <?php endforeach; ?>
        </div>
        <?php
    }

    /**
     * Notice when Stripe/Swish tabs need the master online payment toggle.
     */
    private function render_payments_online_disabled_notice() {
        if (maca_menulist_payments_use_online()) {
            return;
        }

        $flow_url = admin_url('admin.php?page=maca-restu-payments&tab=flow');
        ?>
        <div class="notice notice-info inline maca-payments-online-disabled-notice">
            <p>
                <?php esc_html_e('Enable “Use online payment” under Payment flow before configuring Stripe or Swish here.', 'maca-restu-pro'); ?>
                <a href="<?php echo esc_url($flow_url); ?>"><?php esc_html_e('Go to Payment flow', 'maca-restu-pro'); ?></a>
            </p>
        </div>
        <?php
    }

    /**
     * @param Maca_Menulist_Payment_Provider $provider Provider.
     * @param array<string, mixed>           $field    Field definition.
     */
    private function render_provider_field($provider, $field) {
        $key = $field['key'];
        $type = isset($field['type']) ? $field['type'] : 'text';
        $name = 'maca_payment_' . $provider->get_id() . '_' . $key;
        $stored = maca_menulist_payments_get_setting($provider->get_id(), $key);
        $label = $field['label'];
        $description = isset($field['description']) ? $field['description'] : '';
        $online_disabled = !maca_menulist_payments_use_online();
        ?>
        <tr>
            <th scope="row"><label for="<?php echo esc_attr($name); ?>"><?php echo esc_html($label); ?></label></th>
            <td>
                <?php if ($type === 'checkbox') : ?>
                    <label>
                        <input type="checkbox" class="maca-payments-online-input" id="<?php echo esc_attr($name); ?>" name="<?php echo esc_attr($name); ?>" value="1"
                            <?php checked($stored, '1'); ?>
                            <?php disabled($online_disabled); ?>>
                        <?php echo esc_html($description); ?>
                    </label>
                <?php elseif ($type === 'password') : ?>
                    <?php
                    $secret_attrs = maca_menulist_admin_secret_input_attrs(
                        $stored !== '',
                        array(
                            'id' => $name,
                            'name' => $name,
                            'autocomplete' => isset($field['autocomplete']) ? $field['autocomplete'] : 'off',
                        )
                    );
                    ?>
                    <input class="maca-payments-online-input" <?php disabled($online_disabled); ?>
                        <?php echo maca_menulist_admin_secret_field_attr_string($secret_attrs); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
                    <?php if ($stored !== '') : ?>
                        <p class="description"><?php esc_html_e('A value is already stored. Enter a new value only to replace it.', 'maca-restu-pro'); ?></p>
                    <?php endif; ?>
                    <?php if ($description !== '') : ?>
                        <p class="description"><?php echo esc_html($description); ?></p>
                    <?php endif; ?>
                <?php else : ?>
                    <input type="text" class="regular-text maca-payments-online-input" id="<?php echo esc_attr($name); ?>" name="<?php echo esc_attr($name); ?>"
                        value="<?php echo esc_attr($stored); ?>"
                        autocomplete="<?php echo esc_attr(isset($field['autocomplete']) ? $field['autocomplete'] : 'off'); ?>"
                        <?php disabled($online_disabled); ?>>
                    <?php if ($description !== '') : ?>
                        <p class="description"><?php echo esc_html($description); ?></p>
                    <?php endif; ?>
                <?php endif; ?>
            </td>
        </tr>
        <?php
    }

    /**
     * Render markdown setup guide.
     */
    private function render_guide_tab() {
        $guide_file = maca_menulist_payments_guide_file();
        ?>
        <div class="maca-payments-guide maca-guide-content">
            <?php
            if (file_exists($guide_file)) {
                $html = maca_menulist_render_markdown_file($guide_file);
                if ($html === '') {
                    echo '<p>' . esc_html__('Guide file is empty or could not be read.', 'maca-restu-pro') . '</p>';
                } else {
                    echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
                }
            } else {
                echo '<p>' . esc_html__('Payments guide file not found.', 'maca-restu-pro') . '</p>';
            }
            ?>
        </div>
        <?php
    }
}
