<?php

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

add_action('admin_init', function () {
    register_setting('wnc_settings', 'wnc_options', [
        'sanitize_callback' => 'wnc_sanitize_options',
    ]);
});

add_action('admin_enqueue_scripts', function ($hook) {
    if (!in_array($hook, ['toplevel_page_where-next', 'where-next_page_where-next-pro'], true)) {
        return;
    }

    wp_enqueue_style('wp-color-picker');
    wp_enqueue_script('wp-color-picker');

    wp_add_inline_script(
        'wp-color-picker',
        "jQuery(function($){ $('.wnc-colour-picker').wpColorPicker(); });"
    );
});

function wnc_sanitize_options($input) {
    $defaults = wnc_default_options();
    $existing = wnc_get_options();
    $output = $existing;
    $input = is_array($input) ? $input : [];
    $scope = isset($input['_form_scope']) ? sanitize_key(wp_unslash($input['_form_scope'])) : '';

    $scope_checkboxes = [
        'settings' => ['show_badge', 'archive_button_enabled'],
        'pro' => [
            'pro_auto_place',
            'pro_auto_locations',
            'pro_auto_posts',
            'pro_auto_pages',
            'pro_auto_events',
            'pro_show_desktop_qr',
            'pro_enable_analytics',
        ],
    ];

    foreach ($defaults as $key => $default) {
        $in_scope = array_key_exists($key, $input)
            || in_array($key, $scope_checkboxes[$scope] ?? [], true)
            || (
                in_array($scope, ['settings', 'connect'], true)
                && in_array($key, ['enabled_post_types', 'event_post_types'], true)
            );

        if (!$in_scope) {
            continue;
        }

        if (array_key_exists($key, $input)) {
            $value = wp_unslash($input[$key]);
        } elseif (in_array($key, ['enabled_post_types', 'event_post_types'], true)) {
            $value = [];
        } else {
            $value = $default;
        }

        if ($key === 'fallback_url') {
            $output[$key] = esc_url_raw($value);

        } elseif ($key === 'button_icon') {
            $allowed = ['journey', 'family', 'none'];
            $output[$key] = in_array($value, $allowed, true) ? $value : 'journey';

        } elseif (in_array($key, ['button_bg_colour', 'button_text_colour', 'button_border_colour'], true)) {
            $output[$key] = sanitize_hex_color($value) ?: $default;

        } elseif ($key === 'button_style') {
            $allowed = ['filled', 'outline', 'soft'];
            $output[$key] = in_array($value, $allowed, true) ? $value : 'filled';

        } elseif ($key === 'button_size' || $key === 'icon_size') {
            $allowed = ['small', 'medium', 'large'];
            $output[$key] = in_array($value, $allowed, true) ? $value : 'medium';

        } elseif (strpos($key, 'pro_auto_') === 0) {
            $output[$key] = !empty($value) ? 1 : 0;

        } elseif (in_array($key, ['pro_enable_visibility', 'pro_mobile_only', 'pro_desktop_only', 'pro_show_desktop_qr', 'pro_enable_analytics'], true)) {
            $output[$key] = !empty($value) ? 1 : 0;

        } elseif ($key === 'pro_analytics_retention_days') {
            $allowed = [0, 90, 180, 365, 730];
            $value = absint($value);
            $output[$key] = in_array($value, $allowed, true) ? $value : 365;

        } elseif ($key === 'pro_button_position') {
            $allowed = ['inline', 'sticky-bottom', 'floating-left', 'floating-right'];
            $output[$key] = in_array($value, $allowed, true) ? $value : 'inline';

        } elseif ($key === 'pro_qr_size') {
            $allowed = ['120', '180', '240'];
            $output[$key] = in_array((string) $value, $allowed, true) ? (string) $value : '180';

        } elseif ($key === 'pro_template') {
            $allowed = ['default', 'compact', 'wide', 'card'];
            $output[$key] = in_array($value, $allowed, true) ? $value : 'default';

        } elseif ($key === 'pro_device_targeting') {
            $allowed = ['all', 'mobile', 'desktop'];
            $output[$key] = in_array($value, $allowed, true) ? $value : 'all';

        } elseif ($key === 'show_badge') {
            $output[$key] = !empty($value) ? 1 : 0;

        } elseif ($key === 'badge_variant') {
            $allowed = ['colour', 'light', 'icon'];
            $output[$key] = in_array($value, $allowed, true) ? $value : 'colour';

        } elseif ($key === 'badge_position') {

    $allowed = [
        'manual',
        'after_button',
        'after_content',
        'after_post_footer',
        'footer_inline',
        'footer_center',
        'floating',
    ];

    $output[$key] = in_array($value, $allowed, true)
        ? $value
        : 'manual';
    } elseif ($key === 'archive_button_enabled') {
    $output[$key] = !empty($value) ? 1 : 0;

} elseif ($key === 'archive_button_position') {
    $allowed = ['after_excerpt', 'after_title', 'after_content'];
    $output[$key] = in_array($value, $allowed, true) ? $value : 'after_excerpt';

} elseif ($key === 'archive_button_mode') {
    $allowed = ['url', 'full'];
    $output[$key] = in_array($value, $allowed, true) ? $value : 'url';
}
    elseif (in_array($key, ['enabled_post_types', 'event_post_types'], true)) {
    $allowed_post_types = array_keys(wnc_get_public_post_types_for_settings());
    $selected = [];

    if (is_array($value)) {
        foreach ($value as $post_type) {
            $post_type = sanitize_key($post_type);

            if (in_array($post_type, $allowed_post_types, true)) {
                $selected[] = $post_type;
            }
        }
    }

    $output[$key] = array_values(array_unique($selected));
        } else {
            $output[$key] = sanitize_text_field($value);
        }
    }

    return $output;
}

if (!function_exists('wnc_human_readable_meta_key')) {
    function wnc_human_readable_meta_key($meta_key) {
        $meta_key = (string) $meta_key;
        $label = preg_replace('/^_+/', '', $meta_key);
        $label = str_replace(['_', '-'], ' ', $label);
        $label = preg_replace('/(?<!^)([A-Z])/', ' $1', $label);
        $label = preg_replace('/\s+/', ' ', $label);
        $label = trim($label);
        $label = ucwords(strtolower($label));

        $label = strtr($label, [
            'Url'  => 'URL',
            'Uri'  => 'URI',
            'Api'  => 'API',
            'Id'   => 'ID',
            'Seo'  => 'SEO',
            'Gps'  => 'GPS',
            'Qr'   => 'QR',
            'Html' => 'HTML',
        ]);

        return $label ?: $meta_key;
    }
}

function wnc_render_system_status_page() {
    if (!current_user_can('manage_options')) {
        return;
    }

    $options = wnc_get_options();
    $enabled_post_types = function_exists('wnc_get_enabled_post_types')
        ? wnc_get_enabled_post_types()
        : ['post', 'page'];

    $mapped = 0;

    foreach ($options as $key => $value) {
        if (str_ends_with($key, '_key') && !empty($value)) {
            $mapped++;
        }
    }

    ?>
    <div class="wrap">
        <h1>Where Next System Status</h1>

        <table class="widefat striped" style="max-width:900px;">
            <tbody>
                <tr>
                    <th>Plugin Version</th>
                    <td><?php echo esc_html(defined('WNC_VERSION') ? WNC_VERSION : 'Unknown'); ?></td>
                </tr>
                <tr>
                    <th>WordPress Version</th>
                    <td><?php echo esc_html(get_bloginfo('version')); ?></td>
                </tr>
                <tr>
                    <th>PHP Version</th>
                    <td><?php echo esc_html(PHP_VERSION); ?></td>
                </tr>
                <tr>
                    <th>Enabled Post Types</th>
                    <td><?php echo esc_html(implode(', ', $enabled_post_types)); ?></td>
                </tr>
                <tr>
                    <th>Fallback URL</th>
                    <td><?php echo esc_html($options['fallback_url'] ?? ''); ?></td>
                </tr>
                
                <tr>
                    <th>Mapped Fields</th>
                    <td><?php echo esc_html($mapped); ?></td>
                </tr>
                <tr>
                    <th>Badge Enabled</th>
                    <td><?php echo !empty($options['show_badge']) ? 'Yes' : 'No'; ?></td>
                </tr>
            </tbody>
        </table>
    </div>
    <?php
}

function wnc_get_public_post_types_for_settings() {
    $post_types = get_post_types(['public' => true], 'objects');

    unset($post_types['attachment']);

    uasort($post_types, function ($a, $b) {
        return strcasecmp($a->labels->name ?? $a->name, $b->labels->name ?? $b->name);
    });

    return $post_types;
}

function wnc_get_enabled_post_types() {
    $options = function_exists('wnc_get_options')
        ? wnc_get_options()
        : get_option('wnc_options', []);

    if (!empty($options['enabled_post_types']) && is_array($options['enabled_post_types'])) {
        return array_values(array_unique(array_map('sanitize_key', $options['enabled_post_types'])));
    }

    return ['post', 'page'];
}

function wnc_is_enabled_post_type($post_type = null) {
    if (!$post_type) {
        $post_type = get_post_type();
    }

    return $post_type && in_array($post_type, wnc_get_enabled_post_types(), true);
}

function wnc_get_event_post_types() {
    $options = wnc_get_options();

    if (empty($options['event_post_types']) || !is_array($options['event_post_types'])) {
        return [];
    }

    return array_values(array_intersect(
        wnc_get_enabled_post_types(),
        array_unique(array_map('sanitize_key', $options['event_post_types']))
    ));
}

function wnc_is_event_post_type($post_type = null) {
    $post_type = $post_type ?: get_post_type();

    return $post_type && in_array($post_type, wnc_get_event_post_types(), true);
}

function wnc_render_meta_key_select($key, $label, $options, $meta_keys) {
    $saved_value = $options[$key] ?? '';

    $legacy_source_labels = [
        '__post_title'     => 'Post Title',
        '__featured_image' => 'Featured Image',
        '__permalink'      => 'Permalink',
        '__post_excerpt'   => 'Post Excerpt',
        '__post_content'   => 'Post Content',
    ];

    $built_in_sources = [
        'post:title'          => 'Post Title',
        'post:permalink'      => 'Permalink',
        'post:featured_image' => 'Featured Image',
        'post:excerpt'        => 'Post Excerpt',
        'post:content'        => 'Post Content',
    ];

    $taxonomies = get_taxonomies(['public' => true], 'objects');

    $all_known_values = array_keys($built_in_sources);

    foreach ($meta_keys as $meta_key) {
        $all_known_values[] = 'meta:' . $meta_key;
    }

    foreach ($taxonomies as $taxonomy => $taxonomy_object) {
        $all_known_values[] = 'tax:' . $taxonomy;
    }
    ?>
    <tr>
        <th scope="row">
            <label for="wnc_<?php echo esc_attr($key); ?>">
                <?php echo esc_html($label); ?>
            </label>
        </th>

        <td>
            <select
                id="wnc_<?php echo esc_attr($key); ?>"
                name="wnc_options[<?php echo esc_attr($key); ?>]"
                class="regular-text"
            >
                <option value="">— Not used —</option>

                <optgroup label="Built-in WordPress fields">
                    <?php foreach ($built_in_sources as $source_value => $source_label) : ?>
                        <option value="<?php echo esc_attr($source_value); ?>" <?php selected($saved_value, $source_value); ?>>
                            <?php echo esc_html($source_label); ?>
                        </option>
                    <?php endforeach; ?>
                </optgroup>

                <?php if (!empty($taxonomies)) : ?>
                    <optgroup label="Taxonomies">
                        <?php foreach ($taxonomies as $taxonomy => $taxonomy_object) :
                            $source_value = 'tax:' . $taxonomy;
                            $source_label = $taxonomy_object->labels->singular_name ?: $taxonomy_object->label;
                            ?>
                            <option value="<?php echo esc_attr($source_value); ?>" <?php selected($saved_value, $source_value); ?>>
                                <?php echo esc_html($source_label); ?> — <?php echo esc_html($taxonomy); ?>
                            </option>
                        <?php endforeach; ?>
                    </optgroup>
                <?php endif; ?>

                <?php if (!empty($meta_keys)) : ?>
                    <optgroup label="Custom fields">
                        <?php foreach ($meta_keys as $meta_key) :
                            $source_value = 'meta:' . $meta_key;
                            ?>
                            <option value="<?php echo esc_attr($source_value); ?>" <?php selected($saved_value, $source_value); ?>>
                                <?php echo esc_html($meta_key); ?>
                            </option>
                        <?php endforeach; ?>
                    </optgroup>
                <?php endif; ?>

                <?php if (!empty($saved_value) && !in_array($saved_value, $all_known_values, true)) : ?>
                    <?php
                    $display_value = $legacy_source_labels[$saved_value] ?? $saved_value;

                    if (!isset($legacy_source_labels[$saved_value])) {
                        if (strpos($saved_value, 'meta:') === 0) {
                            $display_value = substr($saved_value, 5);
                        } elseif (strpos($saved_value, 'tax:') === 0) {
                            $display_value = 'Taxonomy: ' . substr($saved_value, 4);
                        } elseif (strpos($saved_value, 'post:') === 0) {
                            $display_value = str_replace('_', ' ', substr($saved_value, 5));
                            $display_value = ucwords($display_value);
                        }
                    }
                    ?>
                    <option value="<?php echo esc_attr($saved_value); ?>" selected>
                        <?php echo esc_html($display_value); ?> (saved source)
                    </option>
                <?php endif; ?>
            </select>
        </td>
    </tr>
    <?php
}

function wnc_render_settings_page() {
    if (!current_user_can('manage_options')) {
        return;
    }

    $options = wnc_get_options();
    $meta_keys = wnc_get_available_meta_keys();

    $fields = [
        // Core
        'name_key'                => 'Name',
        'address_key'             => 'Address',
        'postcode_key'            => 'Postcode',
        'website_key'             => 'Website',
        'booking_url_key'         => 'Booking URL',
        'parking_url_key'         => 'Pre-book Parking URL',
        'category_key'            => 'Category',
        'cover_image_key'         => 'Cover Image',

        // Pricing
        'pricing_key'             => 'Pricing Table / Summary',
        'adult_price_key'         => 'Adult Price',
        'child_price_key'         => 'Child Price',
        'family_price_key'        => 'Family Price',
        'carer_pricing_type_key'  => 'Carer Pricing Type',
        'carer_price_key'         => 'Carer Price',
        'parking_cost_key'        => 'Parking Cost',

        // Membership / opening / details
        'membership_provider_key' => 'Membership Provider',
        'opening_times_key'       => 'Opening Times',
        'monday_key'              => 'Monday Opening Times',
        'tuesday_key'             => 'Tuesday Opening Times',
        'wednesday_key'           => 'Wednesday Opening Times',
        'thursday_key'            => 'Thursday Opening Times',
        'friday_key'              => 'Friday Opening Times',
        'saturday_key'            => 'Saturday Opening Times',
        'sunday_key'              => 'Sunday Opening Times',
        'indoor_outdoor_key'      => 'Indoor / Outdoor',
        'facilities_key'          => 'Facilities',
        'parking_key'             => 'Parking Information',
        'age_suitability_key'     => 'Age Suitability',

        // Location
        'what3words_key'          => 'what3words',
        'latitude_key'            => 'Latitude',
        'longitude_key'           => 'Longitude',
        'phone_key'               => 'Phone',

        // Events
        'place_mode_key'          => 'Place Mode',
        'event_start_date_key'     => 'Event Start Date',
        'event_end_date_key'       => 'Event End Date',
    ];
    ?>
    <div class="wrap">
        <h1>Where Next Settings</h1>

        <p>Configure the button, app fallback and Where Next tag mapping used to send WordPress content into the Where Next app.</p>
        

        <form method="post" action="options.php">
            <?php settings_fields('wnc_settings'); ?>
            <input type="hidden" name="wnc_options[_form_scope]" value="settings">
            
            <h2>Enabled Post Types</h2>

<table class="form-table" role="presentation">
    <tr>
        <th scope="row">Run Where Next on</th>
        <td>
            <?php
            $enabled_post_types = wnc_get_enabled_post_types();
            $public_post_types = wnc_get_public_post_types_for_settings();
            ?>

            <?php foreach ($public_post_types as $post_type => $object) : ?>
                <label style="display:block;margin-bottom:8px;">
                    <input
                        type="checkbox"
                        name="wnc_options[enabled_post_types][]"
                        value="<?php echo esc_attr($post_type); ?>"
                        <?php checked(in_array($post_type, $enabled_post_types, true)); ?>
                    >
                    <?php echo esc_html($object->labels->name ?? $post_type); ?>
                    <code><?php echo esc_html($post_type); ?></code>
                </label>
            <?php endforeach; ?>

            <p class="description">
                Select which post types should show the Where Next meta box, output Where Next tags, and allow the Where Next button.
            </p>
        </td>
    </tr>
</table>

            <h2>Event Post Types</h2>
            <table class="form-table" role="presentation">
                <tr>
                    <th scope="row">Treat as events</th>
                    <td>
                        <?php $event_post_types = wnc_get_event_post_types(); ?>
                        <?php foreach ($public_post_types as $post_type => $object) : ?>
                            <label style="display:block;margin-bottom:8px;">
                                <input
                                    type="checkbox"
                                    name="wnc_options[event_post_types][]"
                                    value="<?php echo esc_attr($post_type); ?>"
                                    <?php checked(in_array($post_type, $event_post_types, true)); ?>
                                >
                                <?php echo esc_html($object->labels->name ?? $post_type); ?>
                                <code><?php echo esc_html($post_type); ?></code>
                            </label>
                        <?php endforeach; ?>
                        <p class="description">Selected content outputs <code>wherenext:placeMode</code> as <code>event</code> and is checked for event dates.</p>
                    </td>
                </tr>
            </table>

            <h2>Button Settings</h2>

            <table class="form-table" role="presentation">
                <tr>
    <th scope="row"><label for="wnc_fallback_url">Fallback URL</label></th>
    <td>
        <input
            id="wnc_fallback_url"
            type="url"
            name="wnc_options[fallback_url]"
            value="<?php echo esc_attr($options['fallback_url'] ?? 'https://wherenext.dev/getwherenext/'); ?>"
            class="regular-text"
            readonly
        >
        <p class="description">
            This fallback URL is currently locked while the Where Next import flow is being tested.
        </p>
    </td>
</tr>

                <tr>
                    <th scope="row"><label for="wnc_button_label">Button label</label></th>
                    <td>
                        <input id="wnc_button_label" type="text" name="wnc_options[button_label]" value="<?php echo esc_attr($options['button_label'] ?? 'Send to Where Next'); ?>" class="regular-text">
                    </td>
                </tr>

                <tr>
                    <th scope="row"><label for="wnc_button_icon">Button icon</label></th>
                    <td>
                        <select id="wnc_button_icon" name="wnc_options[button_icon]">
                            <option value="journey" <?php selected($options['button_icon'] ?? '', 'journey'); ?>>Journey Pin</option>
                            <option value="family" <?php selected($options['button_icon'] ?? '', 'family'); ?>>Family Pin</option>
                            <option value="none" <?php selected($options['button_icon'] ?? '', 'none'); ?>>Text only</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <th scope="row">Button colours</th>
                    <td>
                        <p><label>Background<br><input type="text" name="wnc_options[button_bg_colour]" value="<?php echo esc_attr($options['button_bg_colour'] ?? '#0f766e'); ?>" class="wnc-colour-picker"></label></p>
                        <p><label>Text<br><input type="text" name="wnc_options[button_text_colour]" value="<?php echo esc_attr($options['button_text_colour'] ?? '#ffffff'); ?>" class="wnc-colour-picker"></label></p>
                        <p><label>Border<br><input type="text" name="wnc_options[button_border_colour]" value="<?php echo esc_attr($options['button_border_colour'] ?? '#0f766e'); ?>" class="wnc-colour-picker"></label></p>
                    </td>
                </tr>

                <tr>
                    <th scope="row">Button style</th>
                    <td>
                        <select name="wnc_options[button_style]">
                            <option value="filled" <?php selected($options['button_style'] ?? '', 'filled'); ?>>Filled</option>
                            <option value="outline" <?php selected($options['button_style'] ?? '', 'outline'); ?>>Outline</option>
                            <option value="soft" <?php selected($options['button_style'] ?? '', 'soft'); ?>>Soft</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <th scope="row">Button size</th>
                    <td>
                        <select name="wnc_options[button_size]">
                            <option value="small" <?php selected($options['button_size'] ?? '', 'small'); ?>>Small</option>
                            <option value="medium" <?php selected($options['button_size'] ?? '', 'medium'); ?>>Medium</option>
                            <option value="large" <?php selected($options['button_size'] ?? '', 'large'); ?>>Large</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <th scope="row">Icon size</th>
                    <td>
                        <select name="wnc_options[icon_size]">
                            <option value="small" <?php selected($options['icon_size'] ?? '', 'small'); ?>>Small</option>
                            <option value="medium" <?php selected($options['icon_size'] ?? '', 'medium'); ?>>Medium</option>
                            <option value="large" <?php selected($options['icon_size'] ?? '', 'large'); ?>>Large</option>
                        </select>
                    </td>
                </tr>
            </table>
            
            <h2>Archive / Card Buttons</h2>

<table class="form-table" role="presentation">
    <tr>
        <th scope="row">Show on archive cards</th>
        <td>
            <label>
                <input
                    type="checkbox"
                    name="wnc_options[archive_button_enabled]"
                    value="1"
                    <?php checked(!empty($options['archive_button_enabled'])); ?>
                >
                Automatically add the Where Next button to archive/search excerpts.
            </label>
        </td>
    </tr>

    <tr>
        <th scope="row">Card button position</th>
        <td>
            <select name="wnc_options[archive_button_position]">
                <option value="after_excerpt" <?php selected($options['archive_button_position'] ?? '', 'after_excerpt'); ?>>After excerpt</option>
                <option value="after_title" <?php selected($options['archive_button_position'] ?? '', 'after_title'); ?>>After title</option>
                <option value="after_content" <?php selected($options['archive_button_position'] ?? '', 'after_content'); ?>>After content</option>
            </select>
        </td>
    </tr>

    <tr>
        <th scope="row">Card button mode</th>
        <td>
            <select name="wnc_options[archive_button_mode]">
                <option value="url" <?php selected($options['archive_button_mode'] ?? '', 'url'); ?>>Source URL only</option>
                <option value="full" <?php selected($options['archive_button_mode'] ?? '', 'full'); ?>>Full data</option>
            </select>
        </td>
    </tr>
</table>

            <h2>Preview</h2>
            <p>
                <span style="display:inline-flex;align-items:center;gap:10px;min-height:44px;padding:9px 18px;border-radius:999px;background:<?php echo esc_attr($options['button_bg_colour'] ?? '#0f766e'); ?>;color:<?php echo esc_attr($options['button_text_colour'] ?? '#ffffff'); ?>;border:2px solid <?php echo esc_attr($options['button_border_colour'] ?? '#0f766e'); ?>;font-weight:800;">
                    <?php $preview_icon = wnc_get_icon_url($options['button_icon'] ?? 'journey'); ?>
                    <?php if ($preview_icon) : ?>
                        <img src="<?php echo esc_url($preview_icon); ?>" alt="" style="width:24px;height:24px;object-fit:contain;">
                    <?php endif; ?>
                    <?php echo esc_html($options['button_label'] ?? 'Send to Where Next'); ?>
                </span>
            </p>

            <h2>Where Next Ready Badge</h2>

            <table class="form-table" role="presentation">
                <tr>
                    <th scope="row">Display badge</th>
                    <td>
                        <label>
                            <input
                                type="checkbox"
                                name="wnc_options[show_badge]"
                                value="1"
                                <?php checked(!empty($options['show_badge'])); ?>
                            >
                            Show “Works with Where Next” badge when the site is configured.
                        </label>
                        <p class="description">
Display the badge automatically or place it manually using
<code>[where_next_badge]</code> or
<code>&lt;?php wnc_badge(); ?&gt;</code>.
</p>
                    </td>
                </tr>

                <tr>
                    <th scope="row"><label for="wnc_badge_variant">Badge theme</label></th>
                    <td>
                        <select id="wnc_badge_variant" name="wnc_options[badge_variant]">
                            <option value="colour" <?php selected($options['badge_variant'] ?? 'colour', 'colour'); ?>>Colour</option>
                            <option value="light" <?php selected($options['badge_variant'] ?? 'colour', 'light'); ?>>Light</option>
                            <option value="icon" <?php selected($options['badge_variant'] ?? 'colour', 'icon'); ?>>Icon only</option>
                        </select>
                        <p class="description">
                            Badge image files should be added to <code>assets/badges/</code>.
                        </p>
                    </td>
                </tr>

                <tr>
                    <th scope="row"><label for="wnc_badge_position">Badge position</label></th>
                    <td>
                        <select id="wnc_badge_position" name="wnc_options[badge_position]">
                            <option value="manual" <?php selected($options['badge_position'] ?? 'manual', 'manual'); ?>>
    Manual shortcode only
</option>

<option value="after_button" <?php selected($options['badge_position'], 'after_button'); ?>>
    After Where Next button
</option>

<option value="after_content" <?php selected($options['badge_position'], 'after_content'); ?>>
    After post content
</option>

<option value="after_post_footer" <?php selected($options['badge_position'], 'after_post_footer'); ?>>
    After post footer
</option>

<option value="footer_inline" <?php selected($options['badge_position'], 'footer_inline'); ?>>
    Theme footer (inline)
</option>

<option value="footer_center" <?php selected($options['badge_position'], 'footer_center'); ?>>
    Theme footer (centred)
</option>

<option value="floating" <?php selected($options['badge_position'], 'floating'); ?>>
    Floating badge
</option>
                        </select>
                        <p class="description">
                            Manual shortcode: <code>[where_next_badge]</code>
                        </p>
                    </td>
                </tr>
            </table>

            <h2>Where Next Tag Mapping</h2>
            <p>
                Map your WordPress data sources to the Where Next tag specification.
                You can use built-in WordPress fields, custom fields, or public taxonomies.
            </p>

            <table class="form-table" role="presentation">
                <?php foreach ($fields as $key => $label) {
                    wnc_render_meta_key_select($key, $label, $options, $meta_keys);
                } ?>
            </table>

            <?php submit_button('Save Settings'); ?>
        </form>

        <h2>Shortcodes</h2>
        <p><code>[where_next_button]</code></p>
        <p><code>[where_next_badge]</code></p>
    </div>
    <?php
}
