<?php

namespace MovieEngine\Admin\Monetization;

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

class MonetizationSettings {

    private static $settings_instance;

    public static function render($active_tab_slug = 'monetization_general') {
        settings_fields('movie_engine_monetization');
        $options = get_option('movie_engine_monetization_settings', []);

        if (empty($options)) {
            $old_keys = [
                'general',
                'slots',
                'video',
                'paywall',
                'affiliate',
                'adblock'
            ];
            $migrated_options = [];
            foreach ($old_keys as $key) {
                $old_opt = get_option('movie_engine_monetization_' . $key . '_settings', []);
                if (!empty($old_opt)) {
                    $migrated_options = array_merge($migrated_options, $old_opt);
                }
            }
            if (!empty($migrated_options)) {
                $options = $migrated_options;
                update_option('movie_engine_monetization_settings', $options);
            }
        }

        self::$settings_instance = null;

        $current_sub_tab = str_replace('monetization_', '', $active_tab_slug);
        if ($current_sub_tab === 'monetization') {
            $current_sub_tab = 'general';
        }
?>
        <input type="hidden" name="movie_engine_monetization_settings[active_sub_tab]" id="movie-engine-active-sub-tab" value="<?php echo esc_attr($current_sub_tab); ?>">
        <div class="movie-engine-settings-section">
            <div class="movie-engine-section-header">
                <h2><?php esc_html_e('Monetization', 'cine-movie-engine'); ?></h2>
                <p class="description"><?php esc_html_e('Configure monetization options', 'cine-movie-engine'); ?></p>
            </div>

            <div class="movie-engine-sub-tabs">
                <a href="#general" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'general' ? 'active' : ''); ?>"><?php esc_html_e('General', 'cine-movie-engine'); ?></a>
                <a href="#slots" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'slots' ? 'active' : ''); ?>"><?php esc_html_e('Ad Slots', 'cine-movie-engine'); ?></a>
                <a href="#video" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'video' ? 'active' : ''); ?>"><?php esc_html_e('Video Ads', 'cine-movie-engine'); ?></a>
                <a href="#paywall" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'paywall' ? 'active' : ''); ?>"><?php esc_html_e('Paywall', 'cine-movie-engine'); ?></a>
                <a href="#affiliate" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'affiliate' ? 'active' : ''); ?>"><?php esc_html_e('Affiliate', 'cine-movie-engine'); ?></a>
                <a href="#adblock" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'adblock' ? 'active' : ''); ?>"><?php esc_html_e('AdBlock', 'cine-movie-engine'); ?></a>
                <a href="#plans" class="movie-engine-sub-tab <?php echo esc_attr($current_sub_tab === 'plans' ? 'active' : ''); ?>"><?php esc_html_e('Plans', 'cine-movie-engine'); ?></a>
            </div>

            <div class="movie-engine-sub-tab-content">
                <div id="general" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'general' ? 'active' : ''); ?>">
                    <?php self::render_tab('GeneralTab', $options); ?>
                </div>
                <div id="slots" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'slots' ? 'active' : ''); ?>">
                    <?php self::render_tab('SlotsTab', $options); ?>
                </div>
                <div id="video" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'video' ? 'active' : ''); ?>">
                    <?php self::render_tab('VideoTab', $options); ?>
                </div>
                <div id="paywall" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'paywall' ? 'active' : ''); ?>">
                    <?php self::render_tab('PaywallTab', $options); ?>
                </div>
                <div id="affiliate" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'affiliate' ? 'active' : ''); ?>">
                    <?php self::render_tab('AffiliateTab', $options); ?>
                </div>
                <div id="adblock" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'adblock' ? 'active' : ''); ?>">
                    <?php self::render_tab('AdBlockTab', $options); ?>
                </div>
                <div id="plans" class="movie-engine-sub-panel <?php echo esc_attr($current_sub_tab === 'plans' ? 'active' : ''); ?>">
                    <?php self::render_tab('MembershipPlansTab', $options); ?>
                </div>
            </div>

            <div class="movie-engine-form-actions">
                <?php submit_button(esc_html__('Save Changes', 'cine-movie-engine'), 'primary me-btn-primary', 'submit', false); ?>
            </div>

        </div>
<?php
    }

    /**
     * Render individual tab
     */
    private static function render_tab($tab_name, $options = []) {
        $tab_file = MOVIE_ENGINE_PATH . 'includes/Admin/Monetization/Tabs/' . $tab_name . '.php';

        if (file_exists($tab_file)) {
            require_once $tab_file;
            $tab_class = '\\MovieEngine\\Admin\\Monetization\\Tabs\\' . $tab_name;
            if (class_exists($tab_class) && method_exists($tab_class, 'render')) {
                $tab_class::render($options, false);
            }
        } else {
            $method_name = 'render_monetization_' . strtolower(str_replace('Tab', '', $tab_name)) . '_tab';
            if (self::$settings_instance && method_exists(self::$settings_instance, $method_name)) {
                self::$settings_instance->$method_name();
            }
        }
    }
}
