<?php

use PixelioCartAbandonRecoveryMore\Admin\Views\Reusable\Pixelio_Cart_Abandon_Recovery_More_Header;
use PixelioCartAbandonRecoveryMore\Admin\Views\Reusable\Pixelio_Cart_Abandon_Recovery_More_Admin_Loader_Spinner;
use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_User_Consent_Repository;
use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;

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

$pixelio_wordpress_wrapper = new Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper();
$pixelio_util = new Pixelio_Cart_Abandon_Recovery_More_Util($pixelio_wordpress_wrapper);

$header = new Pixelio_Cart_Abandon_Recovery_More_Header($pixelio_util);
$header->pixelio_cart_abandon_recovery_more_get_views();

$loader = new Pixelio_Cart_Abandon_Recovery_More_Admin_Loader_Spinner(
	__("Please wait until the operation is completed", "rivio-ai-cart-recovery-marketing-email-automation")
);
$loader->pixelio_cart_abandon_recovery_more_get_views();

$repository = new Pixelio_Cart_Abandon_Recovery_More_User_Consent_Repository($pixelio_wordpress_wrapper);
$contacts = $repository->pixelio_get_rgpd_contacts();
$stats = $repository->pixelio_get_rgpd_stats();
?>

<div class="pixelio-cart-abandon-recovery-more-card pixelio-rgpd-card">
	<div class="pixelio-cart-abandon-recovery-more-card-header">
		<div>
			<h2 class="pixelio-cart-abandon-recovery-more-card-title">
				<?php esc_html_e('Consent & GDPR Center', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>
			</h2>
			<p class="pixelio-rgpd-subtitle">
				<?php esc_html_e('Manage consent history, exports, unsubscribes and anonymization from one place.', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>
			</p>
		</div>
	</div>

	<div class="pixelio-rgpd-stats-grid">
		<div class="pixelio-rgpd-stat-card">
			<span><?php esc_html_e('Contacts', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></span>
			<strong><?php echo esc_html($stats->total); ?></strong>
		</div>

		<div class="pixelio-rgpd-stat-card">
			<span><?php esc_html_e('Active', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></span>
			<strong><?php echo esc_html($stats->active); ?></strong>
		</div>

		<div class="pixelio-rgpd-stat-card">
			<span><?php esc_html_e('Unsubscribed', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></span>
			<strong><?php echo esc_html($stats->unsubscribed); ?></strong>
		</div>

		<div class="pixelio-rgpd-stat-card">
			<span><?php esc_html_e('Anonymized', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></span>
			<strong><?php echo esc_html($stats->anonymized); ?></strong>
		</div>
	</div>

	<hr class="pixelio-cart-abandon-recovery-more-card-separator"/>

	<table id="pixelio-rgpd-table" class="display" style="width:100%">
		<thead>
		<tr>
			<th><?php esc_html_e('Email', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></th>
			<th><?php esc_html_e('Consent logs', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></th>
			<th><?php esc_html_e('Status', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></th>
			<th><?php esc_html_e('Created at', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></th>
			<th><?php esc_html_e('Deleted at', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></th>
			<th><?php esc_html_e('Actions', 'rivio-ai-cart-recovery-marketing-email-automation'); ?></th>
		</tr>
		</thead>

		<tbody>
		<?php foreach ($contacts as $contact):
			$is_anonymized = !empty($contact->deleted_at);
			$is_unsubscribed = (int) $contact->global_unsubscribed === 1;
			?>
			<tr data-contact-id="<?php echo esc_attr($contact->id); ?>">
				<td><?php echo esc_html($contact->email); ?></td>
				<td><?php echo esc_html($contact->consent_count); ?></td>
				<td>
					<?php if (!empty($contact->deleted_at)): ?>
						<span class="pixelio-rgpd-badge pixelio-rgpd-badge-danger">
							<?php esc_html_e('Anonymized', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>
						</span>
					<?php elseif ((int) $contact->global_unsubscribed === 1): ?>
						<span class="pixelio-rgpd-badge pixelio-rgpd-badge-warning">
							<?php esc_html_e('Unsubscribed', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>
						</span>
					<?php else: ?>
						<span class="pixelio-rgpd-badge pixelio-rgpd-badge-success">
							<?php esc_html_e('Active', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>
						</span>
					<?php endif; ?>
				</td>
				<td><?php echo esc_html($contact->created_at); ?></td>
				<td><?php echo esc_html($contact->deleted_at ?: '-'); ?></td>

				<td class="pixelio-rgpd-table-actions">
					<?php if (!$is_anonymized): ?>
						<button class="pixelio-rgpd-btn pixelio-rgpd-export" data-contact-id="<?php echo esc_attr($contact->id); ?>">
							<?php esc_html_e('Export', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>
						</button>
					<?php endif; ?>

					<?php if (!$is_anonymized && !$is_unsubscribed): ?>
						<button class="pixelio-rgpd-btn pixelio-rgpd-unsubscribe" data-contact-id="<?php echo esc_attr($contact->id); ?>">
							<?php esc_html_e('Unsubscribe', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>
						</button>
					<?php endif; ?>

					<?php if (!$is_anonymized): ?>
						<button class="pixelio-rgpd-btn pixelio-rgpd-danger pixelio-rgpd-anonymize" data-contact-id="<?php echo esc_attr($contact->id); ?>">
							<?php esc_html_e('Anonymize', 'rivio-ai-cart-recovery-marketing-email-automation'); ?>
						</button>
					<?php endif; ?>
				</td>
			</tr>
		<?php endforeach; ?>
		</tbody>
	</table>
</div>


