<?php

namespace WPProofShield\Support;

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

final class PrivacyTools {

	public function register(): void {
		add_filter( 'wp_privacy_personal_data_exporters', array( $this, 'registerExporters' ) );
		add_filter( 'wp_privacy_personal_data_erasers', array( $this, 'registerErasers' ) );
	}

	public function registerExporters( array $exporters ): array {
		$exporters['proofshield-settings'] = array(
			'exporter_friendly_name' => __( 'ProofShield Settings', 'proofshield' ),
			'callback'               => array( $this, 'exportPersonalData' ),
		);

		return $exporters;
	}

	public function registerErasers( array $erasers ): array {
		$erasers['proofshield-settings'] = array(
			'eraser_friendly_name' => __( 'ProofShield Settings', 'proofshield' ),
			'callback'             => array( $this, 'erasePersonalData' ),
		);

		return $erasers;
	}

	/**
	 * @return array{data:array<int,array<string,mixed>>,done:bool}
	 */
	public function exportPersonalData( string $emailAddress, int $page = 1 ): array {
		if ( $page > 1 ) {
			return array(
				'data' => array(),
				'done' => true,
			);
		}

		$email = sanitize_email( $emailAddress );
		if ( $email === '' ) {
			return array(
				'data' => array(),
				'done' => true,
			);
		}

		$settings = get_option( WPSH_OPTION_KEY, array() );
		if ( ! is_array( $settings ) ) {
			$settings = array();
		}

		$storedConnectEmail = sanitize_email( (string) ( $settings['admin_email_reference'] ?? $settings['admin_email_reference'] ?? '' ) );
		$adminEmail         = sanitize_email( (string) get_option( 'admin_email', '' ) );
		$isMatch            = ( $storedConnectEmail !== '' && strcasecmp( $storedConnectEmail, $email ) === 0 )
			|| ( $adminEmail !== '' && strcasecmp( $adminEmail, $email ) === 0 );

		if ( ! $isMatch ) {
			return array(
				'data' => array(),
				'done' => true,
			);
		}

		$items = array(
			array(
				'name'  => __( 'ProofShield stores only anonymized IP ranges for statistics (when enabled).', 'proofshield' ),
				'value' => __( 'No full IP addresses are stored in ProofShield statistics.', 'proofshield' ),
			),
			array(
				'name'  => __( 'Statistics retention', 'proofshield' ),
				'value' => __( '30 days (automatic cleanup).', 'proofshield' ),
			),
			array(
				'name'  => __( 'Administrator email reference', 'proofshield' ),
				'value' => $storedConnectEmail !== '' ? $storedConnectEmail : __( 'Not set', 'proofshield' ),
			),
			array(
				'name'  => __( 'Weekly statistics mail enabled', 'proofshield' ),
				'value' => ! empty( $settings['stats_weekly_email_enabled'] ) ? __( 'Yes', 'proofshield' ) : __( 'No', 'proofshield' ),
			),
		);

		return array(
			'data' => array(
				array(
					'group_id'    => 'proofshield-settings',
					'group_label' => __( 'ProofShield', 'proofshield' ),
					'item_id'     => 'proofshield-settings-' . md5( strtolower( $email ) ),
					'data'        => $items,
				),
			),
			'done' => true,
		);
	}

	/**
	 * @return array{items_removed:bool,items_retained:bool,messages:array<int,string>,done:bool}
	 */
	public function erasePersonalData( string $emailAddress, int $page = 1 ): array {
		if ( $page > 1 ) {
			return array(
				'items_removed'  => false,
				'items_retained' => false,
				'messages'       => array(),
				'done'           => true,
			);
		}

		$email = sanitize_email( $emailAddress );
		if ( $email === '' ) {
			return array(
				'items_removed'  => false,
				'items_retained' => false,
				'messages'       => array(),
				'done'           => true,
			);
		}

		$settings = get_option( WPSH_OPTION_KEY, array() );
		if ( ! is_array( $settings ) ) {
			$settings = array();
		}

		$storedConnectEmail = sanitize_email( (string) ( $settings['admin_email_reference'] ?? $settings['admin_email_reference'] ?? '' ) );
		if ( $storedConnectEmail !== '' && strcasecmp( $storedConnectEmail, $email ) === 0 ) {
			$settings['admin_email_reference'] = '';
			$settings['admin_email_reference'] = '';
			update_option( WPSH_OPTION_KEY, $settings, false );

			return array(
				'items_removed'  => true,
				'items_retained' => false,
				'messages'       => array(),
				'done'           => true,
			);
		}

		return array(
			'items_removed'  => false,
			'items_retained' => true,
			'messages'       => array(
				__( 'No directly erasable personal data for this email was found in ProofShield.', 'proofshield' ),
			),
			'done'           => true,
		);
	}
}
