<?php
session_start();
include '../layouts/dbconn.php';

if (!isset($_SESSION['user_id']) || $_SESSION['role_code'] !== 'RESIDENT_DOCTOR') {
    echo '<div class="alert alert-danger">Unauthorized access</div>';
    exit();
}

$id = (int)$_GET['id'];

// Fetch consultation details
$sql = "SELECT 
            rsc.*,
            c.consultation_number,
            c.chief_complaint,
            c.history_present_illness,
            c.diagnosis_primary,
            c.diagnosis_secondary,
            c.treatment_plan,
            c.doctor_notes,
            c.started_at,
            p.patient_number,
            u.full_name as patient_name,
            u.gender,
            u.date_of_birth,
            TIMESTAMPDIFF(YEAR, u.date_of_birth, CURDATE()) as age,
            sv.full_name as supervisor_name,
            d.specialization as supervisor_specialty
        FROM resident_supervised_consultations rsc
        INNER JOIN consultations c ON rsc.consultation_id = c.consultation_id
        INNER JOIN patients p ON c.patient_id = p.patient_id
        INNER JOIN users u ON p.user_id = u.user_id
        INNER JOIN doctors d ON rsc.supervisor_id = d.doctor_id
        INNER JOIN users sv ON d.user_id = sv.user_id
        WHERE rsc.supervised_consultation_id = ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    echo '<div class="alert alert-warning">Consultation not found</div>';
    exit();
}

$cons = $result->fetch_assoc();
?>

<div class="row">
    <!-- Patient & Consultation Info -->
    <div class="col-md-6 mb-3">
        <div class="border rounded p-3">
            <h6 class="mb-3">Patient Information</h6>
            <div class="mb-2">
                <strong>Name:</strong> <?php echo htmlspecialchars($cons['patient_name']); ?>
            </div>
            <div class="mb-2">
                <strong>Patient Number:</strong> <?php echo htmlspecialchars($cons['patient_number']); ?>
            </div>
            <div class="mb-2">
                <strong>Age/Gender:</strong> <?php echo $cons['age']; ?> years / <?php echo $cons['gender']; ?>
            </div>
            <div class="mb-2">
                <strong>Consultation Date:</strong> <?php echo date('M d, Y h:i A', strtotime($cons['started_at'])); ?>
            </div>
        </div>
    </div>

    <!-- Supervision Details -->
    <div class="col-md-6 mb-3">
        <div class="border rounded p-3">
            <h6 class="mb-3">Supervision Details</h6>
            <div class="mb-2">
                <strong>Supervisor:</strong> <?php echo htmlspecialchars($cons['supervisor_name']); ?>
            </div>
            <div class="mb-2">
                <strong>Specialty:</strong> <?php echo htmlspecialchars($cons['supervisor_specialty']); ?>
            </div>
            <div class="mb-2">
                <strong>Supervision Level:</strong>
                <span class="badge bg-info"><?php echo str_replace('_', ' ', $cons['supervision_level']); ?></span>
            </div>
            <?php if ($cons['competency_rating']): ?>
            <div class="mb-2">
                <strong>Competency Rating:</strong>
                <span class="badge bg-warning"><?php echo $cons['competency_rating']; ?>/10</span>
            </div>
            <?php endif; ?>
            <div class="mb-2">
                <strong>Learning Value:</strong>
                <span class="badge bg-<?php echo $cons['learning_value'] === 'HIGH' || $cons['learning_value'] === 'EXCEPTIONAL' ? 'success' : 'secondary'; ?>">
                    <?php echo $cons['learning_value']; ?>
                </span>
            </div>
        </div>
    </div>

    <!-- Clinical Information -->
    <div class="col-12 mb-3">
        <div class="border rounded p-3">
            <h6 class="mb-3">Clinical Information</h6>
            
            <div class="mb-3">
                <strong>Chief Complaint:</strong>
                <p><?php echo nl2br(htmlspecialchars($cons['chief_complaint'])); ?></p>
            </div>

            <?php if ($cons['history_present_illness']): ?>
            <div class="mb-3">
                <strong>History of Present Illness:</strong>
                <p><?php echo nl2br(htmlspecialchars($cons['history_present_illness'])); ?></p>
            </div>
            <?php endif; ?>

            <?php if ($cons['diagnosis_primary']): ?>
            <div class="mb-3">
                <strong>Primary Diagnosis:</strong>
                <p><?php echo nl2br(htmlspecialchars($cons['diagnosis_primary'])); ?></p>
            </div>
            <?php endif; ?>

            <?php if ($cons['treatment_plan']): ?>
            <div class="mb-3">
                <strong>Treatment Plan:</strong>
                <p><?php echo nl2br(htmlspecialchars($cons['treatment_plan'])); ?></p>
            </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Resident Assessment -->
    <?php if ($cons['resident_assessment'] || $cons['resident_diagnosis'] || $cons['resident_treatment_plan']): ?>
    <div class="col-12 mb-3">
        <div class="border rounded p-3 bg-light">
            <h6 class="mb-3"><i class="ri-user-star-line me-2"></i>Your Initial Assessment</h6>
            
            <?php if ($cons['resident_assessment']): ?>
            <div class="mb-3">
                <strong>Your Assessment:</strong>
                <p><?php echo nl2br(htmlspecialchars($cons['resident_assessment'])); ?></p>
            </div>
            <?php endif; ?>

            <?php if ($cons['resident_diagnosis']): ?>
            <div class="mb-3">
                <strong>Your Diagnosis:</strong>
                <p><?php echo nl2br(htmlspecialchars($cons['resident_diagnosis'])); ?></p>
            </div>
            <?php endif; ?>

            <?php if ($cons['resident_treatment_plan']): ?>
            <div class="mb-3">
                <strong>Your Treatment Plan:</strong>
                <p><?php echo nl2br(htmlspecialchars($cons['resident_treatment_plan'])); ?></p>
            </div>
            <?php endif; ?>
        </div>
    </div>
    <?php endif; ?>

    <!-- Supervisor Feedback -->
    <div class="col-12 mb-3">
        <div class="border rounded p-3 border-primary">
            <h6 class="mb-3 text-primary"><i class="ri-feedback-line me-2"></i>Supervisor Feedback</h6>
            
            <?php if ($cons['supervisor_corrections']): ?>
            <div class="mb-3">
                <strong class="text-danger">Corrections:</strong>
                <p><?php echo nl2br(htmlspecialchars($cons['supervisor_corrections'])); ?></p>
            </div>
            <?php endif; ?>

            <?php if ($cons['supervisor_additions']): ?>
            <div class="mb-3">
                <strong class="text-info">Additional Points:</strong>
                <p><?php echo nl2br(htmlspecialchars($cons['supervisor_additions'])); ?></p>
            </div>
            <?php endif; ?>

            <?php if ($cons['teaching_points']): ?>
            <div class="mb-3">
                <strong class="text-success">Teaching Points:</strong>
                <p><?php echo nl2br(htmlspecialchars($cons['teaching_points'])); ?></p>
            </div>
            <?php endif; ?>

            <?php if ($cons['supervision_notes']): ?>
            <div class="mb-3">
                <strong>General Notes:</strong>
                <p><?php echo nl2br(htmlspecialchars($cons['supervision_notes'])); ?></p>
            </div>
            <?php endif; ?>

            <?php if ($cons['skills_demonstrated']): ?>
            <div class="mb-3">
                <strong>Skills Demonstrated:</strong>
                <div class="mt-2">
                    <?php
                    $skills = json_decode($cons['skills_demonstrated'], true);
                    if ($skills && is_array($skills)) {
                        foreach ($skills as $skill) {
                            echo '<span class="badge bg-success me-2 mb-2">' . htmlspecialchars($skill) . '</span>';
                        }
                    }
                    ?>
                </div>
            </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<?php mysqli_close($conn); ?>