<?php
/**
 * Duplicated user meta cleanup task.
 *
 * @package DatabaseCleanup
 */

namespace DatabaseCleanup\Includes\Tasks;

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

class Task_Duplicated_Usermeta extends Abstract_Cleanup_Task {

	public function get_id(): string {
		return 'duplicated_usermeta';
	}

	public function get_label(): string {
		return __( 'Duplicated user meta', 'wp-powersuite' );
	}

	public function get_description(): string {
		return __( 'Remove duplicate usermeta rows that share the same user, key, and value.', 'wp-powersuite' );
	}

	public function is_safe(): bool {
		return true;
	}

	public function scan( array $settings ): array {
		global $wpdb;

		unset( $settings );

		$result = $this->scan_duplicate_meta( $wpdb->usermeta, 'user_id' );

		return [
			'count'           => $result['count'],
			'estimated_bytes' => $this->estimate_row_bytes( $result['count'], 512 ),
			'details'         => $result['details'],
		];
	}

	public function clean( array $settings, int $batch_size, mixed $cursor = null ): array {
		global $wpdb;

		unset( $settings );

		return $this->clean_duplicate_meta( $wpdb->usermeta, 'umeta_id', 'user_id', 'user', $batch_size, $cursor );
	}
}
