<?php
/**
 * Fast-Events Uninstall
 *
 * Uninstalling Fast-Events deletes user roles, pages, media, tables, options, and the admin interface directory.
 *
 * @package FastEvents\Uninstaller
 * @since 2.0.3
 */

defined( 'WP_UNINSTALL_PLUGIN' ) || exit;

/**
 * Delete the tables, actions and settings
 *
 * @since 2.0.3
 * @since 2.2.0 Drop coupons table
 * @since 2.5.0 - Drop email log table
 *              - Delete any existing retry messages from the options table.
 * @since 3.0.0 Delete all plugin settings from the options table.
 */
function fast_events_execute_uninstall(): void {
	require_once ABSPATH . 'wp-admin/includes/file.php';
	WP_Filesystem();
	global $wp_filesystem;
	global $wpdb;

	// Delete old actions from the action scheduler.
	$tablename = $wpdb->prefix . 'actionscheduler_actions';
	if ( $wpdb->get_var( "SHOW TABLES LIKE '$tablename'" ) === $tablename ) { //phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
		$wpdb->query( "DELETE FROM {$wpdb->prefix}actionscheduler_logs WHERE action_id in (SELECT action_id FROM {$wpdb->prefix}actionscheduler_actions WHERE hook LIKE 'fast_events%')" );
		$wpdb->query( "DELETE FROM {$wpdb->prefix}actionscheduler_actions WHERE hook LIKE 'fast_events%'" );
	}

	// Delete (sub)accounts fast-events application passwords and reset the user role.
	$users = get_users( array( 'role' => 'fe_account' ) );
	foreach ( $users as $user ) {
		if ( is_multisite() ) {
			require_once ABSPATH . 'wp-admin/includes/ms.php';
			wpmu_delete_user( $user->ID );
		} else {
			require_once ABSPATH . 'wp-admin/includes/user.php';
			wp_delete_user( $user->ID );
		}
	}

	// Finally, remove the 'Fast Events user' role.
	remove_role( 'fe_account' );

	// Get all post-ids (demo data and from the events) and delete them.
	$post_ids = get_option( 'fast_events_demo_data_post_ids', array() );
	$events   = $wpdb->get_results( "SELECT id, pdf_info FROM {$wpdb->prefix}fe_events" );
	if ( ! empty( $events ) ) {
		foreach ( $events as $event ) {
			$pdf_info = json_decode( $event->pdf_info );
			if ( ! empty( $pdf_info ) ) {
				$post_ids[] = $pdf_info->ticket->id;
				$post_ids[] = $pdf_info->invoice->id;
				if ( ! empty( $pdf->ticket->types ) ) {
					foreach ( $pdf_info->ticket->types as $value ) {
						$post_ids[] = $value;
					}
				}
			}
		}
		foreach ( array_unique( $post_ids, SORT_NUMERIC ) as $post_id ) {
			wp_delete_post( $post_id, true );
		}
	}

	// Delete the tables.
	$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}fe_event_entries" );
	$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}fe_event_qrcodes" );
	$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}fe_event_orders" );
	$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}fe_events" );
	$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}fe_event_seats" );
	$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}fe_event_user_groups" );
	$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}fe_event_webhooks" );
	$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}fe_event_log" );
	$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}fe_event_email_log" );
	$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}fe_event_coupons" );

	// Remove all plugin settings, retry messages and ordering shortcodes from the options table.
	$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'fast_events_%'" );

	// Delete admin interface.
	if ( is_main_site() ) {
		$upload_dir = wp_upload_dir();
		$fe_dir     = $upload_dir['basedir'] . '/fast-events';
		if ( file_exists( $fe_dir ) ) {
			$wp_filesystem->rmdir( $fe_dir, true );
		}
	}
}

// Single and multi site uninstall.
if ( is_multisite() ) {
	foreach ( get_sites( array( 'fields' => 'ids' ) ) as $site_id ) {
		switch_to_blog( $site_id );
		fast_events_execute_uninstall();
		restore_current_blog();
	}
} else {
	fast_events_execute_uninstall();
}
