<?php
// admin/dispute_view.php
require_once __DIR__ . '/../config/db.php';
if (session_status() === PHP_SESSION_NONE) { session_start(); }

// 1. SEGURIDAD: Solo admin
if (($_SESSION['role'] ?? 'user') !== 'admin') { die("Acceso denegado."); }

$order_id = intval($_GET['id'] ?? 0);

// 2. OBTENER INFORMACIÓN DE LA DISPUTA
$stmt = $pdo->prepare("
    SELECT o.*, p.title as product_name, b.username as b_name, s.username as s_name 
    FROM orders o 
    JOIN products p ON o.product_id = p.id 
    JOIN users b ON o.buyer_id = b.id 
    JOIN users s ON o.seller_id = s.id 
    WHERE o.id = ?
");
$stmt->execute([$order_id]);
$order = $stmt->fetch();

if (!$order) { die("Caso no encontrado."); }

// 3. OBTENER EL CHAT DE LA DISPUTA
$messages = $pdo->prepare("
    SELECT dm.*, u.username 
    FROM dispute_messages dm 
    JOIN users u ON dm.sender_id = u.id 
    WHERE dm.order_id = ? 
    ORDER BY dm.created_at ASC
");
$messages->execute([$order_id]);
$msgs = $messages->fetchAll();

include '../includes/header.php';
?>

<style>
    .dispute-chat-container {
        background: #161b22;
        border: 1px solid #30363d;
        border-radius: 12px;
        padding: 25px;
        height: 550px;
        overflow-y: auto;
        margin-bottom: 20px;
        display: flex;
        flex-direction: column;
        gap: 20px;
    }
    .msg-bubble {
        padding: 15px;
        border-radius: 12px;
        max-width: 85%;
        position: relative;
        border: 1px solid #30363d;
    }
    .msg-buyer { border-left: 4px solid #00ff88; background: rgba(0, 255, 136, 0.02); }
    .msg-seller { border-left: 4px solid #d4af37; background: rgba(212, 175, 55, 0.02); align-self: flex-end; }
    
    .evidence-img {
        margin-top: 10px;
        max-width: 100%;
        max-height: 400px;
        border-radius: 8px;
        border: 1px solid #30363d;
        display: block;
        transition: 0.3s;
        cursor: zoom-in;
    }
    .evidence-img:hover { border-color: #5865F2; } /* Discord Blue al hacer hover */
    
    .img-error-msg {
        font-size: 11px;
        color: #ff4d4d;
        background: rgba(255, 77, 77, 0.1);
        padding: 8px;
        border-radius: 4px;
        display: inline-block;
        margin-top: 10px;
    }
</style>

<div style="max-width: 1000px; margin: 40px auto; padding: 0 20px; color: white; font-family: 'Inter', sans-serif;">
    <div style="display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 20px;">
        <div>
            <a href="disputes.php" style="color: #8b949e; text-decoration: none; font-size: 11px; font-weight: 800;">
                <i class="fa-solid fa-arrow-left"></i> VOLVER AL TRIBUNAL
            </a>
            <h2 style="font-family: 'Syncopate'; margin: 10px 0 0 0; color: #ff4d4d;">CASE<span>.EVIDENCE</span></h2>
            <p style="color: #8b949e; font-size: 12px;">Analizando disputa de la Orden #<?php echo $order_id; ?></p>
        </div>
        
        <div style="text-align: right;">
            <div style="font-size: 14px; font-weight: 700;"><?php echo htmlspecialchars($order['product_name']); ?></div>
            <div style="color: #00ff88; font-weight: 800; font-size: 18px;">$<?php echo number_format($order['total_price'], 2); ?></div>
        </div>
    </div>

    <div class="dispute-chat-container">
        <?php if(empty($msgs)): ?>
            <div style="text-align:center; margin-top:150px; color:#444;">
                <i class="fa-solid fa-comment-slash" style="font-size: 40px; display:block; margin-bottom:10px;"></i>
                No hay alegatos presentados en este caso.
            </div>
        <?php endif; ?>

        <?php foreach($msgs as $m): 
            $is_buyer = ($m['username'] == $order['b_name']);
        ?>
            <div class="msg-bubble <?php echo $is_buyer ? 'msg-buyer' : 'msg-seller'; ?>">
                <div style="display: flex; justify-content: space-between; margin-bottom: 8px;">
                    <span style="font-weight: 900; font-size: 11px; text-transform: uppercase; color: <?php echo $is_buyer ? '#00ff88' : '#d4af37'; ?>;">
                        @<?php echo htmlspecialchars($m['username']); ?> (<?php echo $is_buyer ? 'Buyer' : 'Seller'; ?>)
                    </span>
                    <span style="font-size: 10px; color: #555;"><?php echo date('H:i - d M', strtotime($m['created_at'])); ?></span>
                </div>
                
                <p style="margin: 0; font-size: 13px; line-height: 1.6; color: #c9d1d9;">
                    <?php echo nl2br(htmlspecialchars($m['message'])); ?>
                </p>

                <?php if($m['evidence_url']): ?>
                    <div style="margin-top: 12px;">
                        <span style="display:block; font-size: 10px; color: #8b949e; margin-bottom: 5px; font-weight: 800;">CAPTURA ADJUNTA:</span>
                        <a href="<?php echo htmlspecialchars($m['evidence_url']); ?>" target="_blank">
                            <img src="<?php echo htmlspecialchars($m['evidence_url']); ?>" 
                                 class="evidence-img" 
                                 alt="Evidence"
                                 onerror="this.style.display='none'; this.nextElementSibling.style.display='block';">
                            <div class="img-error-msg" style="display:none;">
                                <i class="fa-solid fa-triangle-exclamation"></i> Error al cargar imagen. El link de Discord puede haber expirado.
                            </div>
                        </a>
                    </div>
                <?php endif; ?>
            </div>
        <?php endforeach; ?>
    </div>

    <div style="display: grid; grid-template-columns: 1fr 250px; gap: 20px; align-items: start;">
        <div style="background: #0d1117; padding: 20px; border-radius: 12px; border: 1px solid #30363d;">
            <h4 style="margin: 0 0 10px 0; font-size: 12px; color: #8b949e; text-transform: uppercase;">Internal Judge Note</h4>
            <textarea style="width:100%; background:#161b22; border:1px solid #30363d; color:white; padding:12px; border-radius:8px; font-size: 13px; outline: none;" rows="3" placeholder="Escribe aquí tus observaciones del caso (privado para admins)..."></textarea>
        </div>

        <div style="display: flex; flex-direction: column; gap: 10px;">
            <a href="disputes.php" style="text-align: center; background: #ff4d4d; color: white; padding: 15px; border-radius: 8px; text-decoration: none; font-weight: 800; font-size: 12px;">
                IR A DICTAR SENTENCIA
            </a>
            <p style="font-size: 10px; color: #555; text-align: center;">
                Recuerda revisar todas las pruebas antes de liberar o reembolsar fondos.
            </p>
        </div>
    </div>
</div>

<?php include '../includes/footer.php'; ?>