<?php
/**
 * @package Hafenstudios\Core
 */

namespace Hafenstudios\Core\Mail;

defined( 'ABSPATH' ) || exit;

/**
 * Wertobjekt fuer eine Transaktionsmail.
 */
final class Message {

	/** @var string */
	public string $to = '';

	/** @var string */
	public string $subject = '';

	/** @var string */
	public string $body = '';

	/**
	 * Optionale Klartext-Alternative fuer Multipart-Mails (Zustellbarkeit). Ist
	 * dieser Wert nicht leer, setzen die Treiber ihn als Text-Teil neben den
	 * HTML-Body. Leer = wie bisher nur ein Body (kein Verhaltenswechsel).
	 *
	 * @var string
	 */
	public string $text_body = '';

	/** @var string */
	public string $from_name = '';

	/** @var string */
	public string $from_email = '';

	/** @var string */
	public string $reply_to = '';

	/** @var bool */
	public bool $is_html = true;

	/**
	 * @var array<string, string> Zusaetzliche Header key => value.
	 */
	public array $headers = array();

	/**
	 * Bequemer Konstruktor.
	 *
	 * @param string $to      Empfaenger.
	 * @param string $subject Betreff.
	 * @param string $body    Inhalt.
	 */
	public function __construct( string $to = '', string $subject = '', string $body = '' ) {
		$this->to      = $to;
		$this->subject = $subject;
		$this->body    = $body;
	}

	/**
	 * Baut die Header-Zeilen fuer wp_mail/PHPMailer.
	 *
	 * @return array<int, string>
	 */
	public function build_headers(): array {
		$headers = array();

		$content_type = $this->is_html ? 'text/html' : 'text/plain';
		$headers[]    = 'Content-Type: ' . $content_type . '; charset=UTF-8';

		if ( '' !== $this->from_email ) {
			$from        = $this->from_name ? sprintf( '%s <%s>', $this->from_name, $this->from_email ) : $this->from_email;
			$headers[]   = 'From: ' . $from;
		}
		if ( '' !== $this->reply_to ) {
			$headers[] = 'Reply-To: ' . $this->reply_to;
		}
		foreach ( $this->headers as $key => $value ) {
			$headers[] = $key . ': ' . $value;
		}

		return $headers;
	}
}
