<?php
/**
 * @package Hafenstudios\Core
 */

namespace Hafenstudios\Core\Privacy;

use Hafenstudios\Core\Core;
use Hafenstudios\Core\Events\EventBus;

defined( 'ABSPATH' ) || exit;

/**
 * Bindet die Flotte an die WordPress-Datenschutzwerkzeuge (Export und Loeschung)
 * und stellt die Consent-Daten bereit. Plugins haengen ihre eigenen Daten ueber
 * die Flotten-Events `privacy_export` und `privacy_erase` an.
 */
final class PrivacyManager {

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

	/**
	 * @param array<string, mixed> $exporters Registrierte Exporter.
	 * @return array<string, mixed>
	 */
	public function register_exporter( array $exporters ): array {
		$exporters['hafenstudios-core'] = array(
			'exporter_friendly_name' => __( 'Hafenstudios Flotte', 'hafenstudios-core' ),
			'callback'               => array( $this, 'export' ),
		);
		return $exporters;
	}

	/**
	 * @param array<string, mixed> $erasers Registrierte Eraser.
	 * @return array<string, mixed>
	 */
	public function register_eraser( array $erasers ): array {
		$erasers['hafenstudios-core'] = array(
			'eraser_friendly_name' => __( 'Hafenstudios Flotte', 'hafenstudios-core' ),
			'callback'             => array( $this, 'erase' ),
		);
		return $erasers;
	}

	/**
	 * Exportiert Consent-Daten plus per Event angehaengte Plugin-Daten.
	 *
	 * @param string $email E-Mail-Adresse.
	 * @param int    $page  Seite (Batching).
	 * @return array{data:array<int,array<string,mixed>>,done:bool}
	 */
	public function export( string $email, int $page = 1 ): array {
		$items = array();

		foreach ( Core::instance()->consent()->find_by_email( $email ) as $consent ) {
			$items[] = array(
				'group_id'    => 'hs_consents',
				'group_label' => __( 'Einwilligungen', 'hafenstudios-core' ),
				'item_id'     => 'consent-' . $consent['id'],
				'data'        => array(
					array(
						'name'  => __( 'Zweck', 'hafenstudios-core' ),
						'value' => $consent['purpose'],
					),
					array(
						'name'  => __( 'Quelle', 'hafenstudios-core' ),
						'value' => $consent['source'],
					),
					array(
						'name'  => __( 'Zeitpunkt', 'hafenstudios-core' ),
						'value' => $consent['created_at'],
					),
					array(
						'name'  => __( 'Einwilligungstext', 'hafenstudios-core' ),
						'value' => $consent['consent_text'],
					),
				),
			);
		}

		/**
		 * Plugins haengen hier ihre personenbezogenen Daten an.
		 *
		 * @param array<int, array<string, mixed>> $items Export-Items.
		 * @param string                           $email E-Mail.
		 * @param int                              $page  Seite.
		 */
		$items = EventBus::filter( 'privacy_export', $items, $email, $page );

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

	/**
	 * Loescht Consent-Daten plus per Event angehaengte Plugin-Daten.
	 *
	 * @param string $email E-Mail-Adresse.
	 * @param int    $page  Seite.
	 * @return array{items_removed:bool,items_retained:bool,messages:array<int,string>,done:bool}
	 */
	public function erase( string $email, int $page = 1 ): array {
		$removed = Core::instance()->consent()->delete_by_email( $email );

		$result = array(
			'items_removed'  => $removed > 0,
			'items_retained' => false,
			'messages'       => array(),
			'done'           => true,
		);

		/**
		 * Plugins loeschen hier ihre personenbezogenen Daten und melden das Ergebnis.
		 *
		 * @param array<string, mixed> $result Loeschergebnis.
		 * @param string               $email  E-Mail.
		 * @param int                  $page   Seite.
		 */
		return EventBus::filter( 'privacy_erase', $result, $email, $page );
	}
}
