<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

/**
 * --------------------------------------------------------------------------
 *  Load merged image settings based on current AI provider
 *  - sc_wordai_get_current_image_settings() should already be provider-aware
 *    (similar to API/Content), returning one $data array.
 * --------------------------------------------------------------------------
 */
$data = SftcyWordaiBase::sc_wordai_get_current_image_settings();
$is_basic_tier = class_exists( 'SftcyWordaiFeatures' ) && SftcyWordaiFeatures::wordai_is_basic_tier();
$upgrade_url   = class_exists( 'SftcyWordaiFeatures' ) ? SftcyWordaiFeatures::wordai_get_upgrade_url() : 'https://softcoy.com/products/wordai-pro/checkout';
$image_limits  = class_exists( 'SftcyWordaiFeatures' ) ? SftcyWordaiFeatures::wordai_get_feature_limits( 'image_generation' ) : array();
$locked_suffix = ' (Pro ' . html_entity_decode( '&#128274;', ENT_QUOTES, 'UTF-8' ) . ')';
$max_images    = ! empty( $image_limits['maxImages'] ) ? max( 1, (int) $image_limits['maxImages'] ) : 1;
// echo '<pre>';
// print_r( $data );
// echo '</pre>';

/**
 * Header template:
 *  - Renders global WordAI header
 *  - Also sets $current_provider = SftcyWordaiBase::sc_wordai_get_current_ai_provider();
 */
include_once SFTCY_WORDAI_PLUGIN_DIR . 'admin/views/settings-pages-header-template.php';

/**
 * Make sure $current_provider exists (if not set for some reason, default to openai)
 */
if ( ! isset( $current_provider ) ) {
    $current_provider = SftcyWordaiBase::sc_wordai_get_current_ai_provider();
}

/**
 * --------------------------------------------------------------------------
 *  OpenAI-specific row visibility (per image model)
 * --------------------------------------------------------------------------
 */
$openai_image_model = isset( $data['sc-wordai-openai-image-model-slug'] )
    ? $data['sc-wordai-openai-image-model-slug']
    : SftcyWordaiOpenai::$IMAGE_MODEL;

$openai_gpt_image_models = [ 'gpt-image-2', 'gpt-image-1', 'gpt-image-1-mini', 'gpt-image-1.5' ];
$row_show_hide_gptimage1 = ( $current_provider === 'openai' && in_array( $openai_image_model, $openai_gpt_image_models, true ) )
    ? ''
    : 'display: none;';


/**
 * --------------------------------------------------------------------------
 *  Gemini-specific helpers (initial values)
 * --------------------------------------------------------------------------
 * - These keys will be provided by sc_wordai_get_current_image_settings()
 *   for the google provider, similar to OpenAI.
 */
$gemini_image_model = isset( $data['sc-wordai-gemini-image-model-slug'] )
    ? $data['sc-wordai-gemini-image-model-slug']
    : SftcyWordaiGoogleGeminiApi::$IMAGE_MODEL;

// Currently we show the same Gemini rows for all models, but later
// you can switch on $gemini_image_model if needed.

/**
 * --------------------------------------------------------------------------
 *  Decide which AJAX actions to use for Save/Reset based on provider
 * --------------------------------------------------------------------------
 */
$save_action  = SftcyWordaiBase::sc_wordai_get_provider_ajax_action( 'image', $current_provider, 'save' );
$reset_action = SftcyWordaiBase::sc_wordai_get_provider_ajax_action( 'image', $current_provider, 'reset' );

?>
<div id="openai-image-settings-div-wrapper" class="">
    <h3 style="padding: 15px;">
        <span class="sc-wordai-settings-icon dashicons dashicons-admin-settings"></span>
        <?php esc_html_e( 'Image Settings', 'wordai-pro' ); ?>
    </h3>

    <!-- 💎 Minimal Mode Toggle -->
    <div class="sc-wordai-compact-toggle">
        <label>
            <input type="checkbox" id="scWordaiCompactToggle" />
            <span>💎 Minimal Mode</span>
        </label>
    </div>

    <?php if ( $is_basic_tier ) : ?>
        <div class="notice notice-info inline wordai-tier-inline-notice">
            <p>
                <strong><?php esc_html_e( 'WordAI Free keeps image generation to 1 image per request.', 'wordai' ); ?></strong>
                <?php esc_html_e( 'Higher image counts stay visible in the same screen but remain locked for Pro.', 'wordai' ); ?>
                <a class="button button-small wordai-tier-inline-cta" href="<?php echo esc_url( $upgrade_url ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Get Pro', 'wordai' ); ?></a>
            </p>
        </div>
    <?php endif; ?>

    <?php if ( ! SftcyWordaiBase::sc_wordai_provider_supports( 'image', $current_provider ) ) : ?>
        <div class="notice notice-warning inline" style="margin: 0 15px 15px;">
            <p>
                <?php
                printf(
                    esc_html__( '%s does not currently support native image generation from its Claude API key. To generate images in WordAI, switch the global AI provider to OpenAI, Google Gemini, or xAI Grok.', 'wordai-pro' ),
                    SftcyWordaiBase::sc_wordai_get_provider_label( $current_provider )
                );
                ?>
            </p>
        </div>
    <?php else : ?>
    <!--
        Main Image Settings Form
        - Single ID for all providers
        - data-* used by unified JS (similar to API/Content)
    -->
    <form
        id="scwordai-image-settings-form"
        method="post"
        data-provider="<?php echo esc_attr( $current_provider ); ?>"
        data-save-action="<?php echo esc_attr( $save_action ); ?>"
        data-reset-action="<?php echo esc_attr( $reset_action ); ?>"
    >
        <table style="border-spacing: 15px;">
            <tbody>

                <!-- 🔹 Section 1: Model Selection -->
                <tr>
                    <td colspan="2">
                        <h4 class="wordai-section-divider">
                            <?php esc_html_e( '🎨 Model Selection', 'wordai-pro' ); ?>
                        </h4>
                    </td>
                </tr>

                <!-- OpenAI Image Model Dropdown (OpenAI Provider Only) -->
                <tr
                    class="wordai-openai-image-settings-row"
                    style="<?php echo ( $current_provider === 'openai' ) ? '' : 'display:none;'; ?>"
                >
                    <td>
                        <h4><?php esc_html_e( 'OpenAI Image Model', 'wordai-pro' ); ?></h4>
                        <?php
                            // $openai_image_models = SftcyWordaiOpenai::openai_image_models();
                            // echo '<select id="sc-wordai-openai-image-model-slug" name="sc-wordai-openai-image-model-slug">';
                            // foreach ( $openai_image_models as $slug => $label ) {
                            //     $selected = isset( $data['sc-wordai-openai-image-model-slug'] )
                            //         ? selected( $data['sc-wordai-openai-image-model-slug'], $slug, false )
                            //         : selected( $slug, SftcyWordaiOpenai::$IMAGE_MODEL, false );
                            //     echo '<option value="' . esc_attr( $slug ) . '" ' . esc_attr( $selected ) . '>' . esc_html( $label ) . '</option>';
                            // }
                            // echo '</select>';
                        ?>
                        <?php
                        $openai_image_models = SftcyWordaiOpenai::openai_image_models();

                        echo '<select id="sc-wordai-openai-image-model-slug" name="sc-wordai-openai-image-model-slug">';

                        foreach ( $openai_image_models as $key => $val ) {

                            if ( is_array( $val ) ) {

                                echo '<optgroup label="' . esc_attr( $key ) . '">';

                                foreach ( $val as $slug => $label ) {
                                    $selected = isset( $data['sc-wordai-openai-image-model-slug'] )
                                        ? selected( $data['sc-wordai-openai-image-model-slug'], $slug, false )
                                        : selected( $slug, SftcyWordaiOpenai::$IMAGE_MODEL, false );

                                    echo '<option value="' . esc_attr( $slug ) . '" ' . $selected . '>' . esc_html( $label ) . '</option>';
                                }

                                echo '</optgroup>';
                                continue;
                            }

                            $slug  = $key;
                            $label = $val;

                            $selected = isset( $data['sc-wordai-openai-image-model-slug'] )
                                ? selected( $data['sc-wordai-openai-image-model-slug'], $slug, false )
                                : selected( $slug, SftcyWordaiOpenai::$IMAGE_MODEL, false );

                            echo '<option value="' . esc_attr( $slug ) . '" ' . $selected . '>' . esc_html( $label ) . '</option>';
                        }

                        echo '</select>';
                        ?>

                    </td>
                    <td>
                        <p class="sc-wordai-settings-text-hints">
                            <?php esc_html_e(
                                'Choose which GPT Image model to use for OpenAI image generation. GPT Image 1.5 is the default for instant generation, while ChatGPT Images 2.0 / GPT Image 2 remains available for verified organizations.',
                                'wordai-pro'
                            ); ?>
                        </p>
                        <p class="sc-wordai-settings-text-hints">
                            <small>
                                <?php
                                    $link_html = SftcyWordaiBase::make_links_clickable(
                                        'https://platform.openai.com/settings/organization/general|OpenAI Organization Settings'
                                    );

                                    printf(
                                        wp_kses(
                                            __( 'Your organization must be verified to use GPT Image models. To verify, visit %s — verification may take up to 15 minutes.', 'wordai-pro' ),
                                            [ 'a' => [ 'href' => [], 'target' => [], 'rel' => [] ] ]
                                        ),
                                        $link_html
                                    );
                                ?>
                            </small>
                        </p>
                    </td>
                </tr>

                <!-- Google Gemini Image Model Dropdown (Google Provider Only) -->
                <tr
                    class="wordai-google-image-settings-row"
                    style="<?php echo ( $current_provider === 'google' ) ? '' : 'display:none;'; ?>"
                >
                    <td>
                        <h4><?php esc_html_e( 'Gemini / Imagen Image Model', 'wordai-pro' ); ?></h4>
                        <?php 
                            // $gemini_image_models = SftcyWordaiGoogleGeminiSettings::gemini_image_models();
                            // echo '<select id="sc-wordai-gemini-image-model-slug" name="sc-wordai-gemini-image-model-slug">';
                            // foreach ( $gemini_image_models as $slug => $label ) {
                            //     $selected = isset( $data['sc-wordai-gemini-image-model-slug'] )
                            //         ? selected( $data['sc-wordai-gemini-image-model-slug'], $slug, false )
                            //         : selected( $slug, SftcyWordaiGoogleGeminiApi::$IMAGE_MODEL, false );
                            //     echo '<option value="' . esc_attr( $slug ) . '" ' . esc_attr( $selected ) . '>' . esc_html( $label ) . '</option>';
                            // }
                            // echo '</select>';
                        ?>
                        <?php
                        $gemini_image_models = SftcyWordaiGoogleGeminiSettings::gemini_image_models();

                        echo '<select id="sc-wordai-gemini-image-model-slug" name="sc-wordai-gemini-image-model-slug">';

                        foreach ( $gemini_image_models as $key => $val ) {

                            // 🧩 Grouped models (optgroup)
                            if ( is_array( $val ) ) {

                                echo '<optgroup label="' . esc_attr( $key ) . '">';

                                foreach ( $val as $slug => $label ) {

                                    $selected = isset( $data['sc-wordai-gemini-image-model-slug'] )
                                        ? selected( $data['sc-wordai-gemini-image-model-slug'], $slug, false )
                                        : selected( $slug, SftcyWordaiGoogleGeminiApi::$IMAGE_MODEL, false );

                                    echo '<option value="' . esc_attr( $slug ) . '" ' . $selected . '>'
                                        . esc_html( $label ) .
                                    '</option>';
                                }

                                echo '</optgroup>';
                                continue;
                            }

                            // 🧱 Flat fallback (future-proof)
                            $slug  = $key;
                            $label = $val;

                            $selected = isset( $data['sc-wordai-gemini-image-model-slug'] )
                                ? selected( $data['sc-wordai-gemini-image-model-slug'], $slug, false )
                                : selected( $slug, SftcyWordaiGoogleGeminiApi::$IMAGE_MODEL, false );

                            echo '<option value="' . esc_attr( $slug ) . '" ' . $selected . '>'
                                . esc_html( $label ) .
                            '</option>';
                        }

                        echo '</select>';
                        ?>

                    </td>
                    <td>
                        <p class="sc-wordai-settings-text-hints">
                            <?php esc_html_e(
                                'Select which Gemini image model to use. Model availability can depend on your Gemini project quota, free-tier limits, or temporary provider-side capacity.',
                                'wordai-pro'
                            ); ?>
                        </p>
                    </td>
                </tr>

                <!-- xAI Grok Image Model Dropdown -->
                <tr
                    class="wordai-xai-image-settings-row"
                    style="<?php echo ( $current_provider === 'xai' ) ? '' : 'display:none;'; ?>"
                >
                    <td>
                        <h4><?php esc_html_e( 'xAI Grok Image Model', 'wordai-pro' ); ?></h4>
                        <?php
                        $xai_image_models = SftcyWordaiXaiSettings::xai_image_models();
                        $selected_xai_image_model = SftcyWordaiXaiSettings::normalize_xai_image_model_slug(
                            $data['sc-wordai-xai-image-model-slug'] ?? ''
                        );

                        echo '<select id="sc-wordai-xai-image-model-slug" name="sc-wordai-xai-image-model-slug">';
                        foreach ( $xai_image_models as $group_label => $models ) {
                            echo '<optgroup label="' . esc_attr( $group_label ) . '">';
                            foreach ( $models as $slug => $label ) {
                                echo '<option value="' . esc_attr( $slug ) . '" ' . selected( $selected_xai_image_model, $slug, false ) . '>' . esc_html( $label ) . '</option>';
                            }
                            echo '</optgroup>';
                        }
                        echo '</select>';
                        ?>
                    </td>
                    <td>
                        <p class="sc-wordai-settings-text-hints">
                            <?php esc_html_e( 'Use Grok Imagine for xAI image generation. WordAI stores generated images in the media gallery with the same thumbnail and featured-image workflow used by other providers.', 'wordai-pro' ); ?>
                        </p>
                    </td>
                </tr>

                <!-- 🔹 Section 2: Generation Settings (Shared) -->
                <tr>
                    <td colspan="2">
                        <h4 class="wordai-section-divider">
                            <?php esc_html_e( '🖼️ Image Generation Settings', 'wordai-pro' ); ?>
                        </h4>
                    </td>
                </tr>

                <!-- How many images (shared for all providers) -->
                <tr>
                    <td>
                        <h4><?php esc_html_e( 'How Many Image(s)', 'wordai-pro' ); ?></h4>
                        <?php
                            $image_numbers = [];
                            for ( $i = 1; $i <= 50; $i++ ) {
                                $image_numbers[] = $i;
                            }
                            echo '<select id="sc-wordai-image-number" name="sc-wordai-image-number">';
                            foreach ( $image_numbers as $num ) {
                                $is_locked = $is_basic_tier && $num > $max_images;
                                $selected = isset( $data['sc-wordai-image-number'] )
                                    ? selected( $data['sc-wordai-image-number'], $num, false )
                                    : selected( $num, $max_images, false );
                                echo '<option value="' . esc_attr( $num ) . '" ' . esc_attr( $selected ) . disabled( $is_locked, true, false ) . '>' . esc_html( (string) $num . ( $is_locked ? $locked_suffix : '' ) ) . '</option>';
                            }
                            echo '</select>';
                        ?>
                    </td>
                    <td>
                        <p class="sc-wordai-settings-text-hints">
                            <?php esc_html_e(
                                'Set the number of images to generate per request. Multiple outputs allow you to choose the best visual result.',
                                'wordai-pro'
                            ); ?>
                        </p>
                    </td>
                </tr>


                <tr class="wordai-xai-image-settings-row" style="<?php echo ( $current_provider === 'xai' ) ? '' : 'display:none;'; ?>">
                    <td>
                        <h4><?php esc_html_e( 'Grok Image Aspect Ratio', 'wordai-pro' ); ?></h4>
                        <?php
                        $xai_ratios = SftcyWordaiXaiSettings::xai_image_aspect_ratios();
                        $selected_xai_ratio = SftcyWordaiXaiSettings::normalize_xai_image_aspect_ratio( $data['sc-wordai-xai-image-aspect-ratio'] ?? '' );
                        echo '<select id="sc-wordai-xai-image-aspect-ratio" name="sc-wordai-xai-image-aspect-ratio">';
                        foreach ( $xai_ratios as $ratio => $label ) {
                            echo '<option value="' . esc_attr( $ratio ) . '" ' . selected( $selected_xai_ratio, $ratio, false ) . '>' . esc_html( $label ) . '</option>';
                        }
                        echo '</select>';
                        ?>
                    </td>
                    <td>
                        <p class="sc-wordai-settings-text-hints">
                            <?php esc_html_e( 'Choose the visual shape for Grok images. Auto lets xAI select the best ratio from your prompt.', 'wordai-pro' ); ?>
                        </p>
                    </td>
                </tr>

                <tr class="wordai-xai-image-settings-row" style="<?php echo ( $current_provider === 'xai' ) ? '' : 'display:none;'; ?>">
                    <td>
                        <h4><?php esc_html_e( 'Grok Image Resolution', 'wordai-pro' ); ?></h4>
                        <?php
                        $xai_resolutions = SftcyWordaiXaiSettings::xai_image_resolutions();
                        $selected_xai_resolution = SftcyWordaiXaiSettings::normalize_xai_image_resolution( $data['sc-wordai-xai-image-resolution'] ?? '' );
                        echo '<select id="sc-wordai-xai-image-resolution" name="sc-wordai-xai-image-resolution">';
                        foreach ( $xai_resolutions as $resolution => $label ) {
                            echo '<option value="' . esc_attr( $resolution ) . '" ' . selected( $selected_xai_resolution, $resolution, false ) . '>' . esc_html( $label ) . '</option>';
                        }
                        echo '</select>';
                        ?>
                    </td>
                    <td>
                        <p class="sc-wordai-settings-text-hints">
                            <?php esc_html_e( 'Choose 1k for faster generation or 2k when you need a sharper image for larger placements.', 'wordai-pro' ); ?>
                        </p>
                    </td>
                </tr>

				<tr class="wordai-google-image-settings-row" style="<?php echo ($current_provider === 'google') ? '' : 'display:none;'; ?>">
					<td>
						<h4><?php esc_html_e('Gemini Image Format', 'wordai-pro'); ?></h4>
						<?php
							$gemini_mimes = SftcyWordaiGoogleGeminiSettings::gemini_image_mime_types( $gemini_image_model );
							$selected_gemini_mime = SftcyWordaiGoogleGeminiSettings::normalize_gemini_image_mime_type(
								$data['sc-wordai-gemini-image-mime'] ?? '',
								$gemini_image_model
							);
							echo '<select id="sc-wordai-gemini-image-mime" name="sc-wordai-gemini-image-mime">';
							foreach ($gemini_mimes as $mime => $label) {
								$selected = selected($selected_gemini_mime, $mime, false);

								echo '<option value="'.esc_attr($mime).'" '.$selected.'>'.esc_html($label).'</option>';
							}
							echo '</select>';
						?>
					</td>
					<td>
						<p class="sc-wordai-settings-text-hints">
							<?php esc_html_e('Gemini 3 Image models currently return JPEG through the Gemini Interactions API. Legacy Gemini image models can keep broader format choices when selected.', 'wordai-pro'); ?>
						</p>
					</td>
				</tr>


                <!-- 🔸 GPT Image Options (OpenAI only) -->
                <tr
                    class="sc-wordai-gptimage1-row wordai-openai-image-settings-row"
                    style="<?php echo esc_attr( $row_show_hide_gptimage1 ); ?>"
                >
                    <td>
                        <h4><?php esc_html_e( 'GPT Image Size', 'wordai-pro' ); ?></h4>
                        <?php
                            $image_sizes = [ '1024x1024', '1536x1024', '1024x1536' ];
                            echo '<select id="sc-wordai-gptimage1-image-size" name="sc-wordai-gptimage1-image-size">';
                            foreach ( $image_sizes as $size ) {
                                $selected = isset( $data['sc-wordai-gptimage1-image-size'] )
                                    ? selected( $data['sc-wordai-gptimage1-image-size'], $size, false )
                                    : selected( $size, '1024x1024', false );
                                echo '<option value="' . esc_attr( $size ) . '" ' . esc_attr( $selected ) . '>' . esc_html( $size ) . '</option>';
                            }
                            echo '</select>';
                        ?>
                    </td>
                    <td>
                        <p class="sc-wordai-settings-text-hints">
                            <?php esc_html_e(
                                'Current OpenAI GPT Image models in WordAI use the same modern settings panel. Choose square, landscape, or portrait output based on your editor layout and content goal.',
                                'wordai-pro'
                            ); ?>
                        </p>
                    </td>
                </tr>

                <tr
                    class="sc-wordai-gptimage1-row wordai-openai-image-settings-row"
                    style="<?php echo esc_attr( $row_show_hide_gptimage1 ); ?>"
                >
                    <td>
                        <h4><?php esc_html_e( 'GPT Image Format', 'wordai-pro' ); ?></h4>
                        <?php
                            $openai_formats = SftcyWordaiOpenai::openai_image_output_formats();
                            $selected_openai_format = isset( $data['sc-wordai-gptimage1-output-format'] ) ? sanitize_key( $data['sc-wordai-gptimage1-output-format'] ) : 'png';
                            if ( ! isset( $openai_formats[ $selected_openai_format ] ) ) {
                                $selected_openai_format = 'png';
                            }

                            echo '<select id="sc-wordai-gptimage1-output-format" name="sc-wordai-gptimage1-output-format">';
                            foreach ( $openai_formats as $format => $label ) {
                                echo '<option value="' . esc_attr( $format ) . '" ' . selected( $selected_openai_format, $format, false ) . '>' . esc_html( $label ) . '</option>';
                            }
                            echo '</select>';
                        ?>
                    </td>
                    <td>
                        <p class="sc-wordai-settings-text-hints">
                            <?php esc_html_e( 'Choose the output file format for OpenAI GPT Image models. PNG gives highest fidelity, JPEG gives smaller files, and WEBP balances quality with modern compression.', 'wordai-pro' ); ?>
                        </p>
                    </td>
                </tr>

                <tr
                    class="sc-wordai-gptimage1-row wordai-openai-image-settings-row"
                    style="<?php echo esc_attr( $row_show_hide_gptimage1 ); ?>"
                >
                    <td>
                        <h4><?php esc_html_e( 'GPT Image Quality', 'wordai-pro' ); ?></h4>
                        <?php
                            $image_qualities = [ 'auto', 'high', 'medium', 'low' ];
                            echo '<select id="sc-wordai-gptimage1-image-hd-quality" name="sc-wordai-gptimage1-image-hd-quality">';
                            foreach ( $image_qualities as $q ) {
                                $selected = isset( $data['sc-wordai-gptimage1-image-hd-quality'] )
                                    ? selected( $data['sc-wordai-gptimage1-image-hd-quality'], $q, false )
                                    : selected( $q, 'auto', false );
                                echo '<option value="' . esc_attr( $q ) . '" ' . esc_attr( $selected ) . '>' . esc_html( ucfirst( $q ) ) . '</option>';
                            }
                            echo '</select>';
                        ?>
                    </td>
                    <td>
                        <p class="sc-wordai-settings-text-hints">
                            <?php esc_html_e(
                                'Use Auto for balanced results. High gives stronger detail and polish, while Medium and Low reduce latency and cost for faster generation cycles.',
                                'wordai-pro'
                            ); ?>
                        </p>
                    </td>
                </tr>

                <!-- 🔸 Gemini Image Options (Google provider only) -->
                <tr
                    class="wordai-google-image-settings-row"
                    style="<?php echo ( $current_provider === 'google' ) ? '' : 'display:none;'; ?>"
                >
                    <td>
                        <h4><?php esc_html_e( 'Gemini Image Size', 'wordai-pro' ); ?></h4>
                        <?php
                            $gemini_sizes = SftcyWordaiGoogleGeminiSettings::gemini_image_sizes();
                            echo '<select id="sc-wordai-gemini-image-size" name="sc-wordai-gemini-image-size">';
                            foreach ( $gemini_sizes as $size_slug => $label ) {
                                $selected = isset( $data['sc-wordai-gemini-image-size'] )
                                    ? selected( $data['sc-wordai-gemini-image-size'], $size_slug, false )
                                    : selected( $size_slug, '1024x1024', false );
                                echo '<option value="' . esc_attr( $size_slug ) . '" ' . esc_attr( $selected ) . '>' . esc_html( $label ) . '</option>';
                            }
                            echo '</select>';
                        ?>
                    </td>
                    <td>
                        <p class="sc-wordai-settings-text-hints">
                            <?php esc_html_e(
                                'Configure the output resolution for Gemini images. Larger resolutions provide more detail but may take longer to generate.',
                                'wordai-pro'
                            ); ?>
                        </p>
                    </td>
                </tr>

                <tr
                    class="wordai-google-image-settings-row"
                    style="<?php echo ( $current_provider === 'google' ) ? '' : 'display:none;'; ?>"
                >
                    <td>
                        <h4><?php esc_html_e( 'Gemini Image Style', 'wordai-pro' ); ?></h4>
                        <?php
                            $gemini_styles_grouped = SftcyWordaiGoogleGeminiSettings::gemini_image_styles_grouped();
                            $free_gemini_style_allowance = 2;
                            $free_gemini_allowed_styles  = array();
                            if ( $is_basic_tier ) {
                                foreach ( $gemini_styles_grouped as $styles ) {
                                    $free_gemini_allowed_styles = array_merge(
                                        $free_gemini_allowed_styles,
                                        array_slice( array_keys( (array) $styles ), 0, $free_gemini_style_allowance )
                                    );
                                }
                            }
                            $selected_gemini_style = $data['sc-wordai-gemini-image-style'] ?? 'photorealistic';
                            if ( $is_basic_tier && ! in_array( $selected_gemini_style, $free_gemini_allowed_styles, true ) ) {
                                $selected_gemini_style = 'photorealistic';
                            }

                            echo '<select id="sc-wordai-gemini-image-style" name="sc-wordai-gemini-image-style">';

                            foreach ( $gemini_styles_grouped as $group_label => $styles ) {

                                echo '<optgroup label="' . esc_attr( $group_label ) . '">';

                                foreach ( $styles as $slug => $label ) {

                                    $is_locked_style = $is_basic_tier && ! in_array( $slug, $free_gemini_allowed_styles, true );
                                    $selected = selected( $selected_gemini_style, $slug, false );
                                    $label_text = $label . ( $is_locked_style ? ' (Pro)' : '' );

                                    echo '<option value="' . esc_attr( $slug ) . '" ' . $selected . disabled( $is_locked_style, true, false ) . '>' . esc_html( $label_text ) . '</option>';
                                }

                                echo '</optgroup>';
                            }

                            echo '</select>';
                        ?>
                    </td>
                    <td>
                        <p class="sc-wordai-settings-text-hints">
                            <?php esc_html_e(
                                'Choose a default visual style for Gemini images, such as photorealistic, illustration, anime, or 3D.',
                                'wordai-pro'
                            ); ?>
                        </p>
                    </td>
                </tr>

                <tr
                    class="wordai-google-image-settings-row"
                    style="<?php echo ( $current_provider === 'google' ) ? '' : 'display:none;'; ?>"
                >
                    <td>
                        <h4><?php esc_html_e( 'Gemini Image Quality', 'wordai-pro' ); ?></h4>
                        <?php
                            $gemini_qualities = SftcyWordaiGoogleGeminiSettings::gemini_image_qualities();
                            echo '<select id="sc-wordai-gemini-image-quality" name="sc-wordai-gemini-image-quality">';
                            foreach ( $gemini_qualities as $slug => $label ) {
                                $selected = isset( $data['sc-wordai-gemini-image-quality'] )
                                    ? selected( $data['sc-wordai-gemini-image-quality'], $slug, false )
                                    : selected( $slug, 'standard', false );
                                echo '<option value="' . esc_attr( $slug ) . '" ' . esc_attr( $selected ) . '>' . esc_html( $label ) . '</option>';
                            }
                            echo '</select>';
                        ?>
                    </td>
                    <td>
                        <p class="sc-wordai-settings-text-hints">
                            <?php esc_html_e(
                                'Standard quality works well for most content. Use High for premium visuals, or Fast for speed-focused, lower-cost generation.',
                                'wordai-pro'
                            ); ?>
                        </p>
                    </td>
                </tr>

                <!-- 🔹 Section 3: Output Options (Shared) -->
                <tr>
                    <td colspan="2">
                        <h4 class="wordai-section-divider">
                            <?php esc_html_e( '💾 Output & Save Options', 'wordai-pro' ); ?>
                        </h4>
                    </td>
                </tr>

                <tr>
                    <td>
                        <label for="scWordaiImageSaveToGalleryChkbox">
                            <h4><?php esc_html_e( 'Save to Media Gallery', 'wordai-pro' ); ?></h4>
                            <input
                                type="checkbox"
                                name="sc-wordai-imagesave-togallery"
                                id="scWordaiImageSaveToGalleryChkbox"
                                <?php checked( $data['sc-wordai-imagesave-togallery'] ?? 1, 1 ); ?>
                            />
                        </label>
                    </td>
                    <td>
                        <p class="sc-wordai-settings-text-hints">
                            <?php esc_html_e(
                                'Automatically save all generated images to your WordPress Media Library.',
                                'wordai-pro'
                            ); ?>
                        </p>
                    </td>
                </tr>

                <tr>
                    <td>
                        <label for="scWordaiImageSetAsFeatureChkbox">
                            <h4><?php esc_html_e( 'Set as Featured Image', 'wordai-pro' ); ?></h4>
                            <input
                                type="checkbox"
                                name="sc-wordai-set-feature-image"
                                id="scWordaiImageSetAsFeatureChkbox"
                                <?php checked( $data['sc-wordai-set-feature-image'] ?? 1, 1 ); ?>
                            />
                        </label>
                    </td>
                    <td>
                        <p class="sc-wordai-settings-text-hints">
                            <?php esc_html_e(
                                'Assign the first generated image as the featured image of your post, page, or product.',
                                'wordai-pro'
                            ); ?>
                        </p>
                    </td>
                </tr>

                <!-- 🔹 Section 4: Save Controls -->
                <tr>
                    <td colspan="2" style="text-align:center;">
                        <h4 class="wordai-section-divider">
                            <?php esc_html_e( '💾 Save & Reset', 'wordai-pro' ); ?>
                        </h4>
                        <button type="submit" class="button button-primary button-large">
                            <i class="fa-regular fa-hard-drive"></i>
                            <?php esc_html_e( 'Save Image Settings', 'wordai-pro' ); ?>
                        </button>
                        &nbsp;&nbsp;
                        <button
                            type="button"
                            class="button button-primary button-large scwordai-image-settings-form-reset-data-btn"
                        >
                            <i class="fa-regular fa-window-restore"></i>
                            <?php esc_html_e( 'Reset Settings', 'wordai-pro' ); ?>
                        </button>
                        <p class="sc-wordai-api-settings-msg"></p>
                    </td>
                </tr>

            </tbody>
        </table>
    </form>
    <?php endif; ?>
</div>

<?php
include_once SFTCY_WORDAI_PLUGIN_DIR . 'admin/views/settings-pages-footer-template.php';
?>
