<?php
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    exit;
}

/** ------------------------------
 * Core Licensing & Identity
 * ------------------------------ */
delete_option( 'ss_domain' );
delete_option( 'ss_license_status' );
delete_transient( 'ss_license_status' );

/** ------------------------------
 * Telemetry + install id
 * ------------------------------ */
delete_option( 'ss_telemetry_opt_in' );
delete_option( 'ss_installation_id' );

/** ------------------------------
 * AI Policy page (only if auto-created)
 * ------------------------------ */
/** @phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound */

$ss_page_id = (int) get_option( 'ss_ai_policy_page_id' );
if ( $ss_page_id > 0 ) {
    if ( get_post_meta( $ss_page_id, '_ss_ai_autocreated', true ) ) {
        wp_delete_post( $ss_page_id, true );
    }
}
delete_option( 'ss_ai_policy_page_id' );

/** ------------------------------
 * llms.txt file pointer
 * ------------------------------ */
$ss_llms_path = get_option( 'ss_llms_written_path' );
if ( $ss_llms_path && file_exists( $ss_llms_path ) ) {
    wp_delete_file( $ss_llms_path );
}
delete_option( 'ss_llms_written_path' );

/** @phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound */

/** ------------------------------
 * Organization sync
 * ------------------------------ */
delete_option( 'ss_org_sync' );

/** ------------------------------
 * Glossary Page Cleanup
 * ------------------------------ */
$glossary_id = (int) get_option('ss_glossary_page_id');

if ($glossary_id > 0) {
    $post = get_post($glossary_id);

    if ($post) {
        // ✅ Safe check: only delete if it contains our shortcode
        if (strpos($post->post_content, '[searchshifter_glossary]') !== false) {
            wp_delete_post($glossary_id, true);
        }
    }
}

delete_option('ss_glossary_page_id');
delete_option('ss_glossary_conflict');


/** ------------------------------
 * FAQ Page Cleanup
 * ------------------------------ */
$faq_id = (int) get_option('ss_faq_page_id');

if ($faq_id > 0) {
    $post = get_post($faq_id);

    if ($post) {
        if (strpos($post->post_content, '[searchshifter_faq]') !== false) {
            wp_delete_post($faq_id, true);
        }
    }
}

delete_option('ss_faq_page_id');
delete_option('ss_faq_conflict');

delete_option('ss_conflict_notice_dismissed');
delete_option('ss_viewed_glossary');
delete_option('ss_viewed_faq');