<?php
/**
 * Privacy controller for GDPR compliance.
 *
 * Registers WordPress personal data exporters and erasers for member data.
 *
 * @package Awesomemotive\Rewardswp\Controllers
 * @since 1.4.0
 */

namespace Awesomemotive\Rewardswp\Controllers;

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

use Awesomemotive\Rewardswp\Interfaces\ControllerInterface;
use Awesomemotive\Rewardswp\Services\BirthdayService;
use function Awesomemotive\Rewardswp\Helpers\get_member_by;

/**
 * Privacy Controller.
 *
 * @since 1.4.0
 */
class PrivacyController implements ControllerInterface {

	/**
	 * Birthday service instance.
	 *
	 * @since 1.4.0
	 * @var BirthdayService
	 */
	private BirthdayService $birthday_service;

	/**
	 * Constructor.
	 *
	 * @since 1.4.0
	 *
	 * @param BirthdayService $birthday_service Birthday service instance.
	 */
	public function __construct( BirthdayService $birthday_service ) {
		$this->birthday_service = $birthday_service;
	}

	/**
	 * Initialize the controller.
	 *
	 * @since 1.4.0
	 */
	public function init() {
		add_filter( 'wp_privacy_personal_data_exporters', [ $this, 'register_exporter' ] );
		add_filter( 'wp_privacy_personal_data_erasers', [ $this, 'register_eraser' ] );
	}

	/**
	 * Register the personal data exporter.
	 *
	 * @since 1.4.0
	 *
	 * @param array $exporters Registered exporters.
	 * @return array
	 */
	public function register_exporter( array $exporters ): array {
		$exporters['rewardswp-member-data'] = [
			'exporter_friendly_name' => __( 'RewardsWP Member Data', 'rewardswp' ),
			'callback'               => [ $this, 'export_member_data' ],
		];

		return $exporters;
	}

	/**
	 * Register the personal data eraser.
	 *
	 * @since 1.4.0
	 *
	 * @param array $erasers Registered erasers.
	 * @return array
	 */
	public function register_eraser( array $erasers ): array {
		$erasers['rewardswp-member-data'] = [
			'eraser_friendly_name' => __( 'RewardsWP Member Data', 'rewardswp' ),
			'callback'             => [ $this, 'erase_member_data' ],
		];

		return $erasers;
	}

	/**
	 * Export member personal data.
	 *
	 * @since 1.4.0
	 *
	 * @param string $email_address The user's email address.
	 * @param int    $page          Page number for batch processing.
	 * @return array Export data.
	 */
	public function export_member_data( string $email_address, int $page = 1 ): array {
		$data_to_export = [];

		$member = get_member_by( 'email', $email_address );
		if ( ! $member || empty( $member->id ) ) {
			return [
				'data' => $data_to_export,
				'done' => true,
			];
		}

		$member_data = [];

		// Export birthday data.
		$birthday = $member->get_meta( BirthdayService::BIRTHDAY_META_KEY );
		if ( ! empty( $birthday ) ) {
			$member_data[] = [
				'name'  => __( 'Birthday', 'rewardswp' ),
				'value' => $this->birthday_service->format_birthday_for_display( $birthday ),
			];
		}

		$birthday_set_at = $member->get_meta( BirthdayService::BIRTHDAY_SET_AT_META_KEY );
		if ( ! empty( $birthday_set_at ) ) {
			$member_data[] = [
				'name'  => __( 'Birthday Set Date', 'rewardswp' ),
				'value' => $birthday_set_at,
			];
		}

		// Export basic member data.
		$member_data[] = [
			'name'  => __( 'Member Email', 'rewardswp' ),
			'value' => $member->email,
		];

		$member_data[] = [
			'name'  => __( 'Points Balance', 'rewardswp' ),
			'value' => (string) ( $member->points_balance ?? 0 ),
		];

		if ( ! empty( $member_data ) ) {
			$data_to_export[] = [
				'group_id'          => 'rewardswp',
				'group_label'       => __( 'RewardsWP Member Data', 'rewardswp' ),
				'group_description' => __( 'Rewards and loyalty program data.', 'rewardswp' ),
				'item_id'           => 'rewardswp-member-' . $member->id,
				'data'              => $member_data,
			];
		}

		return [
			'data' => $data_to_export,
			'done' => true,
		];
	}

	/**
	 * Erase member personal data.
	 *
	 * @since 1.4.0
	 *
	 * @param string $email_address The user's email address.
	 * @param int    $page          Page number for batch processing.
	 * @return array Erasure result.
	 */
	public function erase_member_data( string $email_address, int $page = 1 ): array {
		$items_removed  = 0;
		$items_retained = 0;

		$member = get_member_by( 'email', $email_address );
		if ( ! $member || empty( $member->id ) ) {
			return [
				'items_removed'  => $items_removed,
				'items_retained' => $items_retained,
				'messages'       => [],
				'done'           => true,
			];
		}

		// Erase birthday data.
		$birthday = $member->get_meta( BirthdayService::BIRTHDAY_META_KEY );
		if ( ! empty( $birthday ) ) {
			$member->delete_meta( BirthdayService::BIRTHDAY_META_KEY );
			$member->delete_meta( BirthdayService::BIRTHDAY_SET_AT_META_KEY );
			$member->delete_meta( BirthdayService::BIRTHDAY_UPDATED_AT_META_KEY );
			$items_removed++;
		}

		return [
			'items_removed'  => $items_removed,
			'items_retained' => $items_retained,
			'messages'       => [],
			'done'           => true,
		];
	}
}
