<?php

namespace MovieEngine\Admin\Monetization\Tabs;

if (! defined("ABSPATH")) {
    exit; // Exit if accessed directly
}

class GeneralTab {

    public static function render($options = [], $standalone = true) {
        if (empty($options)) {
            $options = get_option('movie_engine_monetization_settings', []);
        }
?>
        <?php if ($standalone) : ?>
            <?php settings_fields('movie_engine_monetization'); ?>
            <div class="movie-engine-settings-section">
                <div class="movie-engine-section-header">
                    <h2><?php esc_html_e('General Ad Settings', 'cine-movie-engine'); ?></h2>
                    <p class="description"><?php esc_html_e('Configure global advertisement settings', 'cine-movie-engine'); ?></p>
                </div>
            <?php endif; ?>

            <div class="movie-engine-card">
                <div class="movie-engine-card-header">
                    <h3><span class="fa-solid fa-bullhorn"></span> <?php esc_html_e('Global Settings', 'cine-movie-engine'); ?></h3>
                </div>
                <div class="movie-engine-card-body">
                    <div class="movie-engine-form-group">
                        <label class="movie-engine-checkbox-label">
                            <input type="hidden" name="movie_engine_monetization_settings[enable_ads]" value="0">
                            <input type="checkbox"
                                name="movie_engine_monetization_settings[enable_ads]"
                                value="1"
                                <?php checked($options['movie_engine_enable_ads'] ?? ($options['enable_ads'] ?? 0), 1); ?>>
                            <span><?php esc_html_e('Enable All Ads', 'cine-movie-engine'); ?></span>
                        </label>
                        <p class="movie-engine-help-text"><?php esc_html_e('Master toggle to enable/disable all advertisements on the site.', 'cine-movie-engine'); ?></p>
                    </div>

                    <div class="movie-engine-form-group">
                        <label for="global_ad_script"><?php esc_html_e('Global Ad Script (Header)', 'cine-movie-engine'); ?></label>
                        <textarea id="global_ad_script"
                            name="movie_engine_monetization_settings[global_ad_script]"
                            class="movie-engine-textarea me-code-editor"
                            rows="5"
                            placeholder="<script>...</script>"><?php echo esc_textarea($options['movie_engine_global_ad_script'] ?? ($options['global_ad_script'] ?? '')); ?></textarea>
                        <p class="movie-engine-help-text"><?php esc_html_e('Script loaded on every page (e.g., Google AdSense auto-ads).', 'cine-movie-engine'); ?></p>
                    </div>

                    <div class="movie-engine-form-group">
                        <label class="movie-engine-checkbox-label">
                            <input type="hidden" name="movie_engine_monetization_settings[async_ads]" value="0">
                            <input type="checkbox"
                                name="movie_engine_monetization_settings[async_ads]"
                                value="1"
                                <?php checked($options['movie_engine_async_ads'] ?? ($options['async_ads'] ?? 0), 1); ?>>
                            <span><?php esc_html_e('Load Ads Asynchronously', 'cine-movie-engine'); ?></span>
                        </label>
                    </div>

                </div>
            </div>

            <?php if ($standalone) : ?>
                <div class="movie-engine-form-actions">
                    <?php submit_button(__('Save Changes', 'cine-movie-engine'), 'primary me-btn-primary', 'submit', false); ?>
                    <button type="button" class="button button-link me-reset-section-btn" data-tab="monetization_general"><?php esc_html_e('Reset Section to Defaults', 'cine-movie-engine'); ?></button>
                </div>
            </div>
        <?php endif; ?>
<?php
    }
}
