<?php
/**
 * Prebuilt email copy for the play editor (subject + body tokens).
 *
 * @package CartCue
 */

namespace CartCue;

defined( 'ABSPATH' ) || exit;

final class EmailTemplates {

	/**
	 * @return array<string, array{label: string, subject: string, body: string}>
	 */
	public static function presets(): array {
		return [
			'' => [
				'label'   => __( 'Custom', 'cartcue-sales-automation' ),
				'subject' => '',
				'body'    => '',
			],
			'cart_recovery' => [
				'label'   => __( 'Recovery Email (Recommended)', 'cartcue-sales-automation' ),
				'subject' => __( 'You left something behind, {{customer.first_name}}', 'cartcue-sales-automation' ),
				'body'    => wp_json_encode(
					[
						[
							'type'    => 'text',
							'content' => '<p>' . __( 'Hi {{customer.first_name}},', 'cartcue-sales-automation' ) . '</p><p>' . __( 'Looks like you still have items waiting in your cart. Complete your order whenever you are ready:', 'cartcue-sales-automation' ) . '</p>',
							'align'   => 'left',
						],
						[
							'type'       => 'button',
							'text'       => __( 'Return to checkout', 'cartcue-sales-automation' ) . ' →',
							'url'        => '{{cart.recover_url}}',
							'bg_color'   => '#534AB7',
							'text_color' => '#ffffff',
							'align'      => 'center',
						],
						[
							'type'   => 'text',
							'content' => '<p>' . __( 'Thanks,<br>{{shop.name}}', 'cartcue-sales-automation' ) . '</p>',
							'align' => 'left',
						],
					]
				),
			],
			'welcome' => [
				'label'   => __( 'Welcome + Coupon', 'cartcue-sales-automation' ),
				'subject' => __( 'Welcome to {{shop.name}}, {{customer.first_name}}!', 'cartcue-sales-automation' ),
				'body'    => wp_json_encode(
					[
						[
							'type'    => 'text',
							'content' => '<p>' . __( 'Hi {{customer.first_name}},', 'cartcue-sales-automation' ) . '</p><p>' . __( 'Thank you for your first order with us. We are glad you are here.', 'cartcue-sales-automation' ) . '</p><p>' . __( 'Order #{{order.number}} — {{order.total}}', 'cartcue-sales-automation' ) . '</p>{{order.items}}',
							'align'   => 'left',
						],
						[
							'type'    => 'text',
							'content' => '<p>' . __( 'Cheers,<br>{{shop.name}}', 'cartcue-sales-automation' ) . '</p>',
							'align'   => 'left',
						],
					]
				),
			],
			'win_back' => [
				'label'   => __( 'Win-Back Offer', 'cartcue-sales-automation' ),
				'subject' => __( 'We miss you, {{customer.first_name}}', 'cartcue-sales-automation' ),
				'body'    => wp_json_encode(
					[
						[
							'type'    => 'text',
							'content' => '<p>' . __( 'Hi {{customer.first_name}},', 'cartcue-sales-automation' ) . '</p><p>' . __( 'It has been a while since your last visit. Come back and see what is new at {{shop.name}}.', 'cartcue-sales-automation' ) . '</p>',
							'align'   => 'left',
						],
						[
							'type'       => 'button',
							'text'       => __( 'Shop now', 'cartcue-sales-automation' ) . ' →',
							'url'        => '{{shop.url}}',
							'bg_color'   => '#534AB7',
							'text_color' => '#ffffff',
							'align'      => 'center',
						],
						[
							'type'    => 'text',
							'content' => '<p>' . __( 'Hope to see you soon,<br>{{shop.name}}', 'cartcue-sales-automation' ) . '</p>',
							'align'   => 'left',
						],
					]
				),
			],
		];
	}

	/**
	 * Presets for the admin JS (labels only in select; full copy in emailPresets).
	 *
	 * @return array<string, array{label: string, subject: string, body: string}>
	 */
	public static function for_js(): array {
		return self::presets();
	}

	/**
	 * @return array<int, array<string, mixed>>
	 */
	public static function preset_blocks( string $preset ): array {
		$presets = self::presets();
		if ( empty( $preset ) || empty( $presets[ $preset ]['body'] ) ) {
			return [];
		}
		$decoded = json_decode( (string) $presets[ $preset ]['body'], true );
		return is_array( $decoded ) ? $decoded : [];
	}
}
