<?php
session_start();
if (!isset($_SESSION['admin_id'])) { header("Location: login.php"); exit(); }
include '../include/config.php';

$message_id = (int)($_GET['id'] ?? 0);
if (!$message_id) {
    header("Location: manage_messages.php");
    exit();
}

// Get message details
$message = $conn->query("
    SELECT pm.*, 
           p.first_name as patient_first, p.last_name as patient_last, p.email as patient_email, p.phone as patient_phone,
           d.name as doctor_name, d.specialization, d.email as doctor_email
    FROM patient_doctor_messages pm
    LEFT JOIN patients p ON pm.patient_id = p.patient_id
    LEFT JOIN doctors d ON pm.doctor_id = d.doctor_id
    WHERE pm.id = $message_id
")->fetch_assoc();

if (!$message) {
    header("Location: manage_messages.php");
    exit();
}

// Mark as read
$conn->query("UPDATE patient_doctor_messages SET is_read = 1 WHERE id = $message_id");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Message - Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
    <style>
        body { background: #0a3d2f; font-family: 'Segoe UI', sans-serif; }
        .navbar { background: #064e3b !important; }
        .card { border-radius: 20px; background: rgba(255,255,255,0.95); }
        .message-bubble-patient { background: #0f6b4c; color: white; padding: 12px 18px; border-radius: 20px 20px 20px 5px; display: inline-block; max-width: 70%; }
        .message-bubble-doctor { background: #e9ecef; color: #333; padding: 12px 18px; border-radius: 20px 20px 5px 20px; display: inline-block; max-width: 70%; }
        .info-card { background: #f8f9fa; border-radius: 15px; padding: 15px; margin-bottom: 20px; }
    </style>
</head>
<body>
<nav class="navbar navbar-dark">
    <div class="container">
        <span class="navbar-brand"><i class="fas fa-envelope-open-text"></i> View Message</span>
        <a href="manage_messages.php" class="btn btn-light btn-sm">Back</a>
    </div>
</nav>

<div class="container mt-4">
    <div class="card">
        <div class="card-header bg-success text-white">
            <h5>Message Details</h5>
        </div>
        <div class="card-body">
            <!-- Sender/Recipient Info -->
            <div class="row">
                <div class="col-md-6">
                    <div class="info-card">
                        <h6><i class="fas fa-user-circle"></i> 
                            <?php if($message['sender'] == 'patient'): ?>
                                Patient: <?= htmlspecialchars($message['patient_first'] . ' ' . $message['patient_last']) ?>
                            <?php else: ?>
                                Doctor: Dr. <?= htmlspecialchars($message['doctor_name']) ?>
                            <?php endif; ?>
                        </h6>
                        <p class="mb-0 small text-muted">
                            <i class="fas fa-envelope"></i> <?= $message['sender'] == 'patient' ? $message['patient_email'] : $message['doctor_email'] ?>
                        </p>
                        <p class="mb-0 small text-muted">
                            <i class="fas fa-phone"></i> <?= $message['sender'] == 'patient' ? ($message['patient_phone'] ?? 'Not provided') : 'N/A' ?>
                        </p>
                        <p class="mb-0 small text-muted">
                            <i class="fas fa-clock"></i> Sent: <?= date('M d, Y h:i A', strtotime($message['created_at'])) ?>
                        </p>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="info-card">
                        <h6><i class="fas fa-reply-all"></i> 
                            <?php if($message['sender'] == 'patient'): ?>
                                Recipient: Dr. <?= htmlspecialchars($message['doctor_name']) ?>
                            <?php else: ?>
                                Recipient: <?= htmlspecialchars($message['patient_first'] . ' ' . $message['patient_last']) ?>
                            <?php endif; ?>
                        </h6>
                        <p class="mb-0 small text-muted">
                            <i class="fas fa-stethoscope"></i> 
                            <?php if($message['sender'] == 'patient'): ?>
                                Specialization: <?= htmlspecialchars($message['specialization'] ?? 'General Medicine') ?>
                            <?php else: ?>
                                Patient ID: <?= $message['patient_id'] ?>
                            <?php endif; ?>
                        </p>
                    </div>
                </div>
            </div>

            <!-- Message Content -->
            <div class="text-center mb-3">
                <span class="badge bg-secondary">Message</span>
            </div>
            <div class="text-<?= $message['sender'] == 'patient' ? 'end' : 'start' ?>">
                <div class="message-bubble-<?= $message['sender'] == 'patient' ? 'patient' : 'doctor' ?>">
                    <?= nl2br(htmlspecialchars($message['message'])) ?>
                </div>
            </div>

            <!-- Reply Note -->
            <?php if($message['sender'] == 'patient'): ?>
            <div class="alert alert-info mt-4">
                <i class="fas fa-info-circle"></i> 
                To reply to this patient, please contact them via phone or WhatsApp. Direct messaging from admin is not available.
            </div>
            <?php endif; ?>
        </div>
        <div class="card-footer text-center">
            <a href="manage_messages.php" class="btn btn-secondary">Back to Messages</a>
            <a href="manage_messages.php?delete=<?= $message['id'] ?>" class="btn btn-danger" onclick="return confirm('Delete this message?')">Delete Message</a>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>