<?php
/**
 * Backup & Restore job notification emails.
 *
 * @package BackupRestore
 */

namespace BackupRestore\Includes;

use WP_PowerSuite\Email\EmailComponents;
use WP_PowerSuite\Email\EmailRenderer;

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

/**
 * Builds HTML for backup/restore status emails using the shared design system.
 */
final class BackupEmailBuilder {

	/**
	 * @param array<int, array{label: string, value: string, value_html?: string}> $rows
	 */
	public static function render(
		bool $success,
		string $status_label,
		string $message,
		array $rows,
		string $extra_html = '',
		string $logo_url = '',
		string $footer_brand = ''
	): string {
		$content  = EmailComponents::statusBanner( $status_label, $success );
		$content .= EmailComponents::keyValueTable( $rows );
		$content .= EmailComponents::notice( esc_html( $message ), $success ? 'success' : 'error' );
		$content .= $extra_html;

		if ( $footer_brand === '' ) {
			$footer_brand = __( 'Sent via WP PowerSuite Backup & Restore', 'wp-powersuite' );
		}

		return EmailRenderer::wrap(
			[
				'title'        => $status_label,
				'logo_url'     => $logo_url,
				'content'      => $content,
				'footer_brand' => $footer_brand,
			]
		);
	}

	public static function componentsTableHtml( array $components, array $comp_labels, int $total_size ): string {
		if ( empty( $components ) ) {
			return '';
		}

		$rows = [];
		foreach ( $components as $comp => $size ) {
			$label    = $comp_labels[ $comp ] ?? ucfirst( (string) $comp );
			$rows[] = [
				'label' => $label,
				'value' => size_format( (int) $size, 1 ),
			];
		}
		$rows[] = [
			'label' => __( 'Total', 'wp-powersuite' ),
			'value' => size_format( $total_size, 1 ),
		];

		return EmailComponents::detailsBox( __( 'Backup Contents', 'wp-powersuite' ), $rows );
	}

	public static function logExcerptHtml( string $log_excerpt ): string {
		if ( strlen( $log_excerpt ) > 2000 ) {
			$log_excerpt = substr( $log_excerpt, -2000 );
		}
		return EmailComponents::logBlock( __( 'Error Log', 'wp-powersuite' ), $log_excerpt );
	}
}
