<?php
namespace Pay4All\Pro\Services;

use Pay4All\Pro\Domain\Newsletter;

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

/**
 * Default HTML bodies + subjects for each newsletter kind (free / product
 * / promo) in each supported language (fr / de / it / en). The merchant
 * gets one of these pre-filled the first time they open a language tab
 * on a new campaign, then can freely edit — the defaults are only a
 * starting point, never re-applied on subsequent edits.
 *
 * Every default carries the same token vocabulary described in
 * NewsletterSubstitutor : the merchant can add / remove tokens without
 * ceremony, we don't validate the shape.
 */
final class NewsletterTemplates {

	/**
	 * Returns one { subject, body, cta_label, cta_url } template for the
	 * given kind + language, ready to seed the editor. Language falls back
	 * to English if we don't ship a default for the requested locale
	 * (should not happen since we cover fr / de / it / en, but keeps the
	 * caller null-safe if a future language is added).
	 *
	 * @return array{subject:string,body:string,cta_label:string,cta_url:string}
	 */
	public static function for_kind( string $kind, string $lang ) : array {
		$lang = strtolower( substr( $lang, 0, 2 ) );
		if ( ! in_array( $kind, Newsletter::KINDS, true ) ) { $kind = Newsletter::KIND_FREE; }
		$all = self::all();
		return $all[ $kind ][ $lang ] ?? $all[ $kind ]['en'];
	}

	/**
	 * Full default catalogue. Kept as one giant static array so translators
	 * see the whole surface in a single file — no dance with per-lang .po
	 * strings that would drag runtime gettext into the picture.
	 *
	 * @return array<string,array<string,array{subject:string,body:string,cta_label:string,cta_url:string}>>
	 */
	private static function all() : array {
		$home = home_url( '/' );

		$footer_fr = '<p style="font-size:12px;color:#777;margin-top:24px;">Vous recevez cet e-mail parce que vous avez déjà commandé chez nous. <a href="{unsubscribe_url}">Se désinscrire</a>.</p>';
		$footer_de = '<p style="font-size:12px;color:#777;margin-top:24px;">Sie erhalten diese E-Mail, weil Sie bereits bei uns bestellt haben. <a href="{unsubscribe_url}">Abmelden</a>.</p>';
		$footer_it = '<p style="font-size:12px;color:#777;margin-top:24px;">Ricevi questa e-mail perché hai già ordinato da noi. <a href="{unsubscribe_url}">Annulla iscrizione</a>.</p>';
		$footer_en = '<p style="font-size:12px;color:#777;margin-top:24px;">You are receiving this email because you have ordered from us. <a href="{unsubscribe_url}">Unsubscribe</a>.</p>';

		return [
			Newsletter::KIND_FREE => [
				'fr' => [
					'subject'   => 'Une actualité de {site_name}',
					'body'      => "<p>Bonjour {first_name},</p>\n<p>Nous avons une petite nouveauté à vous partager. Prenez quelques secondes pour la découvrir.</p>\n" . $footer_fr,
					'cta_label' => 'Découvrir',
					'cta_url'   => $home,
				],
				'de' => [
					'subject'   => 'Neuigkeiten von {site_name}',
					'body'      => "<p>Guten Tag {first_name},</p>\n<p>Wir haben eine kleine Neuigkeit für Sie. Nehmen Sie sich einen Moment Zeit, um sie zu entdecken.</p>\n" . $footer_de,
					'cta_label' => 'Entdecken',
					'cta_url'   => $home,
				],
				'it' => [
					'subject'   => 'Novità da {site_name}',
					'body'      => "<p>Buongiorno {first_name},</p>\n<p>Abbiamo una piccola novità da condividere con te. Prendi qualche secondo per scoprirla.</p>\n" . $footer_it,
					'cta_label' => 'Scopri',
					'cta_url'   => $home,
				],
				'en' => [
					'subject'   => 'News from {site_name}',
					'body'      => "<p>Hi {first_name},</p>\n<p>We have a small update to share with you. Take a few seconds to check it out.</p>\n" . $footer_en,
					'cta_label' => 'Discover',
					'cta_url'   => $home,
				],
			],
			Newsletter::KIND_PRODUCT => [
				'fr' => [
					'subject'   => '{product_title} - découvrez notre nouveau produit',
					'body'      => "<p>Bonjour {first_name},</p>\n<p>Nous avons le plaisir de vous présenter <strong>{product_title}</strong>, disponible dès maintenant à {product_price}.</p>\n<p>Nous serions ravis de vous compter parmi les premiers à le découvrir.</p>\n" . $footer_fr,
					'cta_label' => 'Voir le produit',
					'cta_url'   => $home,
				],
				'de' => [
					'subject'   => '{product_title} - unser neues Produkt',
					'body'      => "<p>Guten Tag {first_name},</p>\n<p>Wir freuen uns, Ihnen <strong>{product_title}</strong> vorzustellen, ab sofort erhältlich für {product_price}.</p>\n<p>Wir würden uns freuen, wenn Sie zu den Ersten gehören, die es entdecken.</p>\n" . $footer_de,
					'cta_label' => 'Produkt ansehen',
					'cta_url'   => $home,
				],
				'it' => [
					'subject'   => '{product_title} - scopri il nostro nuovo prodotto',
					'body'      => "<p>Buongiorno {first_name},</p>\n<p>Siamo lieti di presentarti <strong>{product_title}</strong>, disponibile da subito a {product_price}.</p>\n<p>Saremmo felici di averti tra i primi a scoprirlo.</p>\n" . $footer_it,
					'cta_label' => 'Vedi il prodotto',
					'cta_url'   => $home,
				],
				'en' => [
					'subject'   => '{product_title} - discover our new product',
					'body'      => "<p>Hi {first_name},</p>\n<p>We are pleased to introduce <strong>{product_title}</strong>, available now for {product_price}.</p>\n<p>We'd love for you to be among the first to check it out.</p>\n" . $footer_en,
					'cta_label' => 'View the product',
					'cta_url'   => $home,
				],
			],
			Newsletter::KIND_PROMO => [
				'fr' => [
					'subject'   => 'Votre code promo : {promo_code}',
					'body'      => "<p>Bonjour {first_name},</p>\n<p>Profitez de <strong>{promo_discount}</strong> sur votre prochaine commande grâce au code <code>{promo_code}</code>.</p>\n<p>Offre valable jusqu'au {promo_expires_at}.</p>\n" . $footer_fr,
					'cta_label' => 'Utiliser mon code',
					'cta_url'   => $home,
				],
				'de' => [
					'subject'   => 'Ihr Aktionscode: {promo_code}',
					'body'      => "<p>Guten Tag {first_name},</p>\n<p>Profitieren Sie von <strong>{promo_discount}</strong> auf Ihre nächste Bestellung mit dem Code <code>{promo_code}</code>.</p>\n<p>Angebot gültig bis {promo_expires_at}.</p>\n" . $footer_de,
					'cta_label' => 'Meinen Code einlösen',
					'cta_url'   => $home,
				],
				'it' => [
					'subject'   => 'Il tuo codice promo: {promo_code}',
					'body'      => "<p>Buongiorno {first_name},</p>\n<p>Approfitta di <strong>{promo_discount}</strong> sul tuo prossimo ordine con il codice <code>{promo_code}</code>.</p>\n<p>Offerta valida fino al {promo_expires_at}.</p>\n" . $footer_it,
					'cta_label' => 'Usa il mio codice',
					'cta_url'   => $home,
				],
				'en' => [
					'subject'   => 'Your promo code: {promo_code}',
					'body'      => "<p>Hi {first_name},</p>\n<p>Enjoy <strong>{promo_discount}</strong> on your next order with the code <code>{promo_code}</code>.</p>\n<p>Offer valid until {promo_expires_at}.</p>\n" . $footer_en,
					'cta_label' => 'Use my code',
					'cta_url'   => $home,
				],
			],
		];
	}

	/**
	 * Public catalogue of tokens per kind — used by the metabox to render
	 * the clickable variable buttons above each editor. Keeping it here
	 * (next to the templates that consume them) makes it obvious to a
	 * translator which tokens they're allowed to weave into the text.
	 *
	 * @return array<string,array<int,string>>
	 */
	public static function available_tokens( string $kind ) : array {
		$common = [ 'first_name', 'last_name', 'email', 'site_name' ];
		$extras = [
			Newsletter::KIND_FREE    => [],
			Newsletter::KIND_PRODUCT => [ 'product_title', 'product_price', 'product_url' ],
			Newsletter::KIND_PROMO   => [ 'promo_code', 'promo_discount', 'promo_expires_at' ],
		];
		return [
			'common' => $common,
			'extra'  => $extras[ $kind ] ?? [],
			'system' => [ 'unsubscribe_url' ],
		];
	}
}
