<?php

namespace Easymailing\App\Core\Provider\Woocommerce\Service;

class MetadataHelper
{

	const SYNCHRO_META_PREFIX = '_easymailing_synchronization_';
	const NOT_RESET_META_PREFIX = '_easymailing_not_reset_synchronization_';

	// to avoid deleting config in older versions of plugin
	public static function getConfiguration(): string
	{
		return 'easymailing_configuration';
	}

	public static function getSynchronizationConfig(): string
	{
		return self::SYNCHRO_META_PREFIX . 'synchronization_config';
	}

	public static function getSynchronizationStatus(): string
	{
		return self::SYNCHRO_META_PREFIX . 'synchronization_status';
	}

	public static function isResourceCreated(): string
	{
		return self::SYNCHRO_META_PREFIX . 'is_resource_created';
	}

	public static function getResourceId(): string
	{
		return self::SYNCHRO_META_PREFIX . 'resource_id';
	}

	public static function getVariantResourceId(): string
	{
		return self::SYNCHRO_META_PREFIX . 'variant_resource_id';
	}

	public static function getCustomerResourceId(): string
	{
		return self::SYNCHRO_META_PREFIX . 'customer_resource_id';
	}

	public static function getTreatmentPurposes(): string
	{
		return self::SYNCHRO_META_PREFIX . 'treatment_purposes';
	}

	public static function getLastBatchSynchronization(): string
	{
		return self::SYNCHRO_META_PREFIX . 'last_batch_synchronization';
	}

	public static function getBatchSynchronizationStatus(): string
	{
		return self::SYNCHRO_META_PREFIX . 'batch_synchronization_status';
	}

	public static function getBackgroundTaskId(): string
	{
		return self::SYNCHRO_META_PREFIX . 'background_task_id';
	}

	public static function getCheckBackgroundTaskId(): string
	{
		return self::SYNCHRO_META_PREFIX . 'check_background_task_id';
	}

	public static function getApiBatchOperationsIris(): string
	{
		return self::SYNCHRO_META_PREFIX . 'api_batch_operations_iris';
	}

	public static function getBatchSynchronizationPhase(): string
	{
		return self::SYNCHRO_META_PREFIX . 'batch_synchronization_phase';
	}

	public static function getBatchSynchronizationProgress(): string
	{
		return self::SYNCHRO_META_PREFIX . 'batch_synchronization_progress';
	}

	public static function getSkipReason(): string
	{
		return self::SYNCHRO_META_PREFIX . 'skip_reason';
	}

	public static function getErrorMessage(): string
	{
		return self::SYNCHRO_META_PREFIX . 'error_message';
	}

	public static function getRetryOnlyMode(): string
	{
		return self::SYNCHRO_META_PREFIX . 'retry_only_mode';
	}


	//********************** NOT RESET METAS  **************************//

	public static function getCampaignHash(): string
	{
		return self::NOT_RESET_META_PREFIX . 'campaign_hash';
	}


	//********************** METHODS  **************************//

	public static function reset()
	{
		global $wpdb;

		$prefix = self::SYNCHRO_META_PREFIX;

		$wpdb->query(
			$wpdb->prepare(
				"DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s",
				$prefix . '%'
			)
		);
		$wpdb->query(
			$wpdb->prepare(
				"DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE %s",
				$prefix . '%'
			)
		);
		$wpdb->query(
			$wpdb->prepare(
				"DELETE FROM {$wpdb->termmeta} WHERE meta_key LIKE %s",
				$prefix . '%'
			)
		);
		$wpdb->query(
			$wpdb->prepare(
				"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
				$prefix . '%'
			)
		);
	}

	public static function deleteMetadata()
	{
		global $wpdb;

		self::reset();

		$prefix = self::NOT_RESET_META_PREFIX;

		$wpdb->query(
			$wpdb->prepare(
				"DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE %s",
				$prefix . '%'
			)
		);
		$wpdb->query(
			$wpdb->prepare(
				"DELETE FROM {$wpdb->usermeta} WHERE meta_key LIKE %s",
				$prefix . '%'
			)
		);
		$wpdb->query(
			$wpdb->prepare(
				"DELETE FROM {$wpdb->termmeta} WHERE meta_key LIKE %s",
				$prefix . '%'
			)
		);
		$wpdb->query(
			$wpdb->prepare(
				"DELETE FROM {$wpdb->options} WHERE option_name LIKE %s",
				$prefix . '%'
			)
		);
	}
}
