<?php
/**
 * Uninstall routines for the Amazon SES extension.
 *
 * @package Robotstxt_SMTP_AmazonSES
 */

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

$option_keys = array(
	'amazon_ses_access_key',
	'amazon_ses_secret_key',
	'amazon_ses_region',
);

robotstxt_smtp_amazonses_delete_network_settings( $option_keys );

if ( function_exists( 'is_multisite' ) && is_multisite() ) {
	$sites = get_sites( array( 'fields' => 'ids' ) );

	foreach ( $sites as $site_id ) {
		switch_to_blog( (int) $site_id );
		robotstxt_smtp_amazonses_delete_site_settings( $option_keys );
		restore_current_blog();
	}
} else {
	robotstxt_smtp_amazonses_delete_site_settings( $option_keys );
}


if ( ! function_exists( 'robotstxt_smtp_amazonses_delete_site_settings' ) ) {
	/**
	 * Deletes Amazon SES settings and logs for the current site.
	 *
	 * @param array<int, string> $keys Option keys to remove.
	 *
	 * @return void
	 */
	function robotstxt_smtp_amazonses_delete_site_settings( array $keys ): void {
		robotstxt_smtp_amazonses_remove_keys_from_option( 'robotstxt_smtp_options', $keys );
		robotstxt_smtp_amazonses_delete_logs();
	}
}

if ( ! function_exists( 'robotstxt_smtp_amazonses_delete_network_settings' ) ) {
	/**
	 * Removes Amazon SES values stored in the network-wide settings.
	 *
	 * @param array<int, string> $keys Option keys to remove.
	 *
	 * @return void
	 */
	function robotstxt_smtp_amazonses_delete_network_settings( array $keys ): void {
		$settings = get_site_option( 'robotstxt_smtp_network_options', array() );

		if ( ! is_array( $settings ) || empty( $settings ) ) {
			return;
		}

		$clean_settings = robotstxt_smtp_amazonses_remove_keys( $settings, $keys );

		if ( $clean_settings !== $settings ) {
			update_site_option( 'robotstxt_smtp_network_options', $clean_settings );
		}
	}
}

if ( ! function_exists( 'robotstxt_smtp_amazonses_remove_keys_from_option' ) ) {
	/**
	 * Removes specific keys from an option array if it exists.
	 *
	 * @param string             $option_name Option identifier.
	 * @param array<int, string> $keys        Keys to remove.
	 *
	 * @return void
	 */
	function robotstxt_smtp_amazonses_remove_keys_from_option( string $option_name, array $keys ): void {
		$settings = get_option( $option_name, array() );

		if ( ! is_array( $settings ) || empty( $settings ) ) {
			return;
		}

		$clean_settings = robotstxt_smtp_amazonses_remove_keys( $settings, $keys );

		if ( $clean_settings !== $settings ) {
			update_option( $option_name, $clean_settings );
		}
	}
}

if ( ! function_exists( 'robotstxt_smtp_amazonses_remove_keys' ) ) {
	/**
	 * Returns an array without the provided keys.
	 *
	 * @param array<string, mixed> $settings Option values.
	 * @param array<int, string>   $keys     Keys to remove.
	 *
	 * @return array<string, mixed>
	 */
	function robotstxt_smtp_amazonses_remove_keys( array $settings, array $keys ): array {
		foreach ( $keys as $key ) {
			unset( $settings[ $key ] );
		}

		return $settings;
	}
}

if ( ! function_exists( 'robotstxt_smtp_amazonses_delete_logs' ) ) {
	/**
	 * Deletes all stored SMTP logs for the current site.
	 *
	 * @return void
	 */
	function robotstxt_smtp_amazonses_delete_logs(): void {
		do {
			$logs = get_posts(
				array(
					'post_type'      => 'robotstxt_smtp_log',
					'post_status'    => 'any',
					'fields'         => 'ids',
					'posts_per_page' => 100,
					'orderby'        => 'ID',
					'order'          => 'ASC',
				)
			);

			foreach ( $logs as $log_id ) {
				wp_delete_post( (int) $log_id, true );
			}
		} while ( ! empty( $logs ) );
	}
}
