<?php
namespace WPVOID\StockBeacon\Integrations;

use WPVOID\StockBeacon\Core\WaitlistManager;

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

class GDPRHandler {
	private WaitlistManager $manager;
	public function __construct( WaitlistManager $manager ) { $this->manager = $manager; }
	public function register(): void {
		add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'exporters' ) );
		add_filter( 'wp_privacy_personal_data_erasers', array( $this, 'erasers' ) );
		add_action( 'admin_init', array( $this, 'add_privacy_policy_content' ) );
	}
	public function add_privacy_policy_content(): void {
		$content  = '<p>' . esc_html__( 'StockBeacon stores waitlist email addresses, product and variation IDs, WordPress user IDs when available, IP addresses, locales, consent choices, subscription statuses, and related timestamps in this site’s database. This data is used to manage waitlists and send confirmation and restock emails.', 'stockbeacon-product-waitlists-for-woocommerce' ) . '</p>';
		$content .= '<p>' . esc_html__( 'The store owner controls retention. Depending on the configured retention option, subscription records can be kept or removed after a restock email; retained activity logs have the subscriber email removed when the subscription is removed. Administrators can also use WordPress personal-data export and erasure tools for a subscriber’s email address.', 'stockbeacon-product-waitlists-for-woocommerce' ) . '</p>';
		$content .= '<p>' . esc_html__( 'Waitlist emails are delivered through this site’s WordPress mail configuration, so subscriber data may be processed by the mail provider selected by the store owner. Optional integrations configured by the store owner may also process waitlist data. Waitlist subscriber data is not sent to Freemius.', 'stockbeacon-product-waitlists-for-woocommerce' ) . '</p>';

		wp_add_privacy_policy_content(
			esc_html__( 'StockBeacon - Product Waitlists for WooCommerce', 'stockbeacon-product-waitlists-for-woocommerce' ),
			wp_kses_post( $content )
		);
	}
	public function exporters( array $exporters ): array {
		$exporters['stockbeacon-product-waitlists-for-woocommerce'] = array( 'exporter_friendly_name' => __( 'Waitlist data', 'stockbeacon-product-waitlists-for-woocommerce' ), 'callback' => array( $this, 'export' ) );
		return $exporters;
	}
	public function erasers( array $erasers ): array {
		$erasers['stockbeacon-product-waitlists-for-woocommerce'] = array( 'eraser_friendly_name' => __( 'Waitlist data', 'stockbeacon-product-waitlists-for-woocommerce' ), 'callback' => array( $this, 'erase' ) );
		return $erasers;
	}
	public function export( string $email ): array {
		$rows = $this->manager->get_by_email( $email );
		$data = array();
		foreach ( $rows as $row ) {
			$product_name = get_the_title( (int) $row->product_id );
			$item_id      = 'stockbeacon-subscription-' . (int) $row->id;
			$data[]       = array(
				'group_id'    => 'stockbeacon-product-waitlists-for-woocommerce',
				'group_label' => __( 'Waitlist subscriptions', 'stockbeacon-product-waitlists-for-woocommerce' ),
				'item_id'     => $item_id,
				'data'        => array(
					array( 'name' => __( 'Email address', 'stockbeacon-product-waitlists-for-woocommerce' ), 'value' => (string) $row->email ),
					array( 'name' => __( 'Product', 'stockbeacon-product-waitlists-for-woocommerce' ), 'value' => $product_name ),
					array( 'name' => __( 'Product ID', 'stockbeacon-product-waitlists-for-woocommerce' ), 'value' => (string) $row->product_id ),
					array( 'name' => __( 'Variation ID', 'stockbeacon-product-waitlists-for-woocommerce' ), 'value' => (string) $row->variation_id ),
					array( 'name' => __( 'WordPress user ID', 'stockbeacon-product-waitlists-for-woocommerce' ), 'value' => (string) $row->user_id ),
					array( 'name' => __( 'Status', 'stockbeacon-product-waitlists-for-woocommerce' ), 'value' => (string) $row->status ),
					array( 'name' => __( 'IP address', 'stockbeacon-product-waitlists-for-woocommerce' ), 'value' => (string) $row->ip_address ),
					array( 'name' => __( 'Locale', 'stockbeacon-product-waitlists-for-woocommerce' ), 'value' => (string) $row->locale ),
					array( 'name' => __( 'Consent recorded', 'stockbeacon-product-waitlists-for-woocommerce' ), 'value' => ! empty( $row->consent ) ? __( 'Yes', 'stockbeacon-product-waitlists-for-woocommerce' ) : __( 'No', 'stockbeacon-product-waitlists-for-woocommerce' ) ),
					array( 'name' => __( 'Subscribed', 'stockbeacon-product-waitlists-for-woocommerce' ), 'value' => (string) $row->created_at ),
					array( 'name' => __( 'Opted in', 'stockbeacon-product-waitlists-for-woocommerce' ), 'value' => (string) $row->opted_in_at ),
					array( 'name' => __( 'Notified', 'stockbeacon-product-waitlists-for-woocommerce' ), 'value' => (string) $row->notified_at ),
					array( 'name' => __( 'Purchased', 'stockbeacon-product-waitlists-for-woocommerce' ), 'value' => (string) $row->purchased_at ),
				),
			);
		}
		return array( 'data' => $data, 'done' => true );
	}
	public function erase( string $email ): array {
		$removed = $this->manager->erase_by_email( $email );
		return array(
			'items_removed'  => $removed > 0,
			'items_retained' => false,
			/* translators: %d is the number of erased waitlist records. */
			'messages'       => $removed > 0 ? array( sprintf( __( 'Removed %d waitlist record(s).', 'stockbeacon-product-waitlists-for-woocommerce' ), $removed ) ) : array(),
			'done'           => true,
		);
	}
}
