<?php
/**
 * SEO Fury Uninstall (Free Plugin)
 *
 * Fired when the plugin is deleted (not just deactivated).
 * Checks the user's preference before deciding whether to clean up database data.
 *
 * Only cleans up free-plugin data. PRO addon has its own uninstall.php.
 *
 * @package SEO_Fury
 */

if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    exit;
}

global $wpdb;

// Check user preference: 0 = keep data (default), 1 = delete everything.
$delete_data = (int) get_option( 'seo_fury_delete_data', 0 );

// Always remove the preference flag itself regardless of choice.
delete_option( 'seo_fury_delete_data' );

if ( $delete_data !== 1 ) {
    // User chose to keep data — exit without deleting anything else.
    return;
}

// ---- Full cleanup: user explicitly chose to delete everything ----

// 1. Drop free-plugin database tables.
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}seo_redirects" );
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}seo_indexnow_log" );
$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}seo_404_log" );

// 2. Delete all plugin options (current seo_fury_* prefix).
$wpdb->query(
    $wpdb->prepare(
        "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
        $wpdb->esc_like( 'seo_fury_' ) . '%'
    )
);

// 3. Delete legacy options (old my_seo_* prefix, in case migration didn't run).
$wpdb->query(
    $wpdb->prepare(
        "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
        $wpdb->esc_like( 'my_seo_' ) . '%'
    )
);

// 4. Delete all post meta created by the plugin (current prefix).
$wpdb->query(
    $wpdb->prepare(
        "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s",
        $wpdb->esc_like( '_seo_fury_' ) . '%'
    )
);

// 5. Delete legacy post meta (old prefix, in case migration didn't run).
$wpdb->query(
    $wpdb->prepare(
        "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s",
        $wpdb->esc_like( '_my_seo_' ) . '%'
    )
);

// 6. Delete all transients created by the plugin.
$wpdb->query(
    $wpdb->prepare(
        "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
        $wpdb->esc_like( '_transient_seo_fury_' ) . '%'
    )
);
$wpdb->query(
    $wpdb->prepare(
        "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
        $wpdb->esc_like( '_transient_timeout_seo_fury_' ) . '%'
    )
);

// 7. Delete legacy transients (old prefix).
$wpdb->query(
    $wpdb->prepare(
        "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
        $wpdb->esc_like( '_transient_my_seo_' ) . '%'
    )
);
$wpdb->query(
    $wpdb->prepare(
        "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
        $wpdb->esc_like( '_transient_timeout_my_seo_' ) . '%'
    )
);

// 8. Delete user meta.
$wpdb->query(
    $wpdb->prepare(
        "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE %s",
        $wpdb->esc_like( '_seo_fury_' ) . '%'
    )
);
$wpdb->query(
    $wpdb->prepare(
        "DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE %s",
        $wpdb->esc_like( '_my_seo_' ) . '%'
    )
);

// 9. Delete IndexNow key file if it exists.
$indexnow_key = get_option( 'seo_fury_indexnow_api_key', '' );
if ( $indexnow_key ) {
    $key_file = ABSPATH . $indexnow_key . '.txt';
    if ( file_exists( $key_file ) ) {
        wp_delete_file( $key_file );
    }
}

// 10. Flush rewrite rules to clean up sitemap routes.
flush_rewrite_rules();
