<?php

namespace MovieEngine\Admin\Monetization\Tabs;

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

class SlotsTab {

    public static function render($options = [], $standalone = true) {
        if (empty($options)) {
            $options = get_option('movie_engine_monetization_settings', []);
        }

        $slots = [
            'header' => esc_html__('Header (Site-wide)', 'cine-movie-engine'),
            'footer' => esc_html__('Footer (Site-wide)', 'cine-movie-engine'),
        ];

        $pro_slots = [
            'player_top' => esc_html__('Player Area (Top Banner)', 'cine-movie-engine'),
            'player_bottom' => esc_html__('Player Area (Bottom Banner)', 'cine-movie-engine'),
            'before_content' => esc_html__('Before Content (Single Item)', 'cine-movie-engine'),
            'after_content' => esc_html__('After Content (Single Item)', 'cine-movie-engine'),
        ];

        $slot_help = [
            'header' => esc_html__('Recommended sizes: 728x90, 970x90, 970x250 (Desktop) / 320x50, 320x100 (Mobile)', 'cine-movie-engine'),
            'footer' => esc_html__('Recommended sizes: 728x90, 970x90 (Desktop) / 320x50 (Mobile)', 'cine-movie-engine'),
            'before_content' => esc_html__('Recommended sizes: 728x90 (Desktop) / 300x250 (Mobile)', 'cine-movie-engine'),
            'after_content' => esc_html__('Recommended sizes: 728x90, 336x280 (Desktop) / 300x250 (Mobile)', 'cine-movie-engine'),
            'player_top' => esc_html__('Recommended sizes: 728x90, 970x90 (Desktop) / 320x50 (Mobile)', 'cine-movie-engine'),
            'player_bottom' => esc_html__('Recommended sizes: 728x90, 970x90 (Desktop) / 320x50 (Mobile)', 'cine-movie-engine'),
        ];

        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('Ad Slots Manager', 'cine-movie-engine'); ?></h2>
                    <p class="description"><?php esc_html_e('Manage ad positions across your site', 'cine-movie-engine'); ?></p>
                </div>
            <?php endif; ?>

            <?php foreach ($slots as $key => $label) :
                self::render_slot_card($key, $label, $options, $slot_help);
            endforeach; ?>

            <?php foreach ($pro_slots as $key => $label) :
                if (has_action('movie_engine_render_pro_ad_slot')) {
                    do_action('movie_engine_render_pro_ad_slot', $key, $label, $options, $slot_help);
                } else {
                    self::render_pro_stub($key, $label);
                }
            endforeach; ?>

            <?php if ($standalone) : ?>
                <div class="movie-engine-form-actions">
                    <?php submit_button(esc_html__('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_slots"><?php esc_html_e('Reset Section to Defaults', 'cine-movie-engine'); ?></button>
                </div>
            </div>
        <?php endif; ?>
    <?php
    }

    public static function render_slot_card($key, $label, $options, $slot_help = []) {
        $pre_key = 'movie_engine_' . $key;
        $opt_enable = $options[$pre_key . '_enable'] ?? ($options[$key . '_enable'] ?? 0);
        $opt_code = $options[$pre_key . '_code'] ?? ($options[$key . '_code'] ?? '');
        $opt_code_mobile = $options[$pre_key . '_code_mobile'] ?? ($options[$key . '_code_mobile'] ?? '');
        $opt_desktop = $options[$pre_key . '_desktop'] ?? ($options[$key . '_desktop'] ?? 1);
        $opt_mobile = $options[$pre_key . '_mobile'] ?? ($options[$key . '_mobile'] ?? 1);
    ?>
        <div class="movie-engine-card me-ad-slot-card">
            <div class="movie-engine-card-header">
                <h3><span class="fa-solid fa-table-columns"></span> <?php echo esc_html($label); ?></h3>
                <label class="movie-engine-switch">
                    <input type="hidden"
                        name="movie_engine_monetization_settings[<?php echo esc_attr($key); ?>_enable]"
                        value="0">
                    <input type="checkbox"
                        name="movie_engine_monetization_settings[<?php echo esc_attr($key); ?>_enable]"
                        value="1"
                        <?php checked($opt_enable, 1); ?>>
                    <span class="movie-engine-slider round"></span>
                </label>
            </div>
            <div class="movie-engine-card-body">
                <div class="movie-engine-form-group">
                    <label><?php esc_html_e('Desktop Ad Code', 'cine-movie-engine'); ?></label>
                    <textarea name="movie_engine_monetization_settings[<?php echo esc_attr($key); ?>_code]"
                        class="movie-engine-textarea me-code-editor"
                        rows="4"
                        placeholder="<!-- Paste your desktop ad code here -->"><?php echo esc_textarea($opt_code); ?></textarea>
                    <?php if (isset($slot_help[$key])) : ?>
                        <p class="movie-engine-help-text"><?php echo esc_html($slot_help[$key]); ?></p>
                    <?php endif; ?>
                </div>

                <div class="movie-engine-form-group">
                    <label><?php esc_html_e('Mobile Ad Code (Optional)', 'cine-movie-engine'); ?></label>
                    <textarea name="movie_engine_monetization_settings[<?php echo esc_attr($key); ?>_code_mobile]"
                        class="movie-engine-textarea me-code-editor"
                        rows="3"
                        placeholder="<!-- Optional: Paste different code for mobile. If empty, desktop code will be used. -->"><?php echo esc_textarea($opt_code_mobile); ?></textarea>
                </div>
                <div class="movie-engine-form-row">
                    <div class="movie-engine-form-group me-col-6">
                        <label class="movie-engine-checkbox-label">
                            <input type="hidden"
                                name="movie_engine_monetization_settings[<?php echo esc_attr($key); ?>_desktop]"
                                value="0">
                            <input type="checkbox"
                                name="movie_engine_monetization_settings[<?php echo esc_attr($key); ?>_desktop]"
                                value="1"
                                <?php checked($opt_desktop, 1); ?>>
                            <span><?php esc_html_e('Show on Desktop', 'cine-movie-engine'); ?></span>
                        </label>
                    </div>
                    <div class="movie-engine-form-group me-col-6">
                        <label class="movie-engine-checkbox-label">
                            <input type="hidden"
                                name="movie_engine_monetization_settings[<?php echo esc_attr($key); ?>_mobile]"
                                value="0">
                            <input type="checkbox"
                                name="movie_engine_monetization_settings[<?php echo esc_attr($key); ?>_mobile]"
                                value="1"
                                <?php checked($opt_mobile, 1); ?>>
                            <span><?php esc_html_e('Show on Mobile', 'cine-movie-engine'); ?></span>
                        </label>
                    </div>
                </div>
            </div>
        </div>
    <?php
    }

    public static function render_pro_stub($key, $label) {
    ?>
        <div class="movie-engine-card me-ad-slot-card me-pro-card">
            <div class="movie-engine-card-header">
                <h3>
                    <span class="fa-solid fa-table-columns"></span> <?php echo esc_html($label); ?>
                    <span class="me-pro-badge"><?php esc_html_e('Pro', 'cine-movie-engine'); ?></span>
                </h3>
            </div>
            <div class="movie-engine-card-body">
                <div class="me-pro-notice-box">
                    <span class="dashicons dashicons-lock"></span>
                    <div class="me-pro-notice-message">
                        <strong><?php echo esc_html($label); ?></strong>
                        <p><?php esc_html_e('This is a PRO feature. Upgrade to unlock advanced ad placements.', 'cine-movie-engine'); ?></p>
                    </div>
                    <a href="<?php echo esc_url(\MovieEngine\Admin\Upgrade::get_pro_url('pricing')); ?>" target="_blank" class="button button-primary me-pro-notice-btn">
                        <?php esc_html_e('Upgrade to Pro', 'cine-movie-engine'); ?>
                    </a>
                </div>
                <div class="me-locked-feature">
                    <div class="movie-engine-form-group">
                        <label><?php esc_html_e('Desktop Ad Code', 'cine-movie-engine'); ?></label>
                        <textarea class="movie-engine-textarea me-code-editor" rows="4" disabled></textarea>
                    </div>
                </div>
            </div>
        </div>
<?php
    }
}
