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

class SS_Admin_UI {

    public static function init() {

        add_action('admin_notices', [__CLASS__, 'upgrade_notice']);
        add_action('admin_notices', [__CLASS__, 'import_notice']);

        add_filter('plugin_action_links_' . plugin_basename(SEARCHSHIFTER_PLUGIN_FILE), [__CLASS__, 'plugin_links']);

        add_action('restrict_manage_posts', [__CLASS__, 'import_button']);

        add_action('admin_head', [__CLASS__, 'admin_styles']);
    }

    /**
     * 🔒 Upgrade Notice
     */
    public static function upgrade_notice() {

        if (!class_exists('SS_Licensing')) return;

        $plan = strtolower(SS_Licensing::get_cached_status()['plan'] ?? 'free');

        if ($plan !== 'free') return;

        $glossary = wp_count_posts('ss_glossary')->publish ?? 0;
        $faq      = wp_count_posts('ss_qa')->publish ?? 0;

        if ($glossary === 0 || $faq === 0) return;

        echo '<div class="notice notice-warning is-dismissible">
            <p><strong>🔒 Smart Sync Engine is a Pro feature.</strong><br>
            Upgrade to <a href="https://searchshifter.ai/pricing/" target="_blank">Pro</a>.</p>
        </div>';
    }

    /**
     * ✅ Import Success Notice
     */
    public static function import_notice() {

        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( ! isset( $_GET['imported'] ) ) {
    return;
}

// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$imported = sanitize_text_field( wp_unslash( $_GET['imported'] ) );

if ( empty( $imported ) ) {
    return;
}

        echo '<div class="notice notice-success is-dismissible">
            <p>Authority Glossary Dataset Imported Successfully.</p>
        </div>';
    }

    /**
     * 🔘 Import Button (Authority)
     */
    public static function import_button() {

        global $typenow;

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

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

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

        echo '<a href="' . esc_url($url) . '" class="button button-primary" style="margin-left:10px;">
            Import Authority Dataset
        </a>';
        
    }

    /**
     * 🎨 Admin Styles
     */
    public static function admin_styles() {

        echo '<style>
            .ss-modal { z-index:9999999 !important; }
        </style>';
    }

    public static function plugin_links($links) {

        if (class_exists('SS_Licensing')) {
            $plan = strtolower(SS_Licensing::get_cached_status()['plan'] ?? 'free');

            if ($plan !== 'free') {
                return $links;
            }
        }

        $links[] = '<a href="https://searchshifter.ai/pricing/" target="_blank" 
            style="color:#fff;background:#0073aa;padding:3px 8px;border-radius:3px;">
            Upgrade to Pro
        </a>';

        return $links;
    }
}