<?php
/**
 * Fired when the plugin is uninstalled.
 *
 * @package Folderra_Smart_Folder_Organizer
 */

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

// Delete plugin options.
delete_option( 'fold82sm_show_activation_notice' );
delete_option( 'fold82sm_show_welcome' );
delete_option( 'fold82sm_settings' );
delete_option( 'fold82sm_media_organizer_db_version' );

// Remove Media Organizer custom tables.
global $wpdb;

$tables = array(
    $wpdb->prefix . 'folderra_media_usage',
    $wpdb->prefix . 'folderra_media_files',
    $wpdb->prefix . 'folderra_media_actions',
);

foreach ( $tables as $table ) {
    // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
    $wpdb->query( "DROP TABLE IF EXISTS {$table}" );
}

// Remove Media Organizer post meta from attachments.
$meta_keys = array( 'folderra_quarantine', 'folderra_uid' );

foreach ( $meta_keys as $meta_key ) {
    $wpdb->query(
        $wpdb->prepare(
            "DELETE FROM {$wpdb->postmeta} WHERE meta_key = %s",
            $meta_key
        )
    );
}

// Note: We do NOT delete taxonomy terms to preserve user folder data in case they reinstall the plugin.
