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

namespace DatabaseCleanup\Includes\Tasks;

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

class Task_Duplicated_Commentmeta extends Abstract_Cleanup_Task {

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

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

	public function get_description(): string {
		return __( 'Remove duplicate commentmeta rows that share the same comment, 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->commentmeta, 'comment_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->commentmeta, 'meta_id', 'comment_id', 'comment', $batch_size, $cursor );
	}
}
