<?php
/**
 * Dynamic WooCommerce email for a user-defined "custom email".
 *
 * @package Kramar
 * @since   1.0.0
 */

declare( strict_types=1 );

namespace Kramar\Modules\Email;

defined( 'ABSPATH' ) || exit;

/**
 * One instance is created per enabled custom email (see
 * Email_Manager::register_custom_emails). The body is rendered by the existing
 * block pipeline via replace_email_content(); this class only carries identity,
 * recipient, and the trigger entry point.
 *
 * @since 1.0.0
 */
class Kramar_Custom_Email extends \WC_Email {

	/** @var array<string, mixed> */
	private array $config;

	/**
	 * @param string               $id     The custom email id (kramar_custom_{slug}).
	 * @param array<string, mixed> $config The stored config from kramar_custom_emails.
	 */
	public function __construct( string $id, array $config ) {
		$this->config         = $config;
		$this->id             = $id;
		$this->title          = (string) ( $config['title'] ?? $id );
		$this->description     = (string) ( $config['description'] ?? '' );
		$this->customer_email = ( 'customer' === ( $config['recipient_type'] ?? 'customer' ) );
		$this->template_html  = '';
		$this->template_plain = '';

		parent::__construct();

		$this->recipient = $this->resolve_static_recipient();
	}

	/**
	 * Recipient for admin/custom types (customer is resolved per-send from the order).
	 */
	private function resolve_static_recipient(): string {
		$type = (string) ( $this->config['recipient_type'] ?? 'customer' );
		if ( 'admin' === $type ) {
			// The Admin Recipients setting, not WordPress's admin address:
			// "send this to the shop" has one configured answer, and a custom
			// email addressed to the admin has to use the same one as the
			// withdrawal notification.
			return Email_Manager::admin_recipient_default();
		}
		if ( 'custom' === $type ) {
			return (string) ( $this->config['recipient'] ?? '' );
		}
		return '';
	}

	/** {@inheritDoc} */
	public function get_default_subject(): string {
		return (string) ( $this->config['subject'] ?? $this->title );
	}

	/** {@inheritDoc} */
	public function get_default_heading(): string {
		return $this->title;
	}

	/**
	 * Send this custom email for an order.
	 *
	 * @since 1.0.0
	 *
	 * @param int            $order_id The order id.
	 * @param \WC_Order|null $order    Optional preloaded order.
	 */
	public function trigger( int $order_id, ?\WC_Order $order = null ): void {
		$this->setup_locale();

		$order = $order ?? wc_get_order( $order_id );
		if ( $order instanceof \WC_Order ) {
			$this->object = $order;
			if ( 'customer' === ( $this->config['recipient_type'] ?? 'customer' ) ) {
				$this->recipient = $order->get_billing_email();
			}
		}

		$recipient = (string) apply_filters( 'kramar_email_recipient', $this->get_recipient(), $this->id, $order );

		/**
		 * Allow short-circuiting a send. Return false to skip.
		 *
		 * @since 1.0.0
		 */
		$should = (bool) apply_filters( 'kramar_email_should_send', true, $this->id, $order );

		if ( $this->is_enabled() && '' !== $recipient && $should ) {
			$this->send( $recipient, $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
		}

		$this->restore_locale();
	}
}
