<?php
 
$contactEmail = Setting::get('contact_email', '');
?>

<div class="max-w-3xl">
    <a href="/admin/inbox?tab=messages" class="inline-flex items-center gap-1.5 text-sm text-gray-500 hover:text-gray-700 transition-colors mb-4">
        <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>
        Back to Inbox
    </a>

    <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
        
        <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
            <div class="flex items-center gap-2 flex-wrap">
                <h2 class="text-lg font-semibold text-gray-900"><?= htmlspecialchars($message['subject']) ?></h2>
                <?php if (!empty($message['subject_type'])): ?>
                <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-blue-100 text-blue-700"><?= htmlspecialchars($message['subject_type']) ?></span>
                <?php endif; ?>
            </div>
            <div class="mt-2 flex flex-wrap items-center gap-x-2 sm:gap-x-4 gap-y-1 text-sm text-gray-500">
                <span class="flex items-center gap-1.5">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M20 21v-2a4 4 0 00-4-4H8a4 4 0 00-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>
                    <strong class="text-gray-700"><?= htmlspecialchars($message['name']) ?></strong>
                </span>
                <span class="flex items-center gap-1.5">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"/><polyline points="22,6 12,13 2,6"/></svg>
                    <a href="mailto:<?= htmlspecialchars($message['email']) ?>" class="text-blue-600 hover:underline"><?= htmlspecialchars($message['email']) ?></a>
                </span>
                <span class="flex items-center gap-1.5">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>
                    <?= date('F j, Y \a\t g:i A', strtotime($message['created_at'])) ?>
                </span>
                <?php if ($message['ip_address']): ?>
                <span class="flex items-center gap-1.5 text-xs text-gray-400">
                    IP: <?= htmlspecialchars($message['ip_address']) ?>
                </span>
                <?php endif; ?>
            </div>
        </div>

        
        <div class="px-6 py-5">
            <div class="text-sm text-gray-800 whitespace-pre-wrap leading-relaxed"><?= htmlspecialchars($message['message']) ?></div>
        </div>

        <?php
        $contentUrls = [];
        if (!empty($message['content_urls'])) {
            $contentUrls = json_decode($message['content_urls'], true) ?: [];
        }
        ?>
        <?php if (!empty($contentUrls)): ?>
        <div class="px-6 py-4 border-t border-gray-100 bg-yellow-50/50">
            <h4 class="text-xs font-semibold text-gray-500 uppercase tracking-wider mb-2">Reported Content URLs</h4>
            <ul class="space-y-1">
                <?php foreach ($contentUrls as $url): ?>
                <li>
                    <a href="<?= htmlspecialchars($url) ?>" target="_blank" rel="noopener noreferrer" class="text-sm text-blue-600 hover:text-blue-800 hover:underline break-all">
                        <?= htmlspecialchars($url) ?>
                        <svg class="inline w-3 h-3 ml-0.5" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" y1="14" x2="21" y2="3"/></svg>
                    </a>
                </li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php endif; ?>

        
        <div class="px-6 py-4 border-t border-gray-200 bg-gray-50 flex flex-wrap items-center justify-between gap-3">
            <a href="mailto:<?= htmlspecialchars($message['email']) ?>?subject=Re:%20<?= rawurlencode($message['subject']) ?>"
               class="inline-flex items-center gap-2 px-4 py-2 bg-blue-600 text-white text-sm font-medium rounded-lg hover:bg-blue-700 transition-colors">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><polyline points="9 14 4 9 9 4"/><path d="M20 20v-7a4 4 0 00-4-4H4"/></svg>
                Reply by Email
            </a>
            <form method="POST" action="/admin/messages/<?= (int)$message['id'] ?>/delete" onsubmit="return confirm('Delete this message?')">
                <?= Router::csrfField() ?>
                <button type="submit" class="inline-flex items-center gap-2 px-4 py-2 text-sm font-medium text-red-600 bg-white border border-red-200 rounded-lg hover:bg-red-50 transition-colors">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><polyline points="3 6 5 6 21 6"/><path d="M19 6l-2 14H7L5 6"/><path d="M10 11v6"/><path d="M14 11v6"/></svg>
                    Delete
                </button>
            </form>
        </div>
    </div>
</div>
