<?php
/**
 * Admin class for cart recovery emails.
 *
 * @package EDD\Pro\CartRecovery\Emails
 * @copyright Copyright (c) 2026, Sandhills Development, LLC
 * @license https://opensource.org/licenses/GPL-2.0 GNU Public License
 * @since 3.6.5
 */

namespace EDD\Pro\CartRecovery\Emails;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

use EDD\EventManagement\SubscriberInterface;

/**
 * Admin class for cart recovery emails.
 *
 * @since 3.6.5
 * @package EDD\Pro\CartRecovery\Emails
 */
class Admin implements SubscriberInterface {

	/**
	 * Gets the subscribed events.
	 *
	 * @since 3.6.5
	 * @return array
	 */
	public static function get_subscribed_events(): array {
		return array(
			'edd_email_manager_save_email_id' => array( 'save_id', 10, 3 ),
			'edd_delete_acr_custom_email'     => 'delete_custom_email',
			'edd_emails_logs_table_object'    => array( 'filter_logs_table_object', 10, 2 ),
		);
	}

	/**
	 * Saves the email ID. This only runs when creating a new custom email.
	 * This generates a unique ID for the new email.
	 *
	 * @since 3.6.5
	 * @param string                              $email_id       The email ID.
	 * @param \EDD\Emails\Templates\EmailTemplate $email_template The email template.
	 * @param array                               $data           The form data.
	 * @return string|null
	 */
	public function save_id( $email_id, $email_template, $data ) {
		if ( 'edd' !== $email_template->sender ) {
			return $email_id;
		}

		if ( 'abandoned_cart' !== $email_template->context ) {
			return $email_id;
		}

		if ( empty( $data['type'] ) || 'custom' !== $data['type'] ) {
			return $email_id;
		}

		if ( 'acr-custom-new' === $email_id ) {
			return \EDD\Admin\Emails\Manager::get_new_id( 'edd_pro_acr', $email_id );
		}

		return $email_id;
	}

	/**
	 * Deletes a custom email.
	 *
	 * @since 3.6.5
	 * @param array $data The form data.
	 */
	public function delete_custom_email( $data ) {
		if ( empty( $data['email-id'] ) ) {
			return;
		}

		if ( ! current_user_can( 'manage_options' ) ) {
			return;
		}

		if ( ! wp_verify_nonce( $data['_wpnonce'], 'delete_acr_custom_email' ) ) {
			return;
		}

		$email = edd_get_email( $data['email-id'] );
		if ( ! $email ) {
			return;
		}

		edd_delete_email( $email->id );

		edd_redirect(
			edd_get_admin_url(
				array(
					'page' => 'edd-cart-recovery',
					'tab'  => 'settings',
				)
			)
		);
	}

	/**
	 * Filters the object column in the emails logs table.
	 *
	 * @since 3.6.5
	 * @param string                    $email_object_id The email object ID.
	 * @param \EDD\Emails\Logs\EmailLog $email_log The email log object.
	 * @return string The filtered object column.
	 */
	public function filter_logs_table_object( $email_object_id, $email_log ) {
		if ( 'abandoned_cart' !== $email_log->object_type ) {
			return $email_object_id;
		}

		$query = new \EDD\Pro\CartRecovery\Database\Queries\RecoveryCart();
		$cart  = $query->get_item( $email_log->object_id );
		/* translators: %s: Cart ID */
		$label = sprintf( __( 'Abandoned Cart %s', 'easy-digital-downloads' ), absint( $email_log->object_id ) );
		if ( ! $cart ) {
			return $label;
		}

		return sprintf(
			'<a href="%1$s">%2$s</a>',
			esc_url(
				edd_get_admin_url(
					array(
						'page'    => 'edd-cart-recovery',
						'tab'     => 'carts',
						'cart_id' => $cart->id,
					)
				)
			),
			esc_html( $label )
		);
	}
}
