<?php
if (!defined('ABSPATH')) exit;

class SS_Glossary_Admin {

    public static function init() {
        add_action('admin_footer', [self::class, 'title_helper']);
    }

    public static function title_helper() {
        global $post;

        if (!$post || $post->post_type !== 'ss_glossary') return;
        ?>
        <script>
        document.addEventListener("DOMContentLoaded", function () {
            const titleWrap = document.querySelector('#titlewrap');

            if (titleWrap && !document.querySelector('.ss-term-helper')) {
                const helper = document.createElement('p');
                helper.className = 'ss-term-helper';
                helper.style.marginTop = '6px';
                helper.style.color = '#666';
                helper.style.fontSize = '13px';
                helper.innerText = 'Enter the exact term you are defining.';
                titleWrap.appendChild(helper);
            }
        });
        </script>
        <?php
    }


}
            // Admin meta box for term-level plan flag
        add_action('add_meta_boxes', function() {

            if ( ! searchshifter_has_plan('authority') ) return;


            add_meta_box(
                'ss_plan_flag',
                'Term Plan Flag',
                function($post) {
                    $flag = get_post_meta($post->ID, 'ss_plan_flag', true) ?: 'free';
                    ?>
                    <label for="ss_plan_flag">Plan required to unlock this term</label>
                    <select id="ss_plan_flag" name="ss_plan_flag" style="width:100%;margin-top:6px;">
                        <option value="free" <?php selected($flag, 'free'); ?>>Free</option>
                        <option value="pro" <?php selected($flag, 'pro'); ?>>Pro</option>
                        <option value="authority" <?php selected($flag, 'authority'); ?>>Authority</option>
                    </select>
                    <p class="description">Controls public indicator + gating in the UI.</p>
                    <?php
                },
                'ss_glossary',
                'side',
                'default'
            );
        });

                /**
         * Add "Plan" column to Glossary table
         */
        add_filter('manage_ss_glossary_posts_columns', function($columns) {
            $columns['ss_plan_flag'] = __('Plan', 'searchshifter');
            return $columns;
        });

        /**
         * Display Plan flag inside the Plan column
         */
        add_action('manage_ss_glossary_posts_custom_column', function($column, $post_id) {

            if ($column === 'ss_plan_flag') {

                // Correct meta key
            $flag = get_post_meta($post_id, 'ss_plan_flag', true);


                // Default system-level value
                if (!$flag) {
                    $flag = 'free';
                }

                // Colors for each plan
                $colors = [
                    'free'      => '#43a047', // green
                    'pro'       => '#1e88e5', // blue
                    'authority' => '#8e24aa'  // purple
                ];

                $color = isset($colors[$flag]) ? $colors[$flag] : '#999';

                echo '<span style="
                    display:inline-block;
                    width:14px;
                    height:14px;
                    border-radius:50%;
                    background:' . esc_attr( $color ) . ';
                    margin-left:6px;
                " title="' . esc_attr( 'Term Plan: ' . ucfirst( $flag ) ) . '"></span>';

            }
        }, 10, 2);

        
        add_action('restrict_manage_posts', function() {
            global $typenow;

            if ($typenow !== 'ss_glossary') {
                return;
            }

            $plan = ss_get_current_plan();
            if ($plan !== 'authority') {
                return;
            }

            $url = admin_url('edit.php?post_type=ss_glossary&ss_import_authority=1');

           
        });

        add_action('save_post', function($post_id) {

            if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
            if (wp_is_post_revision($post_id)) return;

            $post = get_post($post_id);
            if (!$post || $post->post_type !== 'page') return;

            $content = $post->post_content;

            $has_glossary = strpos($content, '[searchshifter_glossary]') !== false;
            $has_faq      = strpos($content, '[searchshifter_faq]') !== false;

            // If it's a glossary page (already contains shortcode), enforce structure
            if ($has_glossary && trim($content) !== '[searchshifter_glossary]') {
                wp_update_post([
                    'ID' => $post_id,
                    'post_content' => '[searchshifter_glossary]'
                ]);
            }

            // If it's an FAQ page
            if ($has_faq && trim($content) !== '[searchshifter_faq]') {
                wp_update_post([
                    'ID' => $post_id,
                    'post_content' => '[searchshifter_faq]'
                ]);
            }

            if (isset($_POST['ss_sameas_url'])) {
                update_post_meta($post_id, '_ss_sameas_url', esc_url_raw($_POST['ss_sameas_url']));
            }

        });