<?php

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

use Easymailing\App\Api\Client;
use Easymailing\App\Api\Exception\RequestLimitException;
use Easymailing\App\Core\Log\LogManager;
use Easymailing\App\Core\Provider\Woocommerce\Model\LogType;
use Easymailing\App\Core\Provider\Woocommerce\Service\MetadataHelper;
use Easymailing\App\Repository\ConfigRepository;

class DeleteHook
{
	private $config;
	private $apiClient;
	private $logManager;

	public function __construct(ConfigRepository $configRepository,Client $apiClient, LogManager $logManager)
	{
		$this->config = $configRepository->getSynchronizationConfig();
		$this->apiClient = $apiClient;
		$this->logManager = $logManager;
	}

	public function onDeletePost($postID): void
	{
		$postType = get_post_type($postID);

		$this->logManager->createLog(LogType::INFO, sprintf("Delete post type: %s", $postType));

		if($postType === 'product'){
			$this->logManager->createLog(LogType::INFO, sprintf("POST TYPE PRODUCT "));
			$this->deleteProduct($postID);
		}
	}

	public function deleteOrder($orderId): void
	{
		$resourceId = get_post_meta($orderId, MetadataHelper::getResourceId(), true);
		$this->logManager->createLog(LogType::INFO, sprintf("Delete order: %s", $resourceId));

		if(!$resourceId){
			return;
		}

		$url = sprintf("/stores/%s/orders/%s", $this->config->getShopId(), $resourceId);

		try {
			$this->apiClient->deleteOrder($url);
			delete_post_meta($orderId, MetadataHelper::getResourceId());
			delete_post_meta($orderId, MetadataHelper::getSynchronizationStatus());
			update_post_meta($orderId, MetadataHelper::isResourceCreated(), false);
		}catch(RequestLimitException $e){
			$this->logManager->createLog(LogType::ERROR, sprintf("Error in deleteOrder. Request limit exceeded: %s", $e->getMessage()));
			wp_die(__( 'Se ha excedido el límite de peticiones al API. Por favor, inténtalo de nuevo más tarde o prueba con menos entradas.', 'easymailing' ));
		}catch(\Exception $e){
			$this->logManager->createLog(LogType::ERROR, sprintf("Error in deleteOrder: %s", $e->getMessage()));
		}

	}

	public function deleteProduct($productId): void
	{
		$resourceId = get_post_meta($productId, MetadataHelper::getResourceId(), true);
		$this->logManager->createLog(LogType::INFO, sprintf("Delete product: %s", $resourceId));

		if(!$resourceId){
			return;
		}

		$url = sprintf("/stores/%s/products/%s", $this->config->getShopId(), $resourceId);

		try {
			$this->apiClient->deleteProduct($url);
			delete_post_meta($productId, MetadataHelper::getResourceId());
			delete_post_meta($productId, MetadataHelper::getSynchronizationStatus());
			update_post_meta($productId, MetadataHelper::isResourceCreated(), false);
		}catch(RequestLimitException $e){
			$this->logManager->createLog(LogType::ERROR, sprintf("Error in deleteProduct. Request limit exceeded: %s", $e->getMessage()));
			wp_die(__( 'Se ha excedido el límite de peticiones al API. Por favor, inténtalo de nuevo más tarde o prueba con menos entradas.', 'easymailing' ));
		}catch(\Exception $e){
			$this->logManager->createLog(LogType::ERROR, sprintf("Error in deleteProduct: %s", $e->getMessage()));
		}

	}

	public function deleteUser($userId)
	{
		$resourceId = get_user_meta($userId, MetadataHelper::getResourceId(), true);
		$this->logManager->createLog(LogType::INFO, sprintf("Delete user: %s", $userId));

		if(!$resourceId){
			return;
		}

		$url = sprintf("/stores/%s/customers/%s", $this->config->getShopId(), $resourceId);

		try {
			$this->apiClient->deleteUser($url);

			delete_user_meta($userId, MetadataHelper::getResourceId());
			delete_user_meta($userId, MetadataHelper::getSynchronizationStatus());
			update_user_meta($userId, MetadataHelper::isResourceCreated(), false);
		}catch(RequestLimitException $e){
			$this->logManager->createLog(LogType::ERROR, sprintf("Error in deleteUser. Request limit exceeded: %s", $e->getMessage()));
			wp_die(__( 'Se ha excedido el límite de peticiones al API. Por favor, inténtalo de nuevo más tarde o prueba con menos entradas.', 'easymailing' ));
		}catch(\Exception $e){
			$this->logManager->createLog(LogType::ERROR, sprintf("Error in deleteUser: %s", $e->getMessage()));
		}

	}
}
