<?php
/**
 * Fired when the plugin is uninstalled.
 *
 * @link       https://ttswp.com
 * @since      1.0.0
 *
 * @package    TTSWP
 */

// If uninstall not called from WordPress, then exit.
if (!defined('WP_UNINSTALL_PLUGIN')) {
    die;
}

/**
 * Only delete data if the user opted in via the "Delete all data on uninstall" setting.
 * This prevents accidental data loss when reinstalling.
 */
$delete_data = get_option('mementor_tts_delete_data', 'no');
if ($delete_data !== 'yes') {
    return;
}

global $wpdb;

// --- 1. Remove all plugin options ---
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall script cleanup.
$wpdb->query(
    $wpdb->prepare(
        "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
        $wpdb->esc_like('mementor_tts_') . '%'
    )
);

// --- 2. Remove all transients ---
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall script cleanup.
$wpdb->query(
    $wpdb->prepare(
        "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s OR option_name LIKE %s",
        $wpdb->esc_like('_transient_mementor_tts_') . '%',
        $wpdb->esc_like('_transient_timeout_mementor_tts_') . '%'
    )
);

// --- 3. Drop all custom database tables ---
$tables = array(
    $wpdb->prefix . 'mementor_tts_elements',
    $wpdb->prefix . 'mementor_tts_queue',
    $wpdb->prefix . 'mementor_tts_speeches',
    $wpdb->prefix . 'mementor_tts_generation_history',
    $wpdb->prefix . 'mementor_tts_player_stats',
    $wpdb->prefix . 'mementor_tts_player_stats_aggregated',
    $wpdb->prefix . 'mementor_tts_user_credits',
    $wpdb->prefix . 'mementor_tts_transcriptions',
    $wpdb->prefix . 'mementor_tts_analytics',
    $wpdb->prefix . 'mementor_tts_daily_stats',
);

foreach ($tables as $table) {
    // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name from $wpdb->prefix + hardcoded string.
    $wpdb->query("DROP TABLE IF EXISTS $table");
}

// --- 4. Remove all scheduled cron events ---
wp_clear_scheduled_hook('mementor_tts_daily_cleanup');
wp_clear_scheduled_hook('mementor_tts_cleanup_audio');
wp_clear_scheduled_hook('mementor_tts_aggregate_player_stats');
wp_clear_scheduled_hook('mementor_tts_send_telemetry');
wp_clear_scheduled_hook('mementor_tts_send_remote_telemetry');
wp_clear_scheduled_hook('mementor_tts_aggregate_analytics');

// --- 5. Delete post meta ---
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Uninstall script cleanup.
$wpdb->query(
    $wpdb->prepare(
        "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s",
        $wpdb->esc_like('_mementor_tts_') . '%'
    )
);

// --- 6. Clean up uploads directory ---
$upload_dir = wp_upload_dir();
$tts_dir = $upload_dir['basedir'] . '/text-to-speech-tts';

if (file_exists($tts_dir)) {
    require_once ABSPATH . 'wp-admin/includes/file.php';

    global $wp_filesystem;
    WP_Filesystem();

    if ($wp_filesystem && $wp_filesystem->is_dir($tts_dir)) {
        $wp_filesystem->delete($tts_dir, true);
    }
}
