<?php
/**
 * Privacy (GDPR) export and erasure support.
 */

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

class Capsq_Privacy {

	public function __construct() {
		add_action( 'admin_init', array( $this, 'register_privacy_policy' ) );
		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_privacy_policy() {
		if ( ! function_exists( 'wp_add_privacy_policy_content' ) ) {
			return;
		}

		wp_add_privacy_policy_content(
			__( 'Capsquery Facial Authentication for Administrator', 'capsquery-facial-authentication' ),
			wp_kses_post(
				'<p>' . __( 'When a user enrolls in Capsquery Facial Authentication, the plugin stores an encrypted mathematical face descriptor (128 numeric values) and a hashed PIN in the user meta table. No photographs are stored on the server. Face matching runs in the browser; only the descriptor is sent to the server for comparison.', 'capsquery-facial-authentication' ) . '</p>'
				. '<p>' . __( 'Administrators can delete all facial authentication data from Settings → Capsquery Facial Authentication. Users with access can remove their enrolled face from their profile page.', 'capsquery-facial-authentication' ) . '</p>'
			)
		);
	}

	public function register_exporter( $exporters ) {
		$exporters['capsquery-facial-authentication'] = array(
			'exporter_friendly_name' => __( 'Capsquery Facial Authentication Data', 'capsquery-facial-authentication' ),
			'callback'               => array( $this, 'export_data' ),
		);
		return $exporters;
	}

	public function register_eraser( $erasers ) {
		$erasers['capsquery-facial-authentication'] = array(
			'eraser_friendly_name' => __( 'Capsquery Facial Authentication Data', 'capsquery-facial-authentication' ),
			'callback'             => array( $this, 'erase_data' ),
		);
		return $erasers;
	}

	public function export_data( $email, $page = 1 ) {
		$user = get_user_by( 'email', $email );
		if ( ! $user ) {
			return array(
				'data' => array(),
				'done' => true,
			);
		}

		$has_face = (bool) get_user_meta( $user->ID, '_capsq_face_descriptor', true );
		$has_pin  = (bool) get_user_meta( $user->ID, '_capsq_pin_hash', true );

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

		$data = array(
			array(
				'group_id'    => 'capsquery-facial-authentication',
				'group_label' => __( 'Capsquery Facial Authentication', 'capsquery-facial-authentication' ),
				'item_id'     => 'capsquery-facial-authentication-' . $user->ID,
				'data'        => array(
					array(
						'name'  => __( 'Face enrolled', 'capsquery-facial-authentication' ),
						'value' => $has_face ? __( 'Yes (encrypted descriptor stored)', 'capsquery-facial-authentication' ) : __( 'No', 'capsquery-facial-authentication' ),
					),
					array(
						'name'  => __( 'PIN configured', 'capsquery-facial-authentication' ),
						'value' => $has_pin ? __( 'Yes (hashed)', 'capsquery-facial-authentication' ) : __( 'No', 'capsquery-facial-authentication' ),
					),
				),
			),
		);

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

	public function erase_data( $email, $page = 1 ) {
		$user = get_user_by( 'email', $email );
		if ( ! $user ) {
			return array(
				'items_removed'  => false,
				'items_retained' => false,
				'messages'       => array(),
				'done'           => true,
			);
		}

		delete_user_meta( $user->ID, '_capsq_face_descriptor' );
		delete_user_meta( $user->ID, '_capsq_pin_hash' );

		return array(
			'items_removed'  => true,
			'items_retained' => false,
			'messages'       => array( __( 'Capsquery Facial Authentication data removed.', 'capsquery-facial-authentication' ) ),
			'done'           => true,
		);
	}
}
