<?php
/**
 * Cart Recovery — WP Privacy integration (GDPR eraser + exporter)
 *
 * Registers with WordPress's Personal Data Eraser / Exporter system so
 * the site-wide "Tools → Erase Personal Data" and "Export Personal
 * Data" flows include cart-recovery data for any email address.
 *
 * Eraser deletes:
 *   - every `niwa_ai_cart_recovery` row for that email
 *   - the suppression record (if present)
 *
 * Exporter includes (session id, email, name, locale, status, first
 * seen / last activity / sent / recovered timestamps, opens, clicks,
 * recovered order id). Cart snapshots are intentionally excluded —
 * those are merchandising data, not personal data owned by the
 * visitor.
 *
 * @package Niwa_AI
 * @since   0.12.0
 */

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

class Niwa_AI_Cart_Recovery_Privacy {

	public function __construct(
		private readonly Niwa_AI_Cart_Recovery_Repository $repository
	) {}

	public function register_hooks(): void {
		add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'register_exporter' ) );
		add_filter( 'wp_privacy_personal_data_erasers', array( $this, 'register_eraser' ) );
	}

	public function register_exporter( $exporters ) {
		if ( ! is_array( $exporters ) ) {
			$exporters = array();
		}
		$exporters['niwa-ai-cart-recovery'] = array(
			'exporter_friendly_name' => __( 'Niwa AI — Cart Recovery', 'niwa-ai' ),
			'callback'               => array( $this, 'export_personal_data' ),
		);
		return $exporters;
	}

	public function register_eraser( $erasers ) {
		if ( ! is_array( $erasers ) ) {
			$erasers = array();
		}
		$erasers['niwa-ai-cart-recovery'] = array(
			'eraser_friendly_name' => __( 'Niwa AI — Cart Recovery', 'niwa-ai' ),
			'callback'             => array( $this, 'erase_personal_data' ),
		);
		return $erasers;
	}

	/**
	 * WP Privacy exporter callback.
	 *
	 * @param string $email_address The email to export for.
	 * @param int    $page          1-based page number (we return everything on page 1).
	 * @return array
	 */
	public function export_personal_data( $email_address, $page = 1 ) {
		$email = strtolower( trim( (string) $email_address ) );
		$rows  = $page > 1 ? array() : $this->repository->export_for_email( $email );

		$export_items = array();
		foreach ( $rows as $row ) {
			$data = array();
			foreach ( $row as $key => $value ) {
				$data[] = array(
					'name'  => $this->pretty_label( (string) $key ),
					'value' => (string) $value,
				);
			}
			$export_items[] = array(
				'group_id'    => 'niwa-ai-cart-recovery',
				'group_label' => __( 'Cart Recovery', 'niwa-ai' ),
				'item_id'     => 'cart-recovery-' . (int) ( $row['id'] ?? 0 ),
				'data'        => $data,
			);
		}

		return array(
			'data' => $export_items,
			'done' => true,
		);
	}

	/**
	 * WP Privacy eraser callback. Deletes every recovery row + the
	 * suppression entry for the email in one shot.
	 *
	 * @param string $email_address
	 * @param int    $page
	 * @return array
	 */
	public function erase_personal_data( $email_address, $page = 1 ) {
		$email = strtolower( trim( (string) $email_address ) );
		$count = $page > 1 ? 0 : $this->repository->purge_for_email( $email );

		return array(
			'items_removed'  => $count,
			'items_retained' => 0,
			'messages'       => array(),
			'done'           => true,
		);
	}

	private function pretty_label( string $key ): string {
		$map = array(
			'id'                 => __( 'Row ID', 'niwa-ai' ),
			'session_id'         => __( 'Session', 'niwa-ai' ),
			'email'              => __( 'Email', 'niwa-ai' ),
			'customer_name'      => __( 'Name', 'niwa-ai' ),
			'locale'             => __( 'Locale', 'niwa-ai' ),
			'status'             => __( 'Status', 'niwa-ai' ),
			'first_seen_at'      => __( 'First seen', 'niwa-ai' ),
			'last_activity_at'   => __( 'Last activity', 'niwa-ai' ),
			'mail1_sent_at'      => __( 'First reminder sent at', 'niwa-ai' ),
			'mail2_sent_at'      => __( 'Follow-up sent at', 'niwa-ai' ),
			'recovered_at'       => __( 'Recovered at', 'niwa-ai' ),
			'recovered_order_id' => __( 'Recovery order ID', 'niwa-ai' ),
			'opens'              => __( 'Opens', 'niwa-ai' ),
			'clicks'             => __( 'Clicks', 'niwa-ai' ),
		);
		return $map[ $key ] ?? ucwords( str_replace( '_', ' ', $key ) );
	}
}
