<?php
/**
 * ZUTALW Admin Display Settings
 * Handles the display and saving of general display options for the lucky wheel.
 */

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

class ZUTALW_Admin_Display {

    /**
     * Constructor: Hooks into admin initialization.
     */
    public function __construct() {
        add_action( 'admin_init', array( $this, 'register_settings' ) );
    }

    /**
     * Registers plugin settings in the WordPress options table.
     * FIX: Added sanitize_callback to all register_setting calls to satisfy Plugin Check.
     */
    public function register_settings() {
        // --- 1. REGISTER SETTINGS WITH SANITIZATION ---
        
        // Game Title
        register_setting( 'zutalw_display_options_group', 'zutalw_game_title', array(
            'type'              => 'string',
            'sanitize_callback' => 'sanitize_text_field',
            'default'           => 'Zuta Lucky Wheel'
        ));

        // Popup Delay (Integer)
        register_setting( 'zutalw_display_options_group', 'zutalw_popup_delay', array(
            'type'              => 'integer',
            'sanitize_callback' => 'absint',
            'default'           => 0
        ));

        // Max Spins (Integer)
        register_setting( 'zutalw_display_options_group', 'zutalw_max_spins', array(
            'type'              => 'integer',
            'sanitize_callback' => 'absint',
            'default'           => 1
        ));

        // Reset Days (Integer)
        register_setting( 'zutalw_display_options_group', 'zutalw_reset_days', array(
            'type'              => 'integer',
            'sanitize_callback' => 'absint',
            'default'           => 1
        ));
        
        // Recaptcha Keys (Strings)
        register_setting( 'zutalw_display_options_group', 'zutalw_recaptcha_site_key', array(
            'type'              => 'string',
            'sanitize_callback' => 'sanitize_text_field',
            'default'           => ''
        ));

        register_setting( 'zutalw_display_options_group', 'zutalw_recaptcha_secret_key', array(
            'type'              => 'string',
            'sanitize_callback' => 'sanitize_text_field',
            'default'           => ''
        ));

        // Multiple Campaigns Mode (String)
        register_setting( 'zutalw_display_options_group', 'zutalw_multiple_campaigns_mode', array(
            'type'              => 'string',
            'sanitize_callback' => 'sanitize_text_field',
            'default'           => 'latest'
        ));
    }

    /**
     * Renders the Display Settings page content.
     */
    public function render() {
        // Handle form submission securely
        if ( isset( $_POST['zutalw_save_display'] ) && check_admin_referer( 'zutalw_save_display_nonce' ) ) {
            
            // --- 2. SAVE SETTINGS MANUALLY (SECURITY DOUBLE CHECK) ---
            
            if ( isset( $_POST['zutalw_game_title'] ) ) {
                update_option( 'zutalw_game_title', sanitize_text_field( wp_unslash( $_POST['zutalw_game_title'] ) ) );
            }

            // Numeric values
            $delay = isset( $_POST['zutalw_popup_delay'] ) ? intval( $_POST['zutalw_popup_delay'] ) : 0;
            update_option( 'zutalw_popup_delay', $delay );

            $max_spins = isset( $_POST['zutalw_max_spins'] ) ? intval( $_POST['zutalw_max_spins'] ) : 1;
            update_option( 'zutalw_max_spins', $max_spins );

            $reset_days = isset( $_POST['zutalw_reset_days'] ) ? intval( $_POST['zutalw_reset_days'] ) : 1;
            if ( $reset_days < 1 ) { $reset_days = 1; }
            update_option( 'zutalw_reset_days', $reset_days );

            // API Keys
            if ( isset( $_POST['zutalw_recaptcha_site_key'] ) ) {
                update_option( 'zutalw_recaptcha_site_key', sanitize_text_field( wp_unslash( $_POST['zutalw_recaptcha_site_key'] ) ) );
            }
            if ( isset( $_POST['zutalw_recaptcha_secret_key'] ) ) {
                update_option( 'zutalw_recaptcha_secret_key', sanitize_text_field( wp_unslash( $_POST['zutalw_recaptcha_secret_key'] ) ) );
            }

            // Multiple Campaigns Mode
            if ( isset( $_POST['zutalw_multiple_campaigns_mode'] ) ) {
                $mode = sanitize_text_field( wp_unslash( $_POST['zutalw_multiple_campaigns_mode'] ) );
                if ( in_array( $mode, array( 'latest', 'random' ) ) ) {
                    update_option( 'zutalw_multiple_campaigns_mode', $mode );
                }
            }

            echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'Settings saved.', 'zuta-lucky-wheel' ) . '</p></div>';
        }

        // --- 3. RETRIEVE CURRENT VALUES ---
        $current_title = get_option( 'zutalw_game_title', 'Zuta Lucky Wheel' );

        $current_delay = get_option( 'zutalw_popup_delay', 0 );
        $current_max_spins = get_option( 'zutalw_max_spins', 1 );
        $current_reset_days = get_option( 'zutalw_reset_days', 1 );

        $site_key = get_option( 'zutalw_recaptcha_site_key', '' );
        $secret_key = get_option( 'zutalw_recaptcha_secret_key', '' );
        
        $multiple_mode = get_option( 'zutalw_multiple_campaigns_mode', 'latest' );
        ?>
        <div class="zutalw-admin-display-wrap" style="padding: 20px; background: #fff; border: 1px solid #ccd0d4; margin-top: 20px; max-width: 600px;">
            
            <form method="post" action="">
                <?php wp_nonce_field( 'zutalw_save_display_nonce' ); ?>
                
                <h3><?php echo esc_html__( 'General Display Settings', 'zuta-lucky-wheel' ); ?></h3>
                <table class="form-table">
                    <tr valign="top">
                        <th scope="row">
                            <label for="zutalw_multiple_campaigns_mode">
                                <?php echo esc_html__( 'Multiple Campaigns Mode', 'zuta-lucky-wheel' ); ?>
                            </label>
                        </th>
                        <td>
                            <select id="zutalw_multiple_campaigns_mode" name="zutalw_multiple_campaigns_mode">
                                <option value="latest" <?php selected( $multiple_mode, 'latest' ); ?>>
                                    <?php echo esc_html__( 'Show Latest Campaign', 'zuta-lucky-wheel' ); ?>
                                </option>
                                <option value="random" <?php selected( $multiple_mode, 'random' ); ?>>
                                    <?php echo esc_html__( 'Randomize (A/B Testing)', 'zuta-lucky-wheel' ); ?>
                                </option>
                            </select>
                            <p class="description">
                                <?php echo esc_html__( 'Choose how to display the popup when multiple active campaigns target the same page.', 'zuta-lucky-wheel' ); ?>
                            </p>
                        </td>
                    </tr>
                </table>

                <hr style="margin: 20px 0; border: 0; border-top: 1px solid #eee;">

                <h3><?php echo esc_html__( 'Google reCAPTCHA v3 Protection', 'zuta-lucky-wheel' ); ?></h3>
                <table class="form-table">
                    <tr valign="top">
                        <th scope="row"><label for="zutalw_recaptcha_site_key">Site Key</label></th>
                        <td>
                            <input type="text" id="zutalw_recaptcha_site_key" name="zutalw_recaptcha_site_key" value="<?php echo esc_attr( $site_key ); ?>" class="regular-text" />
                        </td>
                    </tr>
                    <tr valign="top">
                        <th scope="row"><label for="zutalw_recaptcha_secret_key">Secret Key</label></th>
                        <td>
                            <input type="password" id="zutalw_recaptcha_secret_key" name="zutalw_recaptcha_secret_key" value="<?php echo esc_attr( $secret_key ); ?>" class="regular-text" />
                            <p class="description"><a href="https://www.google.com/recaptcha/admin/create" target="_blank"><?php echo esc_html__( 'Get Keys Here (Select v3)', 'zuta-lucky-wheel' ); ?></a></p>
                        </td>
                    </tr>
                </table>
                
                <p class="submit">
                    <input type="submit" name="zutalw_save_display" id="submit" class="button button-primary" value="<?php echo esc_attr__( 'Save Changes', 'zuta-lucky-wheel' ); ?>">
                </p>
            </form>
        </div>
        <?php
    }
}