<?php

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

// Cargar la configuración de WordPress si es necesario
require_once __DIR__ . '/vendor/autoload.php';
@require 'helpers.php';

use Easymailing\App\Core\Log\LogDatabase;
use Easymailing\App\Core\Provider\Woocommerce\Service\MetadataHelper;
use Easymailing\App\Utils\Security;


$parameters = require (easymailing_plugin_directory()."config/app.parameters_production.php") ?: [];

$config = get_site_option(MetadataHelper::getConfiguration());
$synchronizationConfig = get_site_option(MetadataHelper::getSynchronizationConfig());

$apiKey = null;
if($config instanceof \Easymailing\App\Model\Config){
	$apiKey = Security::safeDecrypt($config->getApiKey(), $parameters['encrypt_key']);
}

if($synchronizationConfig instanceof \Easymailing\App\Model\SynchronizationConfig && $synchronizationConfig->getShopId() && $apiKey){
	$url = sprintf('%s/stores/%s', $parameters['api_url'], $synchronizationConfig->getShopId());
	$wpResponse = wp_remote_get($url, [
		'timeout' => 10,
		'method'  => 'DELETE',
		'headers' => [
			'Content-Type' => 'application/ld+json',
			'X-Auth-Token' => $apiKey,
		],
	]);
}

// Eliminar opciones de la base de datos
delete_option(MetadataHelper::getConfiguration());

// Eliminar metadatos y logs
MetadataHelper::deleteMetadata();
LogDatabase::deleteTable();
