<?php
/**
 * Cart line item row template.
 *
 * Override: {theme}/titancart/cart/item.php
 *
 * @var array  $item     Line item from CartTotals (hydrated).
 * @var string $currency Currency symbol.
 * @package TitanCart
 */

defined( 'ABSPATH' ) || exit;

$item_id      = (int) $item['item_id'];
$product_id   = (int) $item['product_id'];
$name         = $item['name']            ?? '';
$sku          = $item['sku']             ?? '';
$slug         = $item['slug']            ?? '';
$image        = $item['image']           ?? null;
$quantity     = (int) $item['quantity'];
$unit_price   = (float) $item['unit_price'];
$eff_price    = (float) $item['effective_price'];
$line_total   = (float) $item['line_total'];
$options      = $item['options']         ?? [];
$max_qty      = $item['max_quantity']    ?? '';

// Detect gift card items via meta.
$item_meta = $item['meta'] ?? null;
if ( is_string( $item_meta ) ) {
    $item_meta = json_decode( $item_meta, true );
}
$is_gift_card = is_array( $item_meta ) && ( $item_meta['item_type'] ?? '' ) === 'gift_card';

$product_url  = $is_gift_card
    ? ''
    : apply_filters( 'titancart_product_url', titancart_product_url( $slug ), $product_id, [] );
?>
<tr class="tc-cart__item" data-item-id="<?php echo esc_attr( $item_id ); ?>">

    <td class="tc-cart__col-image">
        <?php if ( $is_gift_card ) : ?>
            <div class="tc-cart__item-image tc-cart__gc-icon" style="display:flex;align-items:center;justify-content:center;width:64px;height:64px;background:linear-gradient(135deg,#2563EB,#3B82F6);border-radius:10px;color:#fff;font-size:28px;" aria-hidden="true">🎁</div>
        <?php elseif ( $image && ! empty( $image['thumb'] ) ) : ?>
            <a href="<?php echo esc_url( $product_url ); ?>">
                <img src="<?php echo esc_url( $image['thumb'] ); ?>"
                     alt="<?php echo esc_attr( $image['alt'] ?: $name ); ?>"
                     class="tc-cart__item-image"
                     width="64" height="64" loading="lazy" />
            </a>
        <?php else : ?>
            <a href="<?php echo esc_url( $product_url ); ?>">
                <div class="tc-cart__item-image tc-cart__item-image--placeholder"></div>
            </a>
        <?php endif; ?>
    </td>

    <td class="tc-cart__col-name">
        <?php if ( $is_gift_card ) : ?>
            <span class="tc-cart__item-name" style="font-weight:600;">
                <?php echo esc_html( $name ); ?>
            </span>
            <?php if ( ! empty( $item_meta['recipient_name'] ) || ! empty( $item_meta['recipient_email'] ) ) : ?>
                <div class="tc-cart__item-sku" style="color:#737373;font-size:12px;margin-top:4px;">
                    <?php
                    printf(
                        /* translators: %s: recipient name or email */
                        esc_html__( 'To: %s', 'titancart-lite' ),
                        esc_html( $item_meta['recipient_name'] ?: $item_meta['recipient_email'] )
                    );
                    if ( ! empty( $item_meta['recipient_name'] ) && ! empty( $item_meta['recipient_email'] ) ) {
                        echo ' &lt;' . esc_html( $item_meta['recipient_email'] ) . '&gt;';
                    }
                    ?>
                </div>
            <?php endif; ?>
            <?php if ( ! empty( $item_meta['message'] ) ) : ?>
                <div style="color:#737373;font-size:11px;font-style:italic;margin-top:2px;max-width:260px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">
                    "<?php echo esc_html( $item_meta['message'] ); ?>"
                </div>
            <?php endif; ?>
        <?php else : ?>
            <a href="<?php echo esc_url( $product_url ); ?>" class="tc-cart__item-name">
                <?php echo esc_html( $name ); ?>
            </a>

            <?php if ( ! empty( $sku ) ) : ?>
                <div class="tc-cart__item-sku">
                    <?php echo esc_html( $sku ); ?>
                </div>
            <?php endif; ?>

            <?php if ( ! empty( $options ) ) : ?>
                <ul class="tc-cart__item-options" aria-label="<?php esc_attr_e( 'Selected options', 'titancart-lite' ); ?>">
                    <?php foreach ( $options as $opt ) : ?>
                        <li class="tc-cart__item-option">
                            <span class="tc-cart__item-option-name">
                                <?php echo esc_html( $opt['option_name'] ?? '' ); ?>:
                            </span>
                            <span class="tc-cart__item-option-value">
                                <?php echo esc_html( $opt['variant_name'] ?? '' ); ?>
                            </span>
                            <?php if ( (float) ( $opt['modifier'] ?? 0 ) !== 0.0 ) :
                                $sign = $opt['modifier'] > 0 ? '+' : '';
                                $mod  = $opt['modifier_type'] === 'P'
                                    ? $sign . $opt['modifier'] . '%'
                                    : $sign . $currency . number_format( (float) $opt['modifier'], 2 );
                            ?>
                                <span class="tc-cart__item-option-modifier">(<?php echo esc_html( $mod ); ?>)</span>
                            <?php endif; ?>
                        </li>
                    <?php endforeach; ?>
                </ul>
            <?php endif; ?>
        <?php endif; ?>
    </td>

    <td class="tc-cart__col-price">
        <?php echo esc_html( $currency . number_format( $eff_price, 2 ) ); ?>
    </td>

    <td class="tc-cart__col-qty">
        <?php if ( $is_gift_card ) : ?>
            <span style="display:inline-block;text-align:center;width:100%;font-size:14px;">1</span>
        <?php else : ?>
            <input type="number"
                   class="tc-cart__qty-input tc-qty-spinner"
                   value="<?php echo esc_attr( $quantity ); ?>"
                   min="1"
                   <?php if ( $max_qty ) : ?>max="<?php echo esc_attr( $max_qty ); ?>"<?php endif; ?>
                   data-item-id="<?php echo esc_attr( $item_id ); ?>"
                   aria-label="<?php esc_attr_e( 'Quantity', 'titancart-lite' ); ?>" />
        <?php endif; ?>
    </td>

    <td class="tc-cart__col-total">
        <span class="tc-cart__line-total" data-item-id="<?php echo esc_attr( $item_id ); ?>">
            <?php echo esc_html( $currency . number_format( $line_total, 2 ) ); ?>
        </span>
    </td>

    <td class="tc-cart__col-remove">
        <button type="button"
                class="tc-cart__remove-btn"
                data-item-id="<?php echo esc_attr( $item_id ); ?>"
                <?php /* translators: %s: product name. */ ?>
                aria-label="<?php echo esc_attr( sprintf( __( 'Remove %s from cart', 'titancart-lite' ), $name ) ); ?>">
            &times;
        </button>
    </td>

</tr>
