<?php
/**
 * Email partial: Downloads block.
 *
 * Renders a list of just-issued download links, grouped by line-item product.
 * Used by both the inline embed in order-confirmation.php and by the
 * standalone downloads-ready.php email.
 *
 * Available variables (passed in via Mailer::render_partial):
 *   $download_groups — Array of:
 *     [
 *       'product_name'      => string  Order-item snapshot product name.
 *       'order_item_id'     => int
 *       'files'             => array of:
 *         [
 *           'file_name'     => string  Original uploaded filename.
 *           'file_size'     => int     Bytes (0 if unknown).
 *           'mime'          => string
 *           'url'           => string  Public token URL.
 *           'expires_at'    => string|null  YYYY-MM-DD HH:MM:SS or null = never.
 *           'max_downloads' => int|null     null = unlimited.
 *           'file_status'   => string  'active'|'disabled' on the file row.
 *         ]
 *     ]
 *
 * @package TitanCart
 */
defined( 'ABSPATH' ) || exit;

if ( empty( $download_groups ) || ! is_array( $download_groups ) ) {
    return;
}

/**
 * Format a byte count as KB/MB/GB. Mirror of the JS helper in OrderDownloadsPanel.
 *
 * @param  int $bytes
 * @return string
 */
$tc_fmt_bytes = static function ( int $bytes ): string {
    if ( $bytes < 1024 ) {
        return $bytes . ' B';
    }
    if ( $bytes < 1024 * 1024 ) {
        return number_format( $bytes / 1024, 1 ) . ' KB';
    }
    if ( $bytes < 1024 * 1024 * 1024 ) {
        return number_format( $bytes / 1024 / 1024, 1 ) . ' MB';
    }
    return number_format( $bytes / 1024 / 1024 / 1024, 2 ) . ' GB';
};
?>

<div style="background:#F0F7FF;border:1px solid #C7DEF9;border-radius:10px;padding:20px 22px;margin:24px 0;">
    <h2 style="font-size:16px;color:#0E2B53;margin:0 0 6px;">
        <?php esc_html_e( 'Your downloads are ready', 'titancart-lite' ); ?>
    </h2>
    <p style="font-size:13px;color:#3F5878;margin:0 0 18px;">
        <?php esc_html_e( 'Click any link below to download. Save the files locally — links expire after the limits shown.', 'titancart-lite' ); ?>
    </p>

    <?php foreach ( $download_groups as $group ) : ?>
        <div style="margin-bottom:14px;">
            <div style="font-weight:600;color:#0E2B53;font-size:13px;margin-bottom:6px;">
                <?php echo esc_html( $group['product_name'] ); ?>
            </div>

            <?php foreach ( $group['files'] as $file ) :
                $size_label = ( ! empty( $file['file_size'] ) )
                    ? $tc_fmt_bytes( (int) $file['file_size'] )
                    : '';

                // Compose the limits sub-line: "Expires Mar 15, 2026 · 5 downloads remaining"
                $limit_parts = [];
                if ( ! empty( $file['expires_at'] ) ) {
                    $limit_parts[] = sprintf(
                        /* translators: %s: expiration date */
                        esc_html__( 'Expires %s', 'titancart-lite' ),
                        esc_html( titancart_format_date( $file['expires_at'] ) )
                    );
                }
                if ( $file['max_downloads'] !== null && $file['max_downloads'] !== '' ) {
                    $limit_parts[] = sprintf(
                        /* translators: %d: maximum download count */
                        esc_html( _n( '%d download', '%d downloads', (int) $file['max_downloads'], 'titancart-lite' ) ),
                        (int) $file['max_downloads']
                    );
                }
                $limits_line = implode( ' · ', $limit_parts );

                // Disabled file rows still appear (transparency: customer can see
                // what they bought) but the link is rendered as plain text plus
                // a notice rather than a clickable URL.
                $is_disabled = ( ( $file['file_status'] ?? 'active' ) === 'disabled' );
            ?>
                <div style="background:#FFFFFF;border:1px solid #E1E5EA;border-radius:8px;padding:12px 14px;margin-bottom:8px;">
                    <table role="presentation" cellpadding="0" cellspacing="0" border="0" width="100%">
                        <tr>
                            <td style="vertical-align:top;">
                                <?php if ( $is_disabled ) : ?>
                                    <div style="font-size:13px;color:#5A6270;font-weight:500;">
                                        <?php echo esc_html( $file['file_name'] ); ?>
                                    </div>
                                    <div style="font-size:11px;color:#C84646;margin-top:4px;">
                                        <?php esc_html_e( 'This file is currently unavailable. Please contact support.', 'titancart-lite' ); ?>
                                    </div>
                                <?php else : ?>
                                    <a href="<?php echo esc_url( $file['url'] ); ?>"
                                       style="font-size:13px;color:#1F6AE1;font-weight:600;text-decoration:none;display:inline-block;word-break:break-word;">
                                        <?php echo esc_html( $file['file_name'] ); ?>
                                    </a>
                                <?php endif; ?>

                                <?php if ( $size_label !== '' || $limits_line !== '' ) : ?>
                                    <div style="font-size:11px;color:#5A6270;margin-top:4px;">
                                        <?php if ( $size_label !== '' ) : ?>
                                            <span><?php echo esc_html( $size_label ); ?></span>
                                        <?php endif; ?>
                                        <?php if ( $size_label !== '' && $limits_line !== '' ) : ?>
                                            <span> · </span>
                                        <?php endif; ?>
                                        <?php if ( $limits_line !== '' ) : ?>
                                            <span><?php echo wp_kses_post( $limits_line ); ?></span>
                                        <?php endif; ?>
                                    </div>
                                <?php endif; ?>
                            </td>
                            <?php if ( ! $is_disabled ) : ?>
                                <td style="vertical-align:top;text-align:right;padding-left:12px;white-space:nowrap;">
                                    <a href="<?php echo esc_url( $file['url'] ); ?>"
                                       style="display:inline-block;background:#1F6AE1;color:#FFFFFF;text-decoration:none;padding:7px 14px;border-radius:6px;font-size:12px;font-weight:600;">
                                        <?php esc_html_e( 'Download', 'titancart-lite' ); ?>
                                    </a>
                                </td>
                            <?php endif; ?>
                        </tr>
                    </table>
                </div>
            <?php endforeach; ?>
        </div>
    <?php endforeach; ?>

    <p style="font-size:11px;color:#5A6270;margin:14px 0 0;">
        <?php esc_html_e( 'Having trouble downloading? Sign in to your account to access your downloads at any time.', 'titancart-lite' ); ?>
    </p>
</div>
