<?php
/**
 * Plugin uninstall class.
 *
 * @package flo-sync
 */

namespace NMFS;

use NMFS\Admin\Pages\Admin_Tools;
use NMFS\Sync\Items\Article_Availabilities\Setup as Article_Availabilities_Setup;
use NMFS\Sync\Items\Articles\Article_Sync_Meta_Helpers;
use NMFS\Sync\Items\Articles\Articles;
use NMFS\Sync\Items\Groups\Sync_Groups as Group_Sync;

defined( 'ABSPATH' ) || exit;

/**
 * Plugin uninstall class.
 */
class Plugin_Uninstall {

	/**
	 * Setup uninstallation filters.
	 *
	 * @param string $service_name_underscored The service name in underscored format.
	 *
	 * @return void
	 */
	public static function setup_shared_hooks( string $service_name_underscored ): void {

		add_action(
			'northmill_online_shared_uninstall_delete_data_after_' . $service_name_underscored,
			array( self::class, 'action_delete_data_after' )
		);

		add_filter(
			'northmill_online_shared_uninstall_delete_cpts_' . $service_name_underscored,
			array( self::class, 'filter_delete_cpts' )
		);

		add_filter(
			'northmill_online_shared_uninstall_delete_post_meta_keys_' . $service_name_underscored,
			array( self::class, 'filter_delete_post_meta_keys' )
		);


		add_filter(
			'northmill_online_shared_uninstall_delete_term_meta_keys_' . $service_name_underscored,
			array( self::class, 'filter_delete_term_meta_keys' )
		);

		add_filter(
			'northmill_online_shared_uninstall_cleaner_message_' . $service_name_underscored,
			function () {
				return __( 'Uninstalling with this option enabled is a destructive action, and can NOT be undone. Among other things meta data connecting WooCommerce orders to Northmill Hub orders will be deleted, removing the connection for existing orders. Log files are also deleted. This cannot be restored by a plugin reinstall.', 'flo-sync' );
			},
			10,
			0
		);
	}

	/**
	 * Filters CPTs to delete on uninstall.
	 *
	 * @param string[] $cpts CPT keys.
	 *
	 * @return string[] Updated array of CPT keys.
	 */
	public static function filter_delete_cpts( array $cpts ): array {
		$cpts[] = Article_Availabilities_Setup::CPT_KEY;

		return $cpts;
	}

	/**
	 * Filters post meta keys to delete on uninstallation.
	 *
	 * @param array $meta_keys Meta keys.
	 *
	 * @return array Updated array of meta keys.
	 */
	public static function filter_delete_post_meta_keys( array $meta_keys ): array {
		if ( ! isset( $meta_keys['product'] ) ) {
			$meta_keys['product'] = array();
		}
		$meta_keys['product'][] = Article_Sync_Meta_Helpers::META_KEY_SYNC_STATUS;
		$meta_keys['product'][] = Article_Sync_Meta_Helpers::META_KEY_HASH;
		$meta_keys['product'][] = Article_Sync_Meta_Helpers::META_KEY_LAST_SYNC_RESPONSE;
		$meta_keys['product'][] = Articles::META_KEY_ORDER_TICKET_TEXT;
		$meta_keys['product'][] = Articles::META_KEY_ORDER_TICKET_DIALOGUE;
		$meta_keys['product'][] = Articles::META_KEY_GROUP_ID;
		$meta_keys['product'][] = Articles::META_KEY_IMAGE_ID;
		$meta_keys['product'][] = Articles::META_KEY_IMAGE_NAME;
		$meta_keys['product'][] = Articles::META_KEY_TMP_LOCK;
		$meta_keys['product'][] = Article_Sync_Meta_Helpers::META_KEY_SYNC_STATUS_PRIVATE;
		$meta_keys['product'][] = Article_Sync_Meta_Helpers::META_KEY_SYNC_ERROR;
		$meta_keys['product'][] = Article_Sync_Meta_Helpers::META_KEY_SYNC_LAST;

		return $meta_keys;
	}

	/**
	 * Filters term meta keys to delete on uninstall.
	 *
	 * @param array $meta_keys Meta keys.
	 *
	 * @return array Updated array of term meta keys.
	 */
	public static function filter_delete_term_meta_keys( array $meta_keys ): array {
		$meta_keys[] = Group_Sync::META_KEY_GROUP_ID;
		$meta_keys[] = Group_Sync::META_KEY_LAST_UPDATED;

		return $meta_keys;
	}

	/**
	 * Delete data after uninstall.
	 *
	 * @return void
	 */
	public static function action_delete_data_after(): void {
		delete_option( Admin_Tools::OPTION_SYNC_3_DATA_REMOVED );
		delete_transient( Group_Sync::TRANSIENT_LAST_CLEANED );

		$posts = get_posts(
			array(
				'post_type'   => 'shop_coupon',
				'meta_key'    => 'mf_id',
				'fields'      => 'ids',
				'numberposts' => -1,
			)
		);

		if ( ! empty( $posts ) ) {
			foreach ( $posts as $post_id ) {
				wp_delete_post( $post_id, true );
			}
		}
	}
}
