<?php
/**
 * WooCommerce email for the StoreSheet daily sync digest.
 *
 * Registered through `woocommerce_email_classes` so it appears under
 * WooCommerce → Settings → Emails with the usual enable/subject/heading/type
 * controls. It sends itself off the `storesheet_sync_digest` hook that
 * DigestNotifier fires once a day.
 *
 * Free build: recipient is the store admin (no recipient form field).
 *
 * @package StoreSheet
 */

declare( strict_types=1 );

namespace StoreSheet\Emails;

defined( 'ABSPATH' ) || exit;

if ( ! class_exists( '\WC_Email' ) ) {
	return;
}

class SyncDigestEmail extends \WC_Email {

	/** @var array<string,mixed> */
	private array $summary = array();

	public function __construct() {
		$this->id             = 'storesheet_sync_digest';
		$this->title          = __( 'StoreSheet: daily sync digest', 'storesheet' );
		$this->description    = __( 'A once-a-day summary of what StoreSheet synced to Google Sheets, and any failures.', 'storesheet' );
		$this->customer_email = false;
		$this->template_html  = 'emails/storesheet-sync-digest.php';
		$this->template_plain = 'emails/plain/storesheet-sync-digest.php';
		$this->template_base  = STORESHEET_DIR . 'templates/';

		/**
		 * Fired once a day by DigestNotifier with the canonical sync summary.
		 *
		 * @since 1.2.0
		 * @param array<string,mixed> $summary Canonical digest summary.
		 */
		add_action( 'storesheet_sync_digest', array( $this, 'trigger_digest' ), 10, 1 );

		parent::__construct();

		$recipient       = trim( (string) $this->get_option( 'recipient', '' ) );
		$this->recipient = '' !== $recipient ? $recipient : (string) get_option( 'admin_email' );
	}

	public function get_default_subject(): string {
		return __( '[{site_title}] StoreSheet daily sync summary', 'storesheet' );
	}

	public function get_default_heading(): string {
		return __( 'Your StoreSheet daily sync summary', 'storesheet' );
	}

	/**
	 * Handle the storesheet_sync_digest hook and send the email.
	 *
	 * @since 1.2.0
	 * @param array<string,mixed> $summary Canonical digest summary.
	 */
	public function trigger_digest( array $summary ): void {
		$this->setup_locale();

		$this->summary = $summary;

		$this->placeholders['{sync_total}']  = (string) ( $summary['total'] ?? 0 );
		$this->placeholders['{sync_failed}'] = (string) ( $summary['total_failed'] ?? 0 );

		if ( $this->is_enabled() && $this->get_recipient() ) {
			$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
		}

		$this->restore_locale();
	}

	public function get_content_html(): string {
		return wc_get_template_html( $this->template_html, $this->template_args( false ), '', $this->template_base );
	}

	public function get_content_plain(): string {
		return wc_get_template_html( $this->template_plain, $this->template_args( true ), '', $this->template_base );
	}

	/**
	 * @param bool $plain_text
	 * @return array<string, mixed>
	 */
	private function template_args( bool $plain_text ): array {
		return array(
			'summary'       => $this->summary,
			'email_heading' => $this->get_heading(),
			'sent_to_admin' => true,
			'plain_text'    => $plain_text,
			'email'         => $this,
		);
	}

	public function init_form_fields(): void {
		$this->form_fields = array(
			'enabled'    => array(
				'title'   => __( 'Enable/Disable', 'storesheet' ),
				'type'    => 'checkbox',
				'label'   => __( 'Enable this email notification', 'storesheet' ),
				'default' => 'yes',
			),
			'subject'    => array(
				'title'       => __( 'Subject', 'storesheet' ),
				'type'        => 'text',
				'desc_tip'    => true,
				/* translators: %s: list of available placeholders */
				'description' => sprintf( __( 'Available placeholders: %s', 'storesheet' ), '<code>{site_title}, {sync_total}, {sync_failed}</code>' ),
				'placeholder' => $this->get_default_subject(),
				'default'     => '',
			),
			'heading'    => array(
				'title'       => __( 'Email heading', 'storesheet' ),
				'type'        => 'text',
				'desc_tip'    => true,
				'placeholder' => $this->get_default_heading(),
				'default'     => '',
			),
			'email_type' => array(
				'title'       => __( 'Email type', 'storesheet' ),
				'type'        => 'select',
				'description' => __( 'Choose which format of email to send.', 'storesheet' ),
				'default'     => 'html',
				'class'       => 'email_type wc-enhanced-select',
				'options'     => $this->get_email_type_options(),
				'desc_tip'    => true,
			),
		);
	}
}
