<?php

// don't call the file directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

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

$bookify_general_settings = get_option( 'bookify_general_settings' );
$bookify_delete_on_uninstall = isset( $bookify_general_settings['deleteDataWhenUnistall'] ) ? $bookify_general_settings['deleteDataWhenUnistall'] : false;

if ( ! $bookify_delete_on_uninstall ) {
	return;
}

global $wpdb;

$bookify_tables = array(
	'bookify_appointments',
	'bookify_appointments_meta',
	'bookify_service_categories',
	'bookify_service_categories_meta',
	'bookify_locations',
	'bookify_locations_meta',
	'bookify_notifications',
	'bookify_notifications_meta',
	'bookify_payments',
	'bookify_payments_meta',
	'bookify_services',
	'bookify_services_meta',
);

foreach ( $bookify_tables as $bookify_table ) {
	$wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $wpdb->prefix . $bookify_table ) );
}

$bookify_roles = array( 'bookify-staff', 'bookify-customer' );

foreach ( $bookify_roles as $bookify_role ) {
	$users = get_users( array( 'role' => $bookify_role ) );
	foreach ( $users as $user ) {
		if ( in_array( 'administrator', $user->roles ) ) {
			$user->remove_role( $bookify_role );
		} else {
			wp_delete_user( $user->ID );
		}
	}
	remove_role( $bookify_role );
}

$bookify_options = array(
	'bookify_company_details',
	'bookify_general_settings',
	'bookify_notification_settings',
	'bookify_payment_settings',
	'bookify_prev_multi_booking',
	'bookify_integration_settings',
	'bookify_onboarding_completed'
);

foreach ( $bookify_options as $bookify_option ) {
	delete_option( $bookify_option );
}
