<?php
namespace Bookaro\Controller\Admin\Settings;

defined( 'ABSPATH' ) || exit;

// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- meta_key lookup resolves the bookable provider/listing and is a bounded, indexed query.

use Bookaro\Utils\GenericPresetProfiles;

final class GeneralTab extends AbstractSettingsTab
{
    public static function get_operational_defaults_map(): array
    {
        return [
            'booking_min_notice_hours' => 0,
            'booking_max_window_days' => 0,
            'booking_buffer_before_minutes' => 0,
            'booking_buffer_after_minutes' => 0,
            'booking_cancel_window_hours' => 0,
            'booking_reschedule_window_hours' => 0,
            'auto_approve_bookings' => 0,
            'auto_complete_enabled' => 1,
            'auto_complete_delay_hours' => 0,
            'auto_expire_enabled' => 1,
            'auto_expire_delay_hours' => 0,
            'booking_spam_honeypot_enabled' => 1,
            'booking_spam_min_fill_seconds' => 4,
            'booking_spam_burst_window_seconds' => 300,
            'booking_spam_burst_ip_limit' => 8,
            'booking_spam_burst_email_limit' => 5,
            'booking_spam_duplicate_pending_hours' => 24,
            'notify_reminder' => 0,
            'notify_review_invite' => 0,
        ];
    }

    public function get_key(): string
    {
        return 'general';
    }

    public function get_section(): array
    {
        return [
            'id'    => 'bookaro_general',
            'title' => __( 'Setup', 'mybookaro' ),
            'page'  => 'bookaro-booking-settings-general',
            'desc'  => __( 'Core setup for resources, placement, and booking rules.', 'mybookaro' ),
        ];
    }

    public function get_fields(): array
    {
        self::register_pro_only_strings();
        $listing_post_types = $this->context( 'listing_post_types', [] );
        $listing_pages = $this->context( 'listing_pages', [] );
        $default_listing_types = $this->context( 'default_listing_types', [] );
        $site_type = $this->context( 'site_type', 'directory' );
        $site_type = sanitize_key( (string) $site_type );
        if ( ! in_array( $site_type, [ 'directory', 'single_business', 'advanced_mapping' ], true ) ) {
            $site_type = 'directory';
        }
        $is_single_business = ( 'single_business' === $site_type );
        $is_advanced_mapping = ( 'advanced_mapping' === $site_type );
        $is_directory = ( 'directory' === $site_type );
        $default_resource_labels = function_exists( 'bookaro_get_default_resource_labels' )
            ? (array) bookaro_get_default_resource_labels()
            : [
                'singular' => ( $is_single_business ? __( 'Business', 'mybookaro' ) : __( 'Listing', 'mybookaro' ) ),
                'plural'   => ( $is_single_business ? __( 'Services', 'mybookaro' ) : __( 'Listings', 'mybookaro' ) ),
            ];
        $default_resource_singular = sanitize_text_field( (string) ( $default_resource_labels['singular'] ?? '' ) );
        $default_resource_plural = sanitize_text_field( (string) ( $default_resource_labels['plural'] ?? '' ) );
        if ( '' === $default_resource_singular ) {
            $default_resource_singular = $is_single_business ? __( 'Business', 'mybookaro' ) : __( 'Listing', 'mybookaro' );
        }
        if ( '' === $default_resource_plural ) {
            $default_resource_plural = $is_single_business ? __( 'Services', 'mybookaro' ) : __( 'Listings', 'mybookaro' );
        }
        $resource_singular = sanitize_text_field( (string) $this->context( 'resource_label_singular', '' ) );
        $resource_plural = sanitize_text_field( (string) $this->context( 'resource_label_plural', '' ) );
        if ( '' === $resource_singular ) {
            $resource_singular = $default_resource_singular;
        }
        if ( '' === $resource_plural ) {
            $resource_plural = $default_resource_plural;
        }
        $resource_singular_lower = function_exists( 'bookaro_resource_label_to_lower' )
            ? bookaro_resource_label_to_lower( $resource_singular )
            : strtolower( $resource_singular );
        $resource_plural_lower = function_exists( 'bookaro_resource_label_to_lower' )
            ? bookaro_resource_label_to_lower( $resource_plural )
            : strtolower( $resource_plural );
        if ( '' === $resource_singular_lower ) {
            $resource_singular_lower = $resource_singular;
        }
        if ( '' === $resource_plural_lower ) {
            $resource_plural_lower = $resource_plural;
        }
        $integration_mode = $this->context( 'integration_mode', 'auto' );
        $integration_mode = sanitize_key( (string) $integration_mode );
        $resolved_mode = function_exists( 'bookaro_get_integration_mode' )
            ? bookaro_get_integration_mode()
            : $integration_mode;
        if ( 'auto' === $resolved_mode ) {
            $resolved_mode = ( function_exists( 'bookaro_is_mylisting_active' ) && bookaro_is_mylisting_active() )
                ? 'mylisting'
                : 'generic';
        }
        $generic_enabled = ( 'generic' === $resolved_mode );
        $page_options = [ '' => __( 'Use default', 'mybookaro' ) ] + $listing_pages;
        $provider_users = $this->context( 'provider_users', [] );
        $docs_url = '';
        if ( defined( 'BOOKARO_PLUGIN_FILE' ) && function_exists( 'plugins_url' ) ) {
            $docs_url = plugins_url( 'readme.txt', BOOKARO_PLUGIN_FILE );
        }
        $docs_link = $docs_url !== ''
            ? sprintf(
                '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>',
                esc_url( $docs_url ),
                esc_html__( 'Read the setup guide', 'mybookaro' )
            )
            : esc_html__( 'See readme.txt: Getting started.', 'mybookaro' );
        $listing_types_description = sprintf(
            /* translators: %1$s: dynamic string, %2$s: dynamic string, %3$s: dynamic string */
            __( 'Select %1$s post types MyBookaro should run on. Example: %2$s. %3$s', 'mybookaro' ),
            $resource_plural_lower,
            '<code>job_listing</code>, <code>listing</code>, <code>property</code>',
            $docs_link
        );
        if ( $is_single_business ) {
            $listing_types_description = sprintf(
                /* translators: %s: dynamic string */
                __( 'Optional for Single Business. Keep empty to use provider-centric defaults, or select %s post types only if you also publish provider entries.', 'mybookaro' ),
                $resource_plural_lower
            );
        }
        $allowed_pages_description = sprintf(
            /* translators: %1$s: dynamic string, %2$s: dynamic string, %3$s: dynamic string */
            __( 'Optional. When Pages are selected, limit to specific page IDs. Leave empty to allow all %1$s pages. Tip: search by ID (e.g. %2$s). %3$s', 'mybookaro' ),
            $resource_singular_lower,
            '<code>123</code>, <code>456</code>',
            $docs_link
        );

        $ui_mode_default = ( 'generic' === $resolved_mode ) ? 'shortcodes' : 'woocommerce';
        $ui_mode_options = [
            'shortcodes' => __( 'Shortcodes', 'mybookaro' ),
            'woocommerce' => __( 'WooCommerce My Account', 'mybookaro' ),
        ];
        $ui_mode_description = __( 'Choose where customers and providers manage bookings.', 'mybookaro' );
        if ( $generic_enabled ) {
            $ui_mode_options = [
                'shortcodes' => __( 'Shortcodes (recommended for Generic)', 'mybookaro' ),
                'woocommerce' => __( 'WooCommerce My Account (secondary in Generic mode)', 'mybookaro' ),
            ];
            $ui_mode_description = __( 'Recommended Generic flow: use Shortcodes first (assign pages, place blocks/shortcodes, then validate). Use WooCommerce My Account only if you intentionally want dashboards there.', 'mybookaro' );
        }

        $depends_shortcodes = [
            [ 'field' => 'ui_mode', 'operator' => 'equals', 'value' => 'shortcodes' ],
        ];
        $depends_site_type_directory = [
            [ 'field' => 'site_type', 'operator' => 'equals', 'value' => 'directory' ],
        ];
        $depends_site_type_single_business = [
            [ 'field' => 'site_type', 'operator' => 'equals', 'value' => 'single_business' ],
        ];
        $depends_site_type_advanced_mapping = [
            [ 'field' => 'site_type', 'operator' => 'equals', 'value' => 'advanced_mapping' ],
        ];
        $depends_provider_meta = [
            [ 'field' => 'provider_resolution_mode', 'operator' => 'equals', 'value' => 'meta_key' ],
        ];
        $depends_mylisting = [
            [ 'field' => 'integration_mode', 'operator' => 'equals', 'value' => 'mylisting' ],
        ];
        $mylisting_depends = ( 'mylisting' === $resolved_mode ) ? [] : $depends_mylisting;
        $depends_setup_notice = [
            [ 'field' => 'setup_notice_enabled', 'operator' => 'truthy' ],
        ];
        $depends_pages_selected = [
            [ 'field' => 'allowed_listing_post_types', 'operator' => 'contains', 'value' => 'page' ],
        ];
        $depends_generic_advanced = [
            [ 'field' => 'generic_advanced_setup_enabled', 'operator' => 'truthy' ],
        ];
        $depends_generic_standard = [
            [ 'field' => 'generic_advanced_setup_enabled', 'operator' => 'falsy' ],
            [ 'field' => 'site_type', 'operator' => 'equals', 'value' => 'directory' ],
        ];
        $default_placement_strategy = $is_single_business ? 'normal_page' : 'directory_template';
        $placement_strategy_options = [
            'directory_template' => __( 'Directory listing template', 'mybookaro' ),
            'normal_page' => __( 'Normal page', 'mybookaro' ),
            'shortcode_only' => __( 'Shortcode/block only', 'mybookaro' ),
            'css_selector' => __( 'CSS selector insertion', 'mybookaro' ),
        ];
        $generic_preset_options = [
            'author_provider' => __( 'Provider-Owned Directory (Listing Author mapping)', 'mybookaro' ),
            'meta_key_provider' => __( 'Editorial + Meta Key Directory (Listing Meta Key mapping)', 'mybookaro' ),
            'author_admin_catalog' => __( 'Shared Catalog Directory (admin-managed services)', 'mybookaro' ),
        ];
        $describe_generic_preset = static function ( string $preset ): string {
            return GenericPresetProfiles::build_selection_detail( $preset );
        };
        $depends_preset_author_provider = [
            [ 'field' => 'generic_advanced_setup_enabled', 'operator' => 'falsy' ],
            [ 'field' => 'generic_setup_preset', 'operator' => 'equals', 'value' => 'author_provider' ],
        ];
        $depends_preset_meta_key_provider = [
            [ 'field' => 'generic_advanced_setup_enabled', 'operator' => 'falsy' ],
            [ 'field' => 'generic_setup_preset', 'operator' => 'equals', 'value' => 'meta_key_provider' ],
        ];
        $depends_preset_author_admin_catalog = [
            [ 'field' => 'generic_advanced_setup_enabled', 'operator' => 'falsy' ],
            [ 'field' => 'generic_setup_preset', 'operator' => 'equals', 'value' => 'author_admin_catalog' ],
        ];
        $provider_resolution_depends = $generic_enabled ? $depends_generic_advanced : [];
        $provider_meta_depends = $depends_provider_meta;
        if ( $generic_enabled ) {
            $provider_meta_depends = [
                [ 'field' => 'generic_advanced_setup_enabled', 'operator' => 'truthy' ],
                [ 'field' => 'provider_resolution_mode', 'operator' => 'equals', 'value' => 'meta_key' ],
            ];
        }

        $integration_options = function_exists( 'bookaro_get_integration_mode_options' )
            ? bookaro_get_integration_mode_options()
            : [
                'auto' => __( 'Auto-select Directory lane', 'mybookaro' ),
                'mylisting' => __( 'MyListing Directory', 'mybookaro' ),
                'generic' => __( 'Generic Directory', 'mybookaro' ),
            ];
        if ( isset( $integration_options['auto'] ) ) {
            $integration_options['auto'] = __( 'Auto-select Directory lane', 'mybookaro' );
        }
        $site_type_options = function_exists( 'bookaro_get_site_type_options' )
            ? bookaro_get_site_type_options()
            : [
                'directory' => __( 'Directory lanes (MyListing Directory or Generic Directory)', 'mybookaro' ),
                'single_business' => __( 'Single Business', 'mybookaro' ),
            ];
        if ( $is_advanced_mapping ) {
            $site_type_options['advanced_mapping'] = __( 'Generic Directory (legacy compatibility state active)', 'mybookaro' );
        } else {
            unset( $site_type_options['advanced_mapping'] );
        }
        $integration_mode_description = __( 'Only used when Lane is set to Directory. Auto-select uses MyListing when active; otherwise Bookaro uses Generic Directory.', 'mybookaro' );
        if ( $is_single_business ) {
            $integration_mode_description = __( 'Not needed for Single Business. This setting only applies to Directory lanes.', 'mybookaro' );
        } elseif ( $is_advanced_mapping ) {
            $integration_mode_description = __( 'Your site is using a legacy Generic Directory state. Directory lane choice is handled automatically.', 'mybookaro' );
        }
        $mylisting_detected = function_exists( 'bookaro_is_mylisting_active' )
            ? bookaro_is_mylisting_active()
            : false;
        $payouts_enabled = function_exists( 'bookaro_payouts_enabled' ) && bookaro_payouts_enabled();
        $gcal_enabled = function_exists( 'bookaro_feature_enabled' ) && bookaro_feature_enabled( 'gcal' );
        $gcal_provider_notice_label = $gcal_enabled
            ? __( 'Show "Connect Google Calendar" card for providers', 'mybookaro' )
            : __( 'Show "Connect Google Calendar" card for providers', 'mybookaro' );
        $gcal_provider_notice_tooltip = $gcal_enabled
            ? __( 'Shows the Google Calendar connect card for providers.', 'mybookaro' )
            : __( 'Shows the Google Calendar connect card for providers. Google Calendar sync is part of MyBookaro Pro.', 'mybookaro' );
        $gcal_customer_notice_label = $gcal_enabled
            ? __( 'Show "Connect Google Calendar" card for customers', 'mybookaro' )
            : __( 'Show "Connect Google Calendar" card for customers', 'mybookaro' );
        $gcal_customer_notice_tooltip = $gcal_enabled
            ? __( 'Shows the Google Calendar connect card for customers.', 'mybookaro' )
            : __( 'Shows the Google Calendar connect card for customers. Google Calendar sync is part of MyBookaro Pro.', 'mybookaro' );
        $data_retention_description = __( 'Configure how long logs are retained. Set to 0 to keep indefinitely.', 'mybookaro' );

        $fields = [
            [
                'id' => 'heading_setup_essentials',
                'label' => '',
                'callback' => 'render_heading_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'Setup essentials', 'mybookaro' ),
                    'description' => __( 'Define your lane, visible resource labels, and booking UI behavior.', 'mybookaro' ),
                    'field_type' => 'heading',
                ],
            ],
            [ 'id'=>'site_type', 'label'=>__( 'Lane', 'mybookaro' ), 'callback'=>'render_select_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'options'=>$site_type_options,'default'=>'directory','description'=>__( 'Start with one lane: MyListing Directory, Single Business, or guided Generic Directory.', 'mybookaro' ),'tooltip'=>__( 'Lane controls which setup checklist Bookaro uses. Advanced Generic options are optional and stay inside Generic Directory.', 'mybookaro' ) ] ],
            [ 'id'=>'ui_mode', 'label'=>__( 'Booking UI Mode', 'mybookaro' ), 'callback'=>'render_select_field', 'section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'options'=>$ui_mode_options,'default'=>$ui_mode_default,'description'=>$ui_mode_description,'tooltip'=>__( 'Choose where customers and providers manage bookings.', 'mybookaro' ) ] ],
            [ 'id'=>'integration_mode','label'=>__( 'Directory lane choice', 'mybookaro' ), 'callback'=>'render_select_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'options'=>$integration_options,'default'=>'auto','description'=>$integration_mode_description,'tooltip'=>__( 'Directory only: choose whether the Directory lane runs as MyListing Directory or Generic Directory.', 'mybookaro' ),'depends'=>$depends_site_type_directory ] ],
            [
                'id' => 'note_integration_mode_single_business',
                'label' => '',
                'callback' => 'render_note_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'Directory lane choice is automatic here', 'mybookaro' ),
                    'description' => __( 'Single Business has its own setup path. You can ignore Directory lane choice.', 'mybookaro' ),
                    'variant' => 'info',
                    'field_type' => 'note',
                    'depends' => $depends_site_type_single_business,
                ],
            ],
            [
                'id' => 'note_integration_mode_advanced_mapping',
                'label' => '',
                'callback' => 'render_note_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'Directory lane choice is automatic here', 'mybookaro' ),
                    'description' => __( 'Generic Directory is using a legacy saved state. Keep your lane selection, and use advanced Generic options only when older setups need them.', 'mybookaro' ),
                    'variant' => 'info',
                    'field_type' => 'note',
                    'depends' => $depends_site_type_advanced_mapping,
                ],
            ],
            [
                'id' => 'resource_label_singular',
                'label' => __( 'Resource Label (Singular)', 'mybookaro' ),
                'callback' => 'render_text_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'default' => $default_resource_singular,
                    'description' => sprintf(
                        /* translators: %s: dynamic string */
                        __( 'Visible UI label for one bookable resource. Default for this lane: %s.', 'mybookaro' ),
                        $default_resource_singular
                    ),
                    'tooltip' => __( 'UI copy only. Internal fields like listing_id remain unchanged.', 'mybookaro' ),
                ],
            ],
            [
                'id' => 'resource_label_plural',
                'label' => __( 'Resource Label (Plural)', 'mybookaro' ),
                'callback' => 'render_text_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'default' => $default_resource_plural,
                    'description' => sprintf(
                        /* translators: %s: dynamic string */
                        __( 'Visible UI label for multiple resources. Default for this site type: %s.', 'mybookaro' ),
                        $default_resource_plural
                    ),
                    'tooltip' => __( 'Examples: Listings, Services, Pages. UI copy only.', 'mybookaro' ),
                ],
            ],
            ...$this->maybe_mylisting_generic_notice( $mylisting_detected ),
            /* translators: %s: dynamic string */
            [ 'id'=>'allowed_listing_post_types','label'=>sprintf( __( '%s Post Types', 'mybookaro' ), $resource_plural ), 'callback'=>'render_multiselect_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'options'=>$listing_post_types,'default'=>$default_listing_types,'size'=>6,'search_placeholder'=>__( 'Search post types...', 'mybookaro' ),'description'=>$listing_types_description,'tooltip'=>__( 'MyBookaro runs only on these post types.', 'mybookaro' ) ] ],
            [
                'id' => 'note_listing_types_required',
                'label' => '',
                'callback' => 'render_note_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    /* translators: %s: dynamic string, %1$s: dynamic string, %2$s: dynamic string */
                    'title' => sprintf( __( '%s types required', 'mybookaro' ), $resource_plural ),
                    'description' => sprintf(
                        /* translators: %1$s: dynamic string, %2$s: dynamic string */
                        __( 'Select at least one %1$s post type to enable booking UI on your %2$s.', 'mybookaro' ),
                        $resource_singular_lower,
                        $resource_plural_lower
                    ),
                    'variant' => 'warning',
                    'field_type' => 'note',
                    'depends' => [
                        [ 'field' => 'allowed_listing_post_types', 'operator' => 'falsy' ],
                        [ 'field' => 'site_type', 'operator' => 'not_equals', 'value' => 'single_business' ],
                    ],
                ],
            ],
            /* translators: %s: dynamic string */
            [ 'id'=>'allowed_listing_pages','label'=>sprintf( __( 'Allowed %s Pages', 'mybookaro' ), $resource_singular ), 'callback'=>'render_multiselect_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'options'=>$listing_pages,'default'=>[],'size'=>6,'search_placeholder'=>__( 'Search pages or IDs...', 'mybookaro' ),'description'=>$allowed_pages_description,'tooltip'=>__( 'Limit booking to specific pages when Pages are selected above.', 'mybookaro' ),'depends'=>$depends_pages_selected ] ],
            [ 'id'=>'default_provider_id','label'=>__( 'Default Provider (Single Business)', 'mybookaro' ), 'callback'=>'render_select_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'options'=>$provider_users,'default'=>'','description'=>__( 'Optional fallback when no listing/provider context is provided.', 'mybookaro' ),'tooltip'=>__( 'Helpful for single-business sites without listing context.', 'mybookaro' ) ] ],
            [
                'id' => 'note_default_provider_single_business',
                'label' => '',
                'callback' => 'render_note_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'Default Provider recommended', 'mybookaro' ),
                    'description' => __( 'Single Business works best when Default Provider is selected so booking button and services cards can run on normal pages without listing context.', 'mybookaro' ),
                    'variant' => 'warning',
                    'field_type' => 'note',
                    'depends' => [
                        [ 'field' => 'site_type', 'operator' => 'equals', 'value' => 'single_business' ],
                        [ 'field' => 'default_provider_id', 'operator' => 'falsy' ],
                    ],
                ],
            ],
            [ 'id'=>'currency_symbol','label'=>__( 'Currency Symbol', 'mybookaro' ), 'callback'=>'render_text_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>'','description'=>__( 'Optional. Blank uses the site currency (WooCommerce if available).', 'mybookaro' ),'tooltip'=>__( 'Shown in booking UI and reports.', 'mybookaro' ) ] ],
            [ 'id'=>'frontend_enabled','label'=>__( 'Frontend UI', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Enable booking form and dashboards on the frontend', 'mybookaro' ),'default'=>1,'description'=>__( 'Keep off while configuring. Booking UI stays hidden.', 'mybookaro' ),'tooltip'=>__( 'Master switch for frontend booking UI.', 'mybookaro' ) ] ],
            [ 'id'=>'require_login','label'=>__( 'Booking Access', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Require visitors to log in before booking', 'mybookaro' ),'default'=>1,'description'=>__( 'Uncheck to allow guests to book without an account.', 'mybookaro' ),'tooltip'=>__( 'When enabled, only logged-in users can open the booking form. When disabled, anonymous visitors can complete a booking (guest bookings).', 'mybookaro' ) ] ],
            [
                'id' => 'note_setup_first_run',
                'label' => '',
                'callback' => 'render_note_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'First run tip', 'mybookaro' ),
                    'description' => __( 'Start with UI mode, listing types, and the frontend toggle. Then set booking rules.', 'mybookaro' ),
                    'variant' => 'info',
                    'field_type' => 'note',
                ],
            ],
            [
                'id' => 'heading_pages_placement',
                'label' => '',
                'callback' => 'render_heading_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'Pages and placement', 'mybookaro' ),
                    'description' => __( 'Set booking pages and dashboards for Shortcodes mode.', 'mybookaro' ),
                    'field_type' => 'heading',
                    'depends' => $depends_shortcodes,
                ],
            ],
            [
                'id' => 'note_pages_shortcodes',
                'label' => '',
                'callback' => 'render_note_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'Shortcodes mode required', 'mybookaro' ),
                    'description' => __( 'Create or select pages that include the booking form and provider/customer dashboard embeds.', 'mybookaro' ),
                    'variant' => 'info',
                    'field_type' => 'note',
                    'depends' => $depends_shortcodes,
                ],
            ],
            [
                'id' => 'dashboard_pages_helper',
                'label' => '',
                'callback' => 'render_dashboard_pages_helper_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'field_type' => 'note',
                    'depends' => $depends_shortcodes,
                ],
            ],
            [ 'id'=>'booking_page_id','label'=>__( 'Booking Page Override', 'mybookaro' ), 'callback'=>'render_select_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'options'=>$page_options,'default'=>'','description'=>__( 'Optional global booking page used when no per-type override is set.', 'mybookaro' ),'tooltip'=>__( 'Only used in Shortcodes mode.', 'mybookaro' ),'depends'=>$depends_shortcodes ] ],
            [ 'id'=>'provider_dashboard_page_id','label'=>__( 'Provider Dashboard Page Override', 'mybookaro' ), 'callback'=>'render_select_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'options'=>$page_options,'default'=>'','description'=>__( 'Optional global provider dashboard page used when no per-type override is set.', 'mybookaro' ),'tooltip'=>__( 'Only used in Shortcodes mode.', 'mybookaro' ),'depends'=>$depends_shortcodes ] ],
            [ 'id'=>'customer_dashboard_page_id','label'=>__( 'Customer Dashboard Page Override', 'mybookaro' ), 'callback'=>'render_select_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'options'=>$page_options,'default'=>'','description'=>__( 'Optional global customer dashboard page used when no per-type override is set.', 'mybookaro' ),'tooltip'=>__( 'Only used in Shortcodes mode.', 'mybookaro' ),'depends'=>$depends_shortcodes ] ],
            /* translators: %s: dynamic string */
            [ 'id'=>'listing_post_type_pages','label'=>sprintf( __( '%s Page Overrides', 'mybookaro' ), $resource_singular ), 'callback'=>'render_listing_page_overrides_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'listing_post_types'=>$listing_post_types,'page_options'=>$listing_pages,'default_listing_types'=>$default_listing_types,'description'=>sprintf( __( 'Optional per-%s-type overrides for booking + dashboards.', 'mybookaro' ), $resource_singular_lower ),'tooltip'=>__( 'Useful when each listing type uses a different booking page.', 'mybookaro' ),'depends'=>$depends_shortcodes ] ],
            [
                'id' => 'heading_provider_resolution',
                'label' => '',
                'callback' => 'render_heading_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'Provider resolution', 'mybookaro' ),
                    'description' => __( 'Control how MyBookaro links listings to provider accounts.', 'mybookaro' ),
                    'field_type' => 'heading',
                    'depends' => $provider_resolution_depends,
                ],
            ],
            [ 'id'=>'provider_resolution_mode','label'=>__( 'Provider Resolution Mode', 'mybookaro' ), 'callback'=>'render_select_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'options'=>['author'=>__( 'Listing Author', 'mybookaro' ),'meta_key'=>__( 'Listing Meta Key', 'mybookaro' )],'default'=>'author','description'=>__( 'Listing Author: provider = listing post_author. Listing Meta Key: provider = user ID from listing meta, with author fallback.', 'mybookaro' ),'tooltip'=>__( 'Choose how provider ownership is determined.', 'mybookaro' ),'tooltip_example'=>__( 'Pick Listing Author when providers publish their own listings. Pick Listing Meta Key when editors/admins publish listings and provider ownership is stored in meta like provider_user_id.', 'mybookaro' ),'depends'=>$provider_resolution_depends ] ],
            [ 'id'=>'provider_meta_key','label'=>__( 'Provider Meta Key', 'mybookaro' ), 'callback'=>'render_text_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>'','description'=>__( 'Meta key that stores the provider WordPress user ID (example: provider_user_id).', 'mybookaro' ),'tooltip'=>__( 'Only used when Provider Resolution Mode is set to Listing Meta Key.', 'mybookaro' ),'tooltip_example'=>__( 'Example: if listing #123 has provider_user_id=44, MyBookaro resolves provider user #44 even when post_author is an editor/admin.', 'mybookaro' ),'depends'=>$provider_meta_depends ] ],
            [
                'id' => 'heading_mylisting',
                'label' => '',
                'callback' => 'render_heading_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'MyListing services mapping', 'mybookaro' ),
                    'description' => __( 'Map MyListing repeater fields to services.', 'mybookaro' ),
                    'field_type' => 'heading',
                    'depends' => $mylisting_depends,
                ],
            ],
            [ 'id'=>'mylisting_services_repeater_class','label'=>__( 'Services Repeater Class', 'mybookaro' ), 'callback'=>'render_text_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>'mybookaro_services','description'=>__( 'Only rows with this class become services. Example: mybookaro_services.', 'mybookaro' ),'tooltip'=>__( 'Set this in MyListing > Listing Type > Repeater Row CSS Class.', 'mybookaro' ),'depends'=>$mylisting_depends ] ],
            [
                'id' => 'heading_booking_rules',
                'label' => '',
                'callback' => 'render_heading_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'Booking rules', 'mybookaro' ),
                    'description' => __( 'Define notice windows, buffers, and cancellation rules.', 'mybookaro' ),
                    'field_type' => 'heading',
                ],
            ],
            [ 'id'=>'booking_min_notice_hours','label'=>__( 'Minimum Notice (hours, 0=off)', 'mybookaro' ), 'callback'=>'render_number_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>'0','min'=>0,'step'=>1,'tooltip'=>__( 'Require a minimum number of hours before a booking can start.', 'mybookaro' ) ] ],
            [ 'id'=>'booking_max_window_days','label'=>__( 'Maximum Booking Window (days, 0=off)', 'mybookaro' ), 'callback'=>'render_number_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>'0','min'=>0,'step'=>1,'tooltip'=>__( 'Limit how far in advance bookings can be made.', 'mybookaro' ) ] ],
            [ 'id'=>'booking_buffer_before_minutes','label'=>__( 'Buffer Before (minutes, 0=off)', 'mybookaro' ), 'callback'=>'render_number_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>'0','min'=>0,'step'=>1,'tooltip'=>__( 'Add preparation time before each booking.', 'mybookaro' ) ] ],
            [ 'id'=>'booking_buffer_after_minutes','label'=>__( 'Buffer After (minutes, 0=off)', 'mybookaro' ), 'callback'=>'render_number_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>'0','min'=>0,'step'=>1,'tooltip'=>__( 'Add cleanup time after each booking.', 'mybookaro' ) ] ],
            [ 'id'=>'booking_cancel_window_hours','label'=>__( 'Cancellation Window (hours, 0=off)', 'mybookaro' ), 'callback'=>'render_number_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>'0','min'=>0,'step'=>1,'tooltip'=>__( 'How close to start time a customer can cancel.', 'mybookaro' ) ] ],
            [ 'id'=>'booking_cancel_policy_apply_providers','label'=>__( 'Provider Cancellation Window', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Apply the cancellation window to providers too', 'mybookaro' ),'default'=>1,'description'=>__( 'Disable to let providers cancel at any time while customers still follow the window.', 'mybookaro' ),'tooltip'=>__( 'Controls whether providers are restricted by the cancellation window.', 'mybookaro' ) ] ],
            [ 'id'=>'booking_cancel_policy_text','label'=>__( 'Cancellation Policy Text', 'mybookaro' ), 'callback'=>'render_text_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>'','description'=>__( 'Optional. Overrides the cancellation policy sentence shown to customers. Use {{hours}} to insert the cancellation window (hours). Leave blank to use the default text.', 'mybookaro' ),'tooltip'=>__( 'Supports {{hours}} variable.', 'mybookaro' ) ] ],
            [ 'id'=>'booking_reschedule_window_hours','label'=>__( 'Reschedule Window (hours, 0=off)', 'mybookaro' ), 'callback'=>'render_number_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>'0','min'=>0,'step'=>1,'tooltip'=>__( 'How close to start time a customer can reschedule.', 'mybookaro' ) ] ],
            [ 'id'=>'booking_reschedule_policy_text','label'=>__( 'Reschedule Policy Text', 'mybookaro' ), 'callback'=>'render_text_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>'','description'=>__( 'Optional. Overrides the reschedule policy sentence shown to customers. Use {{hours}} to insert the reschedule window (hours). Leave blank to use the default text.', 'mybookaro' ),'tooltip'=>__( 'Supports {{hours}} variable.', 'mybookaro' ) ] ],
            [ 'id'=>'booking_phone_required','label'=>__( 'Require Phone Number', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Require phone number on the booking form', 'mybookaro' ),'default'=>1,'description'=>__( 'Disable to make phone optional for customers.', 'mybookaro' ),'tooltip'=>__( 'Useful for SMS reminders and follow-ups.', 'mybookaro' ) ] ],
            [
                'id' => 'heading_booking_spam',
                'label' => '',
                'callback' => 'render_heading_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'Spam protection', 'mybookaro' ),
                    'description' => __( 'Reduce junk booking requests without blocking normal users.', 'mybookaro' ),
                    'field_type' => 'heading',
                ],
            ],
            [ 'id'=>'booking_spam_honeypot_enabled','label'=>__( 'Honeypot Field', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Enable hidden honeypot field on the booking form', 'mybookaro' ),'default'=>1,'tooltip'=>__( 'Blocks automated bots that fill every form input.', 'mybookaro' ),'tooltip_example'=>__( 'If a bot fills the hidden "website" field, the booking is blocked before it is saved.', 'mybookaro' ) ] ],
            [ 'id'=>'booking_spam_min_fill_seconds','label'=>__( 'Minimum Form Fill Time (seconds, 0=off)', 'mybookaro' ), 'callback'=>'render_number_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>4,'min'=>0,'step'=>1,'description'=>__( 'Bookings submitted faster than this are treated as suspicious.', 'mybookaro' ),'tooltip'=>__( 'Helps catch bots that submit instantly.', 'mybookaro' ),'tooltip_example'=>__( 'Set to 4 so requests submitted in 1-2 seconds are blocked, while real users continue normally.', 'mybookaro' ) ] ],
            [ 'id'=>'booking_spam_burst_window_seconds','label'=>__( 'Burst Window (seconds)', 'mybookaro' ), 'callback'=>'render_number_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>300,'min'=>30,'step'=>30,'description'=>__( 'Time window used for per-IP and per-email burst protection.', 'mybookaro' ),'tooltip'=>__( 'Defines how long a burst window lasts.', 'mybookaro' ),'tooltip_example'=>__( 'Use 300 to apply limits over a 5-minute period.', 'mybookaro' ) ] ],
            [ 'id'=>'booking_spam_burst_ip_limit','label'=>__( 'Per-IP Burst Limit (requests/window, 0=off)', 'mybookaro' ), 'callback'=>'render_number_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>8,'min'=>0,'step'=>1,'description'=>__( 'Maximum booking requests allowed per IP inside the burst window.', 'mybookaro' ),'tooltip'=>__( 'Blocks repeated bursts from the same IP address.', 'mybookaro' ),'tooltip_example'=>__( 'Set to 8 with a 300-second window to block the 9th request from one IP within 5 minutes.', 'mybookaro' ) ] ],
            [ 'id'=>'booking_spam_burst_email_limit','label'=>__( 'Per-Email Burst Limit (requests/window, 0=off)', 'mybookaro' ), 'callback'=>'render_number_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>5,'min'=>0,'step'=>1,'description'=>__( 'Maximum booking requests allowed per customer email inside the burst window.', 'mybookaro' ),'tooltip'=>__( 'Stops rapid repeated submissions from the same email.', 'mybookaro' ),'tooltip_example'=>__( 'Set to 5 with a 300-second window to block the 6th request from the same email in 5 minutes.', 'mybookaro' ) ] ],
            [ 'id'=>'booking_spam_duplicate_pending_hours','label'=>__( 'Duplicate Pending Block Window (hours, 0=off)', 'mybookaro' ), 'callback'=>'render_number_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>24,'min'=>0,'step'=>1,'description'=>__( 'Block identical pending booking requests from the same customer during this window.', 'mybookaro' ),'tooltip'=>__( 'Prevents accidental duplicate pending requests.', 'mybookaro' ),'tooltip_example'=>__( 'Set to 24 to block a second identical pending request for the same slot within one day.', 'mybookaro' ) ] ],
            [ 'id'=>'booking_spam_blacklist_words','label'=>__( 'Blacklist Words (optional)', 'mybookaro' ), 'callback'=>'render_textarea_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>'','rows'=>5,'description'=>__( 'One word/phrase per line (or comma-separated). Requests containing these terms are blocked.', 'mybookaro' ),'tooltip'=>__( 'Use for obvious spam words or promo patterns you never want in requests.', 'mybookaro' ),'tooltip_example'=>__( 'Add terms like viagra, crypto giveaway, and telegram to block requests containing those phrases.', 'mybookaro' ) ] ],
            [
                'id' => 'heading_automation',
                'label' => '',
                'callback' => 'render_heading_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'Automation', 'mybookaro' ),
                    'description' => __( 'Let MyBookaro auto-approve, complete, or expire bookings.', 'mybookaro' ),
                    'field_type' => 'heading',
                ],
            ],
            [ 'id'=>'auto_approve_bookings','label'=>__( 'Auto-Approve Bookings', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Automatically approve new bookings and skip provider approval', 'mybookaro' ),'default'=>0,'tooltip'=>__( 'Providers will not be able to approve bookings manually.', 'mybookaro' ) ] ],
            [ 'id'=>'auto_complete_enabled','label'=>__( 'Auto-Complete Bookings', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Automatically mark approved bookings as completed after they end', 'mybookaro' ),'default'=>1,'tooltip'=>__( 'Marks finished bookings as completed without manual action.', 'mybookaro' ) ] ],
            [ 'id'=>'auto_complete_delay_hours','label'=>__( 'Auto-Complete Delay (hours, 0=immediately)', 'mybookaro' ), 'callback'=>'render_number_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>'0','min'=>0,'step'=>1,'tooltip'=>__( 'Delay completion after booking end time.', 'mybookaro' ),'depends'=>[ [ 'field' => 'auto_complete_enabled', 'operator' => 'truthy' ] ] ] ],
            [ 'id'=>'auto_expire_enabled','label'=>__( 'Auto-Expire Pending Bookings', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Automatically reject pending bookings after they pass their scheduled time', 'mybookaro' ),'default'=>1,'tooltip'=>__( 'Prevents stale pending requests.', 'mybookaro' ) ] ],
            [ 'id'=>'auto_expire_delay_hours','label'=>__( 'Auto-Expire Delay (hours, 0=immediately)', 'mybookaro' ), 'callback'=>'render_number_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>'0','min'=>0,'step'=>1,'tooltip'=>__( 'Delay expiration after booking start time.', 'mybookaro' ),'depends'=>[ [ 'field' => 'auto_expire_enabled', 'operator' => 'truthy' ] ] ] ],
            [
                'id' => 'heading_onboarding_cards',
                'label' => '',
                'callback' => 'render_heading_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'Onboarding cards', 'mybookaro' ),
                    'description' => __( 'Toggle onboarding cards shown to providers and customers.', 'mybookaro' ),
                    'field_type' => 'heading',
                ],
            ],
            [ 'id'=>'setup_notice_enabled','label'=>__( 'Show Setup Notice', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Show onboarding cards on dashboards', 'mybookaro' ),'default'=>1,'tooltip'=>__( 'Help users complete their setup.', 'mybookaro' ) ] ],
            [ 'id'=>'setup_notice_timeslots','label'=>__( 'Setup Notice: Timeslots', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Show "Set Up Timeslots" card', 'mybookaro' ),'default'=>1,'tooltip'=>__( 'Shows the timeslots setup card on dashboards.', 'mybookaro' ),'depends'=>$depends_setup_notice ] ],
            [ 'id'=>'setup_notice_services','label'=>__( 'Setup Notice: Services', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Show "Add Services" card', 'mybookaro' ),'default'=>1,'tooltip'=>__( 'Shows the services setup card on dashboards.', 'mybookaro' ),'depends'=>$depends_setup_notice ] ],
            [ 'id'=>'setup_notice_gcal_provider','label'=>__( 'Setup Notice: Google Calendar (Provider)', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>$gcal_provider_notice_label,'default'=>1,'tooltip'=>$gcal_provider_notice_tooltip,'depends'=>$depends_setup_notice ] ],
            [ 'id'=>'setup_notice_exceptions','label'=>__( 'Setup Notice: Exceptions', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Show "Exceptions" card', 'mybookaro' ),'default'=>1,'tooltip'=>__( 'Shows the exceptions setup card on dashboards.', 'mybookaro' ),'depends'=>$depends_setup_notice ] ],
            [ 'id'=>'setup_notice_gcal_customer','label'=>__( 'Setup Notice: Google Calendar (Customer)', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>$gcal_customer_notice_label,'default'=>1,'tooltip'=>$gcal_customer_notice_tooltip,'depends'=>$depends_setup_notice ] ],
            [
                'id' => 'heading_admin_data',
                'label' => '',
                'callback' => 'render_heading_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'Admin and data', 'mybookaro' ),
                    'description' => __( 'Admin tools and uninstall behavior.', 'mybookaro' ),
                    'field_type' => 'heading',
                ],
            ],
            [ 'id'=>'allow_admin_edit_bookings','label'=>__( 'Allow Admin to Edit Bookings', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Enable admin edit links on Bookings list', 'mybookaro' ),'tooltip'=>__( 'Adds direct edit links for admins in the bookings list.', 'mybookaro' ) ] ],
            [ 'id'=>'delete_data_on_uninstall','label'=>__( 'Delete Data on Uninstall', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Remove all MyBookaro data when the plugin is uninstalled', 'mybookaro' ),'default'=>0,'description'=>__( 'Keep data by default. Enable to delete all plugin data on uninstall.', 'mybookaro' ),'tooltip'=>__( 'Enable only if you want to erase all data on uninstall.', 'mybookaro' ) ] ],
            [
                'id' => 'heading_data_retention',
                'label' => '',
                'callback' => 'render_heading_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'Data retention', 'mybookaro' ),
                    'description' => $data_retention_description,
                    'field_type' => 'heading',
                ],
            ],
            [
                'id' => 'note_data_retention',
                'label' => '',
                'callback' => 'render_note_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'Retention guidance', 'mybookaro' ),
                    'description' => __( 'Shorter retention reduces stored personal data but may limit troubleshooting and audit history. Choose values that match your legal and business requirements.', 'mybookaro' ),
                    'variant' => 'info',
                    'field_type' => 'note',
                ],
            ],
            [ 'id'=>'notification_log_retention_days','label'=>__( 'Notification Log Retention (days, 0=keep)', 'mybookaro' ), 'callback'=>'render_number_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>180,'min'=>0,'step'=>1,'description'=>__( 'How long to keep email/SMS/webhook notification logs.', 'mybookaro' ),'tooltip'=>__( 'Set to 0 to keep notification logs indefinitely.', 'mybookaro' ) ] ],
            [ 'id'=>'log_file_retention_days','label'=>__( 'Log File Retention (days, 0=keep)', 'mybookaro' ), 'callback'=>'render_number_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>0,'min'=>0,'step'=>1,'description'=>__( 'How long to keep the file log in uploads/bookaro/bookaro.log.', 'mybookaro' ),'tooltip'=>__( 'Set to 0 to keep the log file until manual deletion.', 'mybookaro' ) ] ],
            [ 'id'=>'webhook_queue_retention_days','label'=>__( 'Webhook Queue Retention (days, 0=keep)', 'mybookaro' ), 'callback'=>'render_number_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'default'=>30,'min'=>0,'step'=>1,'description'=>__( 'How long to keep queued webhook delivery attempts.', 'mybookaro' ),'tooltip'=>__( 'Set to 0 to keep webhook queues indefinitely.', 'mybookaro' ) ] ],
        ];

        $payout_notice_index = array_search( 'setup_notice_exceptions', array_column( $fields, 'id' ), true );
        if ( false !== $payout_notice_index ) {
            $payout_notice_index += 1;
            if ( $payouts_enabled ) {
                $payout_field = [
                    'id' => 'setup_notice_payouts',
                    'label' => __( 'Setup Notice: Payouts', 'mybookaro' ),
                    'callback' => 'render_checkbox_field',
                    'section' => 'bookaro_general',
                    'page' => 'bookaro-booking-settings-general',
                    'args' => [
                        'label' => __( 'Show "Set Up Payouts" card', 'mybookaro' ),
                        'default' => 1,
                        'tooltip' => __( 'Shows the payouts setup card on dashboards.', 'mybookaro' ),
                        'depends' => $depends_setup_notice,
                    ],
                ];
                array_splice( $fields, $payout_notice_index, 0, [ $payout_field ] );
            } else {
                $payout_note = [
                    'id' => 'note_setup_notice_payouts_disabled',
                    'label' => '',
                    'callback' => 'render_note_field',
                    'section' => 'bookaro_general',
                    'page' => 'bookaro-booking-settings-general',
                    'args' => [
                        'title' => __( 'Payouts notice is disabled', 'mybookaro' ),
                        'description' => __( 'Enable payouts in the Payments tab to show the payouts setup card to providers.', 'mybookaro' ),
                        'variant' => 'info',
                        'field_type' => 'note',
                        'depends' => $depends_setup_notice,
                    ],
                ];
                array_splice( $fields, $payout_notice_index, 0, [ $payout_note ] );
            }
        }

        if ( $generic_enabled && ! $is_single_business ) {
            $generic_setup_fields = [
                [
                    'id' => 'note_generic_guided_mapper',
                    'label' => '',
                    'callback' => 'render_note_field',
                    'section' => 'bookaro_general',
                    'page' => 'bookaro-booking-settings-general',
                    'args' => [
                        'title' => __( 'Generic guided setup (4 decisions)', 'mybookaro' ),
                        'description' => __( 'Complete Generic setup in order: 1) site shape, 2) provider ownership mapping, 3) services source, 4) placement strategy, then assign required pages and run readiness validation.', 'mybookaro' ),
                        'variant' => 'info',
                        'field_type' => 'note',
                        'depends' => $depends_site_type_directory,
                    ],
                ],
                [ 'id'=>'generic_setup_preset','label'=>__( 'Guided setup path', 'mybookaro' ), 'callback'=>'render_select_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'options'=>$generic_preset_options,'default'=>'author_provider','description'=>__( 'Choose the path that matches your directory ownership model, then confirm mapping, Services Source, placement, required pages, and readiness.', 'mybookaro' ),'tooltip'=>__( 'Path-first setup: choose a guided path before changing low-level mapping controls.', 'mybookaro' ),'tooltip_example'=>__( 'Provider-Owned Directory: providers author listings. Editorial + Meta Key Directory: editors author listings and provider_user_id-style meta stores ownership. Shared Catalog Directory: listing author mapping + shared admin-managed service catalog.', 'mybookaro' ),'depends'=>$depends_generic_standard ] ],
                [ 'id'=>'generic_advanced_setup_enabled','label'=>__( 'Generic compatibility preset (advanced)', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Enable Generic advanced compatibility controls', 'mybookaro' ),'default'=>0,'description'=>__( 'Keep this off for guided-path setup. Turn it on only when legacy compatibility or power-user mapping/services/placement controls are required.', 'mybookaro' ),'tooltip'=>__( 'Advanced Generic compatibility controls expose raw fields that can override guided-path defaults.', 'mybookaro' ),'depends'=>$depends_site_type_directory ] ],
                [
                    'id' => 'note_generic_advanced_enabled',
                    'label' => '',
                    'callback' => 'render_note_field',
                    'section' => 'bookaro_general',
                    'page' => 'bookaro-booking-settings-general',
                    'args' => [
                        'title' => __( 'Generic advanced compatibility controls are active', 'mybookaro' ),
                        'description' => __( 'Raw provider mapping, services source, and placement controls are now visible below and can override guided-path defaults. Existing saved values are preserved.', 'mybookaro' ),
                        'variant' => 'warn',
                        'field_type' => 'note',
                        'depends' => $depends_generic_advanced,
                    ],
                ],
                [
                    'id' => 'note_generic_standard_defaults',
                    'label' => '',
                    'callback' => 'render_note_field',
                    'section' => 'bookaro_general',
                    'page' => 'bookaro-booking-settings-general',
                    'args' => [
                        'title' => __( 'Guided path defaults are active', 'mybookaro' ),
                        'description' => __( 'New Generic setups use safer defaults: Listing Author provider mapping, Provider Only services source, and Auto-Insert after content. Existing custom values are preserved, so review mapping if your ownership model uses meta keys.', 'mybookaro' ),
                        'variant' => 'info',
                        'field_type' => 'note',
                        'depends' => $depends_generic_standard,
                    ],
                ],
                [
                    'id' => 'note_generic_preset_author_provider',
                    'label' => '',
                    'callback' => 'render_note_field',
                    'section' => 'bookaro_general',
                    'page' => 'bookaro-booking-settings-general',
                    'args' => [
                        'title' => __( 'Guided path: Provider-Owned Directory', 'mybookaro' ),
                        'description' => $describe_generic_preset( 'author_provider' ),
                        'variant' => 'info',
                        'field_type' => 'note',
                        'depends' => $depends_preset_author_provider,
                    ],
                ],
                [
                    'id' => 'note_generic_preset_meta_key_provider',
                    'label' => '',
                    'callback' => 'render_note_field',
                    'section' => 'bookaro_general',
                    'page' => 'bookaro-booking-settings-general',
                    'args' => [
                        'title' => __( 'Guided path: Editorial + Meta Key Directory', 'mybookaro' ),
                        'description' => $describe_generic_preset( 'meta_key_provider' ),
                        'variant' => 'info',
                        'field_type' => 'note',
                        'depends' => $depends_preset_meta_key_provider,
                    ],
                ],
                [
                    'id' => 'note_generic_preset_author_admin_catalog',
                    'label' => '',
                    'callback' => 'render_note_field',
                    'section' => 'bookaro_general',
                    'page' => 'bookaro-booking-settings-general',
                    'args' => [
                        'title' => __( 'Guided path: Shared Catalog Directory', 'mybookaro' ),
                        'description' => $describe_generic_preset( 'author_admin_catalog' ),
                        'variant' => 'info',
                        'field_type' => 'note',
                        'depends' => $depends_preset_author_admin_catalog,
                    ],
                ],
            ];
            $generic_service_fields = [
                [ 'id'=>'services_model','label'=>__( 'Services source', 'mybookaro' ), 'callback'=>'render_select_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'options'=>['provider_only'=>__( 'Provider Only', 'mybookaro' ),'admin_only'=>__( 'Admin Catalog Only', 'mybookaro' ),'hybrid'=>__( 'Hybrid', 'mybookaro' )],'default'=>'provider_only','description'=>__( 'Choose how services are sourced in Generic mode. Provider Only uses listing-specific services. Admin Catalog uses shared services and listing-specific admin services. Hybrid merges catalog + provider services.', 'mybookaro' ),'tooltip'=>__( 'Decision 3 in the Generic guided setup.', 'mybookaro' ),'tooltip_example'=>__( 'Provider Only example: each provider creates services from their dashboard. Admin Catalog example: you publish one shared service list and providers cannot add custom services.', 'mybookaro' ),'depends'=>$depends_generic_advanced ] ],
                [ 'id'=>'provider_services_enabled','label'=>__( 'Provider Services', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Allow providers to manage services from their dashboard', 'mybookaro' ),'default'=>1,'tooltip'=>__( 'Enables the Services tab for providers in Generic mode.', 'mybookaro' ),'depends'=>$depends_generic_advanced ] ],
                [ 'id'=>'allow_provider_custom_services','label'=>__( 'Hybrid: Provider Custom Services', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Allow providers to create custom services (hybrid only)', 'mybookaro' ),'default'=>0,'tooltip'=>__( 'Allow providers to add their own services in hybrid mode.', 'mybookaro' ),'depends'=>$depends_generic_advanced ] ],
                [ 'id'=>'allow_provider_override_catalog','label'=>__( 'Hybrid: Provider Overrides', 'mybookaro' ), 'callback'=>'render_checkbox_field','section'=>'bookaro_general','page'=>'bookaro-booking-settings-general','args'=>[ 'label'=>__( 'Allow providers to override catalog service price/duration (hybrid only)', 'mybookaro' ),'default'=>0,'tooltip'=>__( 'Allow providers to override catalog prices in hybrid mode.', 'mybookaro' ),'depends'=>$depends_generic_advanced ] ],
            ];
            $insert_after = array_search( 'default_provider_id', array_column( $fields, 'id' ), true );
            if ( false !== $insert_after ) {
                array_splice( $fields, $insert_after + 1, 0, array_merge( $generic_setup_fields, $generic_service_fields ) );
            } else {
                $fields = array_merge( $fields, $generic_setup_fields, $generic_service_fields );
            }
        } elseif ( $generic_enabled && $is_single_business ) {
            $insert_after = array_search( 'default_provider_id', array_column( $fields, 'id' ), true );
            $single_business_note = [
                'id' => 'note_single_business_defaults',
                'label' => '',
                'callback' => 'render_note_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'Single Business defaults are active', 'mybookaro' ),
                    'description' => __( 'Single Business lane defaults are active. Set a Default Provider, assign your booking page, and create at least one service to launch without listings.', 'mybookaro' ),
                    'variant' => 'info',
                    'field_type' => 'note',
                ],
            ];
            if ( false !== $insert_after ) {
                array_splice( $fields, $insert_after + 1, 0, [ $single_business_note ] );
            } else {
                $fields[] = $single_business_note;
            }
        }

        if ( $generic_enabled ) {
            $strategy_description = $is_single_business
                ? __( 'Single Business default is Normal page. Use this to place booking UI on normal pages without listing context, keep shortcode-only placement, or use CSS selector insertion when needed.', 'mybookaro' )
                : __( 'Choose where booking UI should appear in Generic mode: directory template, normal page, shortcode/block only, or CSS selector insertion.', 'mybookaro' );
            $generic_auto_insert_fields = [
                [
                    'id' => 'booking_placement_strategy',
                    'label' => __( 'Placement strategy', 'mybookaro' ),
                    'callback' => 'render_select_field',
                    'section' => 'bookaro_general',
                    'page' => 'bookaro-booking-settings-general',
                    'args' => [
                        'options' => $placement_strategy_options,
                        'default' => $default_placement_strategy,
                        'description' => $strategy_description,
                        'tooltip' => __( 'Use this first. It controls whether placement is listing-template, normal-page, shortcode-only, or CSS selector based.', 'mybookaro' ),
                        'tooltip_example' => __( 'Single Business: choose Normal page and set Default Provider. Directory: keep Directory listing template unless you intentionally place UI on standalone pages.', 'mybookaro' ),
                    ],
                ],
                [
                    'id' => 'auto_insert_booking_button',
                    'label' => __( 'Auto-Insert position', 'mybookaro' ),
                    'callback' => 'render_select_field',
                    'section' => 'bookaro_general',
                    'page' => 'bookaro-booking-settings-general',
                    'args' => [
                        'options' => [
                            'before_content' => __( 'Before content', 'mybookaro' ),
                            'after_content' => __( 'After content', 'mybookaro' ),
                        ],
                        'default' => 'after_content',
                        'description' => __( 'Used for Directory template and Normal page strategies.', 'mybookaro' ),
                        'tooltip' => __( 'Controls where the auto-inserted Book Now button appears.', 'mybookaro' ),
                        'depends' => [
                            [ 'field' => 'booking_placement_strategy', 'operator' => 'not_equals', 'value' => 'shortcode_only' ],
                            [ 'field' => 'booking_placement_strategy', 'operator' => 'not_equals', 'value' => 'css_selector' ],
                        ],
                    ],
                ],
                [
                    'id' => 'auto_insert_booking_button_selector',
                    'label' => __( 'Auto-Insert CSS Selector', 'mybookaro' ),
                    'callback' => 'render_text_field',
                    'section' => 'bookaro_general',
                    'page' => 'bookaro-booking-settings-general',
                    'args' => [
                        'default' => '',
                        'description' => __( 'Used when Placement strategy is CSS selector insertion. Example: .listing-cta or #book-now-anchor. The button is inserted after the first matching element.', 'mybookaro' ),
                        'tooltip' => __( 'CSS selector used to find where the button should be inserted.', 'mybookaro' ),
                        'depends' => [
                            [ 'field' => 'booking_placement_strategy', 'operator' => 'equals', 'value' => 'css_selector' ],
                        ],
                    ],
                ],
            ];

            $insert_after = array_search( 'frontend_enabled', array_column( $fields, 'id' ), true );
            if ( false !== $insert_after ) {
                array_splice( $fields, $insert_after + 1, 0, $generic_auto_insert_fields );
            } else {
                $fields = array_merge( $fields, $generic_auto_insert_fields );
            }
        }

        return $fields;
    }

    /**
     * Pro-only strings kept in Lite for translations.
     */
    private static function register_pro_only_strings(): void
    {
        __( 'Chat Retention (days, 0=keep)', 'mybookaro' );
        __( 'How long to keep chat messages and threads.', 'mybookaro' );
        __( 'Set to 0 to keep chat history indefinitely.', 'mybookaro' );
    }

    private function maybe_mylisting_generic_notice( bool $mylisting_detected ): array
    {
        if ( ! $mylisting_detected ) {
            return [];
        }

        return [
            [
                'id' => 'note_mylisting_generic_mode',
                'label' => '',
                'callback' => 'render_note_field',
                'section' => 'bookaro_general',
                'page' => 'bookaro-booking-settings-general',
                'args' => [
                    'title' => __( 'Using Generic mode with MyListing', 'mybookaro' ),
                    'description' => __( 'Switching to Generic Directory lane disables MyListing repeater mapping. Confirm listing types, lane choice, and Generic lane settings before launch.', 'mybookaro' ),
                    'variant' => 'warning',
                    'field_type' => 'note',
                    'depends' => [
                        [ 'field' => 'integration_mode', 'operator' => 'equals', 'value' => 'generic' ],
                    ],
                ],
            ],
        ];
    }
}
