<?php

namespace VendorTasksDokanClickUp\Privacy;

use VendorTasksDokanClickUp\VendorIntegration\VendorTaskState;

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

/**
 * GDPR Personal Data Eraser
 *
 * Registers with WordPress's built-in personal data erasure system.
 * On erasure request, deletes the vendor's ClickUp task and clears all
 * _vendor_tasks_dokan_clickup_* user meta.
 *
 * @since 1.1.0
 * @package VendorTasksDokanClickUp
 * @subpackage Privacy
 */
class PersonalDataEraser {

	/**
	 * Class constructor
	 *
	 * @since 1.1.0
	 */
	public function __construct() {
		add_filter( 'wp_privacy_personal_data_erasers', array( $this, 'register_eraser' ) );
	}

	/**
	 * Register the personal data eraser
	 *
	 * @since 1.1.0
	 *
	 * @param array $erasers Registered erasers.
	 * @return array
	 */
	public function register_eraser( array $erasers ): array {
		$erasers['alamin-vendor-tasks-dokan-clickup'] = array(
			'eraser_friendly_name' => __( 'Dokan Vendor ClickUp Integration', 'alamin-vendor-tasks-dokan-clickup' ),
			'callback'             => array( $this, 'erase_data' ),
		);
		return $erasers;
	}

	/**
	 * Erase ClickUp task and user meta for a given email address
	 *
	 * @since 1.1.0
	 *
	 * @param string $email_address Email address to erase.
	 * @param int    $page          Page number (unused — single operation).
	 * @return array{items_removed: bool, items_retained: bool, messages: string[], done: bool}
	 */
	public function erase_data( string $email_address, int $page = 1 ): array {
		$response = array(
			'items_removed'  => false,
			'items_retained' => false,
			'messages'       => array(),
			'done'           => true,
		);

		$user = get_user_by( 'email', $email_address );
		if ( ! $user ) {
			return $response;
		}

		$user_id    = (int) $user->ID;
		$task_state = new VendorTaskState();
		$task_id    = $task_state->get_task_id( $user_id );

		if ( ! empty( $task_id ) ) {
			$client = vendor_tasks_dokan_clickup()->get_clickup_client();

			if ( $client ) {
				$result = $client->delete_task( $task_id );

				if ( is_wp_error( $result ) ) {
					$response['items_retained'] = true;
					$response['messages'][]     = sprintf(
						/* translators: %s: error message */
						__( 'ClickUp task could not be deleted: %s', 'alamin-vendor-tasks-dokan-clickup' ),
						$result->get_error_message()
					);

					vendor_tasks_dokan_clickup()->log(
						sprintf( 'GDPR erasure: failed to delete task %s for user %d: %s', $task_id, $user_id, $result->get_error_message() ),
						'error'
					);
				} else {
					$response['items_removed'] = true;
					$response['messages'][]    = __( 'ClickUp vendor task deleted.', 'alamin-vendor-tasks-dokan-clickup' );
				}
			}
		}

		// Always remove local meta regardless of API outcome
		$had_meta = $task_state->has_task( $user_id ) || ! empty( $task_state->get_task_id( $user_id ) );
		$task_state->clear_task( $user_id );
		if ( $had_meta ) {
			$response['items_removed'] = true;
		}

		// Remove from retry queue if present
		( new \VendorTasksDokanClickUp\Queue\PendingQueue() )->dequeue( $user_id );

		return $response;
	}
}
