<?php
/**
 * Render email-safe HTML (tables + inline styles) from a small set of templates.
 *
 * @package SuiteMkt3k
 */

namespace SuiteMkt3k\Email;

use SuiteMkt3k\Modules\Settings\Settings_Manager;
use SuiteMkt3k\Utils\I18n;

defined( 'ABSPATH' ) || exit;

final class Email_Renderer {

	public static function templates(): array {
		return array(
			'test'      => __( 'SMTP test', 'suitemkt3k' ),
			'classic'   => __( 'Classic promo', 'suitemkt3k' ),
			'welcome'   => __( 'Welcome', 'suitemkt3k' ),
			'reminder'  => __( 'Reminder', 'suitemkt3k' ),
			'cart'      => __( 'Cart recovery', 'suitemkt3k' ),
			'announce'  => __( 'Announcement', 'suitemkt3k' ),
		);
	}

	/**
	 * Templates available in the newsletter designer (no SMTP test, no cart recovery).
	 */
	public static function public_templates(): array {
		$tpl = self::templates();
		unset( $tpl['test'], $tpl['cart'] );
		return $tpl;
	}

	/**
	 * Default copy when switching newsletter templates in the designer.
	 *
	 * @return array<string, array<string, string>>
	 */
	public static function newsletter_presets(): array {
		return Newsletter_Template_Presets::all();
	}

	/**
	 * Company footer lines shown in sent emails (and admin previews).
	 *
	 * @return array{footer_text: string, address: string}
	 */
	public static function footer_context(): array {
		$company = (array) Settings_Manager::get( 'company', '', array() );
		return array(
			'footer_text' => (string) ( $company['name'] ?? get_bloginfo( 'name' ) ),
			'address'     => (string) ( $company['address'] ?? '' ),
		);
	}

	public static function render( string $template_key, array $data ): string {
		I18n::ensure_textdomain();

		$company    = (array) Settings_Manager::get( 'company', '', array() );
		$logo_url = '';
		if ( ! empty( $company['logo_id'] ) ) {
			$logo_url = Email_Assets::attachment_url( (int) $company['logo_id'], 'medium' );
		}
		if ( ! empty( $data['image_url'] ) ) {
			$data['image_url'] = Email_Assets::to_absolute_url( (string) $data['image_url'] );
		} elseif ( ! empty( $data['image_id'] ) ) {
			$data['image_url'] = Email_Assets::attachment_url( (int) $data['image_id'], 'large' );
		}

		$template_key = sanitize_key( $template_key );
		if ( ! in_array( $template_key, array( 'classic', 'welcome', 'reminder', 'announce', 'cart', 'test' ), true ) ) {
			$template_key = 'classic';
		}

		$brand         = (string) ( $data['brand'] ?? ( $company['name'] ?: get_bloginfo( 'name' ) ) );
		$heading       = (string) ( $data['heading'] ?? '' );
		$intro_line    = (string) ( $data['intro_line'] ?? '' );
		$footer_line   = (string) ( $data['footer_line'] ?? '' );
		$preheader_raw = (string) ( $data['preheader'] ?? '' );
		if ( '' === $intro_line && '' !== $preheader_raw && 'cart' === $template_key ) {
			$intro_line = $preheader_raw;
		}
		if ( '' === $footer_line && '' !== $preheader_raw && in_array( $template_key, array( 'classic', 'welcome', 'reminder', 'announce' ), true ) && '' === $intro_line ) {
			$footer_line = $preheader_raw;
		}
		$inbox_preview = (string) ( $data['inbox_preview'] ?? '' );
		if ( '' === $inbox_preview ) {
			$inbox_preview = wp_strip_all_tags( $heading );
			if ( strlen( $inbox_preview ) > 120 ) {
				$inbox_preview = substr( $inbox_preview, 0, 117 ) . '…';
			}
		}
		$body         = (string) ( $data['body'] ?? '' );
		$cta_label    = (string) ( $data['cta_label'] ?? '' );
		$cta_url      = (string) ( $data['cta_url'] ?? '' );
		$image_url    = (string) ( $data['image_url'] ?? '' );
		$footer_defaults = self::footer_context();
		$footer_text     = (string) ( $data['footer_text'] ?? $footer_defaults['footer_text'] );
		$address         = (string) ( $data['address'] ?? $footer_defaults['address'] );
		$unsubscribe  = (string) ( $data['unsubscribe_url'] ?? '' );
		$unsubscribe_text = (string) ( $data['unsubscribe_text'] ?? '' );
		$unsubscribe_cta  = (string) ( $data['unsubscribe_cta'] ?? __( 'Unsubscribe', 'suitemkt3k' ) );
		$accent           = (string) ( $data['accent'] ?? '#2AB2E5' );
		$header_color     = (string) ( $data['header_color'] ?? $accent );
		$secondary_accent = (string) ( $data['secondary_accent'] ?? $accent );
		$badge_label      = (string) ( $data['badge_label'] ?? '' );
		$cta_radius       = max( 0, min( 99, (int) ( $data['cta_radius'] ?? 10 ) ) );
		$footer_color     = (string) ( $data['footer_color'] ?? '#94A3B8' );
		$discount         = (string) ( $data['discount_html'] ?? '' );

		ob_start();
		include SUITEMKT3K_PATH . 'includes/email/templates/base.php';
		return (string) ob_get_clean();
	}
}
