<?php
/**
 * Email template: Downloads Ready (Standalone) — Customer.
 *
 * Sent in two scenarios:
 *   1. Async gateway capture — PayPal / Stripe webhook arrives later than the
 *      original confirmation email. By the time keys issue, the confirmation
 *      has already gone out without download links.
 *   2. Admin "Resend downloads email" — explicit re-issuance with fresh tokens.
 *
 * This email exists ONLY to deliver download links. It does not duplicate the
 * order summary, addresses, or status — those live in the original
 * confirmation. A short order-reference banner ties the two emails together.
 *
 * Available variables (via Mailer::render()):
 *   $order            — Full order row
 *   $download_groups  — Hydrated download groups (see parts/downloads-block.php)
 *   $is_resend        — true when triggered by admin resend, false for async first delivery
 *   $site_name        — Blog name
 *   $site_url         — Home URL
 *   $mailer           — Mailer instance
 *
 * @package TitanCart
 */
defined( 'ABSPATH' ) || exit;

$customer_name  = trim( ( $order['billing_first_name'] ?? '' ) . ' ' . ( $order['billing_last_name'] ?? '' ) );
$my_orders_url  = apply_filters( 'titancart_my_orders_url', home_url( '/my-orders/' ) );
$is_resend      = ! empty( $is_resend );
?>

<h1 style="margin-bottom:6px;">
    <?php
    if ( $is_resend ) {
        printf(
            /* translators: %s: customer first name */
            esc_html__( 'Hi %s — here are your fresh download links', 'titancart-lite' ),
            esc_html( $order['billing_first_name'] ?? '' )
        );
    } else {
        printf(
            /* translators: %s: customer first name */
            esc_html__( 'Hi %s — your downloads are ready', 'titancart-lite' ),
            esc_html( $order['billing_first_name'] ?? '' )
        );
    }
    ?>
</h1>

<p style="color:#5A6270;font-size:13px;margin-bottom:24px;">
    <?php
    if ( $is_resend ) {
        esc_html_e(
            'We\'ve generated a new set of download links for your order. The previous links from earlier emails will no longer work — please use the ones below.',
            'titancart-lite'
        );
    } else {
        esc_html_e(
            'Your payment has cleared and your download links are now active.',
            'titancart-lite'
        );
    }
    ?>
</p>

<!-- Order reference banner -->
<div style="background:#F6F8FB;border-radius:8px;padding:14px 18px;margin-bottom:8px;font-size:13px;">
    <strong><?php esc_html_e( 'Order:', 'titancart-lite' ); ?></strong>
    <?php echo esc_html( $order['order_number'] ?? '' ); ?>
    <?php if ( ! empty( $order['created_at'] ) ) : ?>
        &nbsp; · &nbsp;
        <strong><?php esc_html_e( 'Placed:', 'titancart-lite' ); ?></strong>
        <?php echo esc_html( titancart_format_date( $order['created_at'] ) ); ?>
    <?php endif; ?>
</div>

<?php
// Pull in the shared partial. $download_groups is defined in scope.
\TitanCart\Storefront\TemplateLoader::get_template(
    'emails/parts/downloads-block.php',
    [ 'download_groups' => $download_groups ],
    false
);
?>

<p style="margin-top:24px;">
    <a href="<?php echo esc_url( $my_orders_url ); ?>" class="btn">
        <?php esc_html_e( 'View Your Orders', 'titancart-lite' ); ?>
    </a>
</p>

<hr class="divider">

<p style="color:#5A6270;font-size:12px;text-align:center;">
    <?php
    printf(
        /* translators: %s: site name */
        esc_html__( 'Questions? Reply to this email or contact %s.', 'titancart-lite' ),
        esc_html( $site_name )
    );
    ?>
</p>
