<?php
/**
 * Central email delivery service.
 *
 * @package Donatico\Emails
 */

declare(strict_types=1);

namespace Donatico\Emails;

/**
 * Wraps wp_mail() with HTML support, a configurable From header,
 * and optional debug logging.
 *
 * All outbound Donatico emails pass through this single send() method
 * so that filters, logging, and From overrides apply consistently.
 */
class EmailService {

	/**
	 * Send an HTML email.
	 *
	 * @param string   $to            Recipient email address.
	 * @param string   $subject       Email subject.
	 * @param string   $body          Full HTML body.
	 * @param string[] $extra_headers Additional headers (e.g. Reply-To).
	 * @return bool True on successful hand-off to wp_mail().
	 */
	public function send(
		string $to,
		string $subject,
		string $body,
		array $extra_headers = array()
	): bool {
		if ( ! \is_email( $to ) ) {
			$this->log( "Skipped — invalid recipient: {$to}" );
			return false;
		}

		$headers = \array_merge(
			array(
				'Content-Type: text/html; charset=UTF-8',
				'From: ' . $this->buildFromHeader(),
			),
			$extra_headers
		);

		/**
		 * Allow last-minute modifications to any Donatico outbound email.
		 *
		 * @param string   $subject
		 * @param string   $body
		 * @param string   $to
		 */
		$subject = (string) \apply_filters( 'donatico_email_subject', $subject, $to );
		$body    = (string) \apply_filters( 'donatico_email_body', $body, $to, $subject );

		$this->log( "Sending to: {$to} | Subject: {$subject}" );

		$result = \wp_mail( $to, $subject, $body, $headers );

		if ( ! $result ) {
			$this->log( "wp_mail() returned false for: {$to} | Subject: {$subject}", 'error' );
		}

		return $result;
	}

	// -------------------------------------------------------------------------
	// From header helpers
	// -------------------------------------------------------------------------

	/**
	 * Build a "Name <email>" From header from stored options.
	 *
	 * @return string
	 */
	private function buildFromHeader(): string {
		$name  = $this->getFromName();
		$email = $this->getFromEmail();
		return "{$name} <{$email}>";
	}

	/**
	 * @return string
	 */
	public function getFromName(): string {
		$name = (string) \get_option( 'donatico_email_from_name', '' );
		return $name !== '' ? $name : \get_bloginfo( 'name' );
	}

	/**
	 * @return string
	 */
	public function getFromEmail(): string {
		$email = (string) \get_option( 'donatico_email_from_address', '' );
		if ( $email !== '' && \is_email( $email ) ) {
			return $email;
		}
		return (string) \get_option( 'admin_email', '' );
	}

	// -------------------------------------------------------------------------
	// Logging
	// -------------------------------------------------------------------------

	/**
	 * Log a message to error_log() when email debug is enabled.
	 *
	 * @param string $message
	 * @param string $level   'info' or 'error'.
	 * @return void
	 */
	private function log( string $message, string $level = 'info' ): void {
		if ( ! \apply_filters( 'donatico_email_debug', \defined( 'WP_DEBUG' ) && WP_DEBUG ) ) {
			return;
		}
		// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
		\error_log( '[Donatico Email][' . \strtoupper( $level ) . '] ' . $message );
	}
}
