<?php
/**
 * Bulk Operations.
 *
 * @package TrustLens
 * @since   1.1.0
 */

defined( 'ABSPATH' ) || exit;

/**
 * Bulk Operations class.
 *
 * Handles bulk actions on customers.
 *
 * @since 1.1.0
 */
class TrustLens_Bulk_Operations {

	/**
	 * Block multiple customers.
	 *
	 * @since 1.1.0
	 * @param array  $email_hashes Array of customer email hashes.
	 * @param string $reason       Block reason.
	 * @return array Results with success/failure counts.
	 */
	public static function bulk_block( array $email_hashes, string $reason = '' ): array {
		$results = array(
			'success' => 0,
			'failed'  => 0,
			'errors'  => array(),
		);

		foreach ( $email_hashes as $email_hash ) {
			$result = wstl_block_customer( $email_hash, $reason );
			if ( $result ) {
				$results['success']++;
			} else {
				$results['failed']++;
				$results['errors'][] = $email_hash;
			}
		}

		// Log bulk action.
		self::log_bulk_action( 'block', $email_hashes, $results, array( 'reason' => $reason ) );

		return $results;
	}

	/**
	 * Unblock multiple customers.
	 *
	 * @since 1.1.0
	 * @param array $email_hashes Array of customer email hashes.
	 * @return array Results with success/failure counts.
	 */
	public static function bulk_unblock( array $email_hashes ): array {
		$results = array(
			'success' => 0,
			'failed'  => 0,
			'errors'  => array(),
		);

		foreach ( $email_hashes as $email_hash ) {
			$result = wstl_unblock_customer( $email_hash );
			if ( $result ) {
				$results['success']++;
			} else {
				$results['failed']++;
				$results['errors'][] = $email_hash;
			}
		}

		self::log_bulk_action( 'unblock', $email_hashes, $results );

		return $results;
	}

	/**
	 * Add multiple customers to allowlist.
	 *
	 * @since 1.1.0
	 * @param array $email_hashes Array of customer email hashes.
	 * @return array Results.
	 */
	public static function bulk_allowlist( array $email_hashes ): array {
		$results = array(
			'success' => 0,
			'failed'  => 0,
			'errors'  => array(),
		);

		foreach ( $email_hashes as $email_hash ) {
			$result = wstl_allowlist_customer( $email_hash );
			if ( $result ) {
				$results['success']++;
			} else {
				$results['failed']++;
				$results['errors'][] = $email_hash;
			}
		}

		self::log_bulk_action( 'allowlist', $email_hashes, $results );

		return $results;
	}

	/**
	 * Remove multiple customers from allowlist.
	 *
	 * @since 1.1.0
	 * @param array $email_hashes Array of customer email hashes.
	 * @return array Results.
	 */
	public static function bulk_remove_allowlist( array $email_hashes ): array {
		$results = array(
			'success' => 0,
			'failed'  => 0,
			'errors'  => array(),
		);

		foreach ( $email_hashes as $email_hash ) {
			$result = wstl_remove_from_allowlist( $email_hash );
			if ( $result ) {
				$results['success']++;
			} else {
				$results['failed']++;
				$results['errors'][] = $email_hash;
			}
		}

		self::log_bulk_action( 'remove_allowlist', $email_hashes, $results );

		return $results;
	}

	/**
	 * Recalculate scores for multiple customers.
	 *
	 * @since 1.1.0
	 * @param array $email_hashes Array of customer email hashes.
	 * @return array Results.
	 */
	public static function bulk_recalculate_scores( array $email_hashes ): array {
		$results = array(
			'success' => 0,
			'failed'  => 0,
			'errors'  => array(),
		);

		$score_calculator = TrustLens_Score_Calculator::instance();

		foreach ( $email_hashes as $email_hash ) {
			try {
				$score_calculator->recalculate_score( $email_hash );
				$results['success']++;
			} catch ( Exception $e ) {
				$results['failed']++;
				$results['errors'][] = array(
					'email_hash' => $email_hash,
					'error'      => $e->getMessage(),
				);
			}
		}

		self::log_bulk_action( 'recalculate_scores', $email_hashes, $results );

		return $results;
	}

	/**
	 * Delete multiple customers.
	 *
	 * @since 1.1.0
	 * @param array $email_hashes Array of customer email hashes.
	 * @return array Results.
	 */
	public static function bulk_delete( array $email_hashes ): array {
		$results = array(
			'success' => 0,
			'failed'  => 0,
			'errors'  => array(),
		);

		foreach ( $email_hashes as $email_hash ) {
			$result = wstl_delete_customer( $email_hash );
			if ( $result ) {
				$results['success']++;
			} else {
				$results['failed']++;
				$results['errors'][] = $email_hash;
			}
		}

		self::log_bulk_action( 'delete', $email_hashes, $results );

		return $results;
	}

	/**
	 * Log bulk action.
	 *
	 * @since 1.1.0
	 * @param string $action       Action performed.
	 * @param array  $email_hashes Affected customers.
	 * @param array  $results      Results.
	 * @param array  $extra        Extra data.
	 */
	private static function log_bulk_action( string $action, array $email_hashes, array $results, array $extra = array() ): void {
		/**
		 * Fires after a bulk action is completed.
		 *
		 * @since 1.1.0
		 * @param string $action       Action performed.
		 * @param array  $email_hashes Affected customers.
		 * @param array  $results      Results.
		 * @param array  $extra        Extra data.
		 */
		do_action( 'trustlens/bulk_action_completed', $action, $email_hashes, $results, $extra );

		// Log to error log in debug mode.
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Debug logging intentionally only runs in development mode.
			error_log( sprintf(
				'TrustLens Bulk Action: %s - %d customers, %d success, %d failed',
				$action,
				count( $email_hashes ),
				$results['success'],
				$results['failed']
			) );
		}
	}
}
