<?php
/**
 * Email delivery for HTML reports.
 *
 * @package WPLens
 */

namespace WPLens\Reports;

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

final class EmailSender {

	/**
	 * @param array<int, string> $recipients
	 */
	public function send_weekly_report( array $recipients, string $html ): bool {
		$recipients = array_values( array_filter( array_map( 'sanitize_email', $recipients ) ) );
		if ( empty( $recipients ) ) {
			return false;
		}

		$subject = sprintf( /* translators: %s site name */ __( 'AI Visibility Report — %s', 'lens-ai-visibility-monitor' ), get_bloginfo( 'name' ) );
		$headers = array( 'Content-Type: text/html; charset=UTF-8' );

		add_filter( 'wp_mail_content_type', array( $this, 'force_html_content_type' ) );
		$sent = wp_mail( $recipients, $subject, $html, $headers );
		remove_filter( 'wp_mail_content_type', array( $this, 'force_html_content_type' ) );

		return (bool) $sent;
	}

	public function force_html_content_type(): string {
		return 'text/html';
	}
}
