<?php
require_once '../layouts/session.php';
require_once '../layouts/dbconn.php';

header('Content-Type: application/json');

if (!isset($_SESSION['user_role']) || $_SESSION['user_role'] !== 'HEAD_NURSE') {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit();
}

$conn = db_connect();

try {
    // In production, fetch from shift_handovers table
    // For now, create sample data from activity logs
    
    $handover_sql = "SELECT al.*, u.first_name, u.last_name,
                     CONCAT(u.first_name, ' ', u.last_name) as user_name
                     FROM activity_log al
                     INNER JOIN users u ON al.user_id = u.user_id
                     WHERE al.action = 'SHIFT_HANDOVER'
                     AND DATE(al.logged_at) = CURDATE()
                     ORDER BY al.logged_at DESC
                     LIMIT 5";
    
    $result = $conn->query($handover_sql);
    
    $html = '';
    
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
            $data = json_decode($row['new_values'], true);
            
            $html .= '<div class="handover-card">';
            $html .= '<div class="d-flex justify-content-between mb-2">';
            $html .= '<strong>' . htmlspecialchars($data['handover_type'] ?? 'Shift Handover') . '</strong>';
            $html .= '<span class="text-muted">' . date('h:i A', strtotime($row['logged_at'])) . '</span>';
            $html .= '</div>';
            $html .= '<div class="small mb-2">';
            $html .= '<strong>From:</strong> ' . htmlspecialchars($data['outgoing_nurse'] ?? 'N/A') . '<br>';
            $html .= '<strong>To:</strong> ' . htmlspecialchars($data['incoming_nurse'] ?? 'N/A');
            $html .= '</div>';
            if (isset($data['patient_updates'])) {
                $html .= '<div class="small text-muted">' . 
                         htmlspecialchars(substr($data['patient_updates'], 0, 100)) . 
                         '...</div>';
            }
            $html .= '</div>';
        }
    } else {
        // Create sample handovers if none exist
        $html .= '<div class="handover-card">';
        $html .= '<div class="d-flex justify-content-between mb-2">';
        $html .= '<strong>Day to Night Shift Handover</strong>';
        $html .= '<span class="text-muted">6:00 PM</span>';
        $html .= '</div>';
        $html .= '<div class="small mb-2">';
        $html .= '<strong>From:</strong> Day Shift Team<br>';
        $html .= '<strong>To:</strong> Night Shift Team';
        $html .= '</div>';
        $html .= '<div class="small text-muted">All patients stable. Ward A has 8 patients, Ward B has 6 patients. Bed 203 requires frequent monitoring.</div>';
        $html .= '</div>';
        
        $html .= '<div class="alert alert-info">';
        $html .= '<i class="ri-information-line me-2"></i>';
        $html .= 'No shift handovers recorded yet today. Click "Initiate Shift Handover" to create one.';
        $html .= '</div>';
    }
    
    echo json_encode([
        'success' => true,
        'html' => $html
    ]);
    
} catch (Exception $e) {
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
}

$conn->close();
?>