<?php
if (!defined('ABSPATH')) exit;
/**
 * Renders the Odoo message thread for a given order.
 * Include from the order details page — $order must be in scope.
 *
 * @var WC_Order $order
 */
$messages = $order->get_meta('_odoo_messages');
if (empty($messages) || !is_array($messages)) {
    return;
}
?>
<div class="fusion-woodoo-communication">
    <h3><?php esc_html_e('Messages', 'fusion-woodoo'); ?></h3>
    <div class="fusion-woodoo-messages">
        <?php foreach ($messages as $msg):
            $type = sanitize_key($msg['type'] ?? 'note');
        ?>
            <div class="fusion-woodoo-message fusion-woodoo-message--<?php echo esc_attr($type); ?>">
                <div class="fusion-woodoo-message__meta">
                    <span class="fusion-woodoo-message__author"><?php echo esc_html($msg['author'] ?? __('Odoo', 'fusion-woodoo')); ?></span>
                    <span class="fusion-woodoo-message__date"><?php echo esc_html($msg['date'] ?? ''); ?></span>
                    <?php if ($type === 'email'): ?>
                        <span class="fusion-woodoo-message__type-badge"><?php esc_html_e('Email', 'fusion-woodoo'); ?></span>
                    <?php elseif ($type === 'note'): ?>
                        <span class="fusion-woodoo-message__type-badge fusion-woodoo-message__type-badge--note"><?php esc_html_e('Note', 'fusion-woodoo'); ?></span>
                    <?php endif; ?>
                </div>
                <div class="fusion-woodoo-message__body">
                    <?php echo wp_kses_post($msg['body'] ?? ''); ?>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>
