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

class SS_Glossary {

    public static function init() {
        add_action('init', [self::class, 'register_cpt']);
        add_action('load-post.php', [self::class, 'enable_slug']);
        add_action('load-post-new.php', [self::class, 'enable_slug']);
        add_action('edit_form_after_title', [self::class, 'guide_link']);

        add_filter('get_sample_permalink_html', [self::class, 'hide_slug'], 10, 2);
        add_filter('rest_prepare_ss_glossary', [self::class, 'remove_slug_rest'], 10, 3);
        add_filter('enter_title_here', [self::class, 'title_placeholder'], 10, 2);

        add_action('admin_init', [self::class, 'migrate_terms']);
    }

    public static function register_cpt() {

        $labels = [
            'name' => __('Glossary', 'searchshifter'),
            'singular_name' => __('Glossary Term', 'searchshifter'),
        ];

        register_post_type('ss_glossary', [
            'labels' => $labels,
            'public' => true,
            'has_archive' => false,
            'show_in_rest' => true,
            'supports' => ['title'],
            'show_in_menu' => 'searchshifter',
            'rewrite' => [
                'slug' => 'term',
                'with_front' => false
            ]
        ]);
    }

    public static function enable_slug() {
        if (!in_array(ss_get_current_plan(), ['pro','authority','elite'])) return;

        remove_filter('get_sample_permalink_html', '__return_empty_string', 99);
    }

    public static function hide_slug($html, $post_id) {
        if (ss_get_current_plan() === 'free' && get_post_type($post_id) === 'ss_glossary') {
            return '';
        }
        return $html;
    }

    public static function remove_slug_rest($response) {
        if (ss_get_current_plan() === 'free') {
            unset($response->data['slug']);
            unset($response->data['permalink_template']);
            unset($response->data['generated_slug']);
        }
        return $response;
    }

    public static function guide_link() {
        global $post;

        if (!$post || $post->post_type !== 'ss_glossary') return;

        echo '<div style="background:#f0f6ff;padding:12px 16px;border-left:4px solid #2271b1;margin:15px 0;">
            Need help creating glossary entries? 
            <a href="https://project1.impactmindz.in/glossary/" target="_blank"><strong>View guide</strong></a>
        </div>';
    }

    public static function migrate_terms() {

        if (get_option('ss_glossary_migrated') == 'yes') return;

        $old_terms = get_option('ss_glossary_terms', []);

        foreach ($old_terms as $item) {

            $post_id = wp_insert_post([
                'post_title' => sanitize_text_field($item['term'] ?? ''),
                'post_content' => sanitize_textarea_field($item['definition'] ?? ''),
                'post_type' => 'ss_glossary',
                'post_status' => 'publish'
            ]);

            if (!empty($item['same_as'])) {
                update_post_meta($post_id, 'ss_same_as', esc_url_raw($item['same_as']));
            }
        }

        update_option('ss_glossary_migrated', 'yes');
    }

    public static function title_placeholder($title, $post) {
        return $post->post_type === 'ss_glossary' ? 'Add term' : $title;
    }
}