<?php

namespace PixelioCartAbandonRecoveryMore\Admin\Controllers;

use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_User_Consent_Repository;

if (!defined('ABSPATH')) exit;

class Pixelio_Cart_Abandon_Recovery_More_Rgpd_Ajax_Controller
{
	private Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $pixelio_wordpress_wrapper;
	private Pixelio_Cart_Abandon_Recovery_More_User_Consent_Repository $repository;

	public function __construct(Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $wordpress_wrapper)
	{
		$this->pixelio_wordpress_wrapper = $wordpress_wrapper;
		$this->repository = new Pixelio_Cart_Abandon_Recovery_More_User_Consent_Repository($wordpress_wrapper);

		$this->pixelio_wordpress_wrapper->pixelio_add_action('wp_ajax_pixelio_rgpd_export_contact', [$this, 'pixelio_export_contact']);
		$this->pixelio_wordpress_wrapper->pixelio_add_action('wp_ajax_pixelio_rgpd_unsubscribe_contact', [$this, 'pixelio_unsubscribe_contact']);
		$this->pixelio_wordpress_wrapper->pixelio_add_action('wp_ajax_pixelio_rgpd_anonymize_contact', [$this, 'pixelio_anonymize_contact']);
	}

	private function pixelio_get_contact_id_from_request(): int
	{
		$this->pixelio_wordpress_wrapper->pixelio_check_ajax_referer('pixelio_rgpd_nonce', 'nonce');

		if (!current_user_can('manage_options')) {
			$this->pixelio_wordpress_wrapper->pixelio_wp_send_json_error([
				'message' => __('Unauthorized', 'rivio-ai-cart-recovery-marketing-email-automation')
			]);
			$this->pixelio_wordpress_wrapper->pixelio_wp_die();
		}

		$contact_id = isset($_POST['contact_id']) ? absint($_POST['contact_id']) : 0;

		if ($contact_id <= 0) {
			$this->pixelio_wordpress_wrapper->pixelio_wp_send_json_error([
				'message' => __('Invalid contact.', 'rivio-ai-cart-recovery-marketing-email-automation')
			]);
			$this->pixelio_wordpress_wrapper->pixelio_wp_die();
		}

		return $contact_id;
	}

	public function pixelio_export_contact(): void
	{
		$contact_id = $this->pixelio_get_contact_id_from_request();
		$export = $this->repository->pixelio_get_contact_export_by_id($contact_id);

		if (empty($export)) {
			$this->pixelio_wordpress_wrapper->pixelio_wp_send_json_error([
				'message' => __('Contact not found.', 'rivio-ai-cart-recovery-marketing-email-automation')
			]);
			$this->pixelio_wordpress_wrapper->pixelio_wp_die();
		}

		$this->pixelio_wordpress_wrapper->pixelio_wp_send_json_success($export);
		$this->pixelio_wordpress_wrapper->pixelio_wp_die();
	}

	public function pixelio_unsubscribe_contact(): void
	{
		$contact_id = $this->pixelio_get_contact_id_from_request();
		$done = $this->repository->pixelio_unsubscribe_by_contact_id($contact_id);

		if (!$done) {
			$this->pixelio_wordpress_wrapper->pixelio_wp_send_json_error([
				'message' => __('Unable to unsubscribe this contact.', 'rivio-ai-cart-recovery-marketing-email-automation')
			]);
			$this->pixelio_wordpress_wrapper->pixelio_wp_die();
		}

		$this->pixelio_wordpress_wrapper->pixelio_wp_send_json_success([
			'message' => __('Contact unsubscribed successfully.', 'rivio-ai-cart-recovery-marketing-email-automation')
		]);
		$this->pixelio_wordpress_wrapper->pixelio_wp_die();
	}

	public function pixelio_anonymize_contact(): void
	{
		$contact_id = $this->pixelio_get_contact_id_from_request();
		$done = $this->repository->pixelio_anonymize_contact_by_id($contact_id);

		if (!$done) {
			$this->pixelio_wordpress_wrapper->pixelio_wp_send_json_error([
				'message' => __('Unable to anonymize this contact.', 'rivio-ai-cart-recovery-marketing-email-automation')
			]);
			$this->pixelio_wordpress_wrapper->pixelio_wp_die();
		}

		$this->pixelio_wordpress_wrapper->pixelio_wp_send_json_success([
			'message' => __('Contact anonymized successfully.', 'rivio-ai-cart-recovery-marketing-email-automation')
		]);
		$this->pixelio_wordpress_wrapper->pixelio_wp_die();
	}
}
