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

namespace DatabaseCleanup\Includes\Tasks;

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

class Task_Duplicated_Termmeta extends Abstract_Cleanup_Task {

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

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

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