<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (!isset($_SESSION['doctor_id'])) {
    header("Location: login.php");
    exit();
}

include '../include/config.php';

$doctor_id = $_SESSION['doctor_id'];
$doctor_name = $_SESSION['doctor_name'];

// Create notifications table if not exists
$conn->query("CREATE TABLE IF NOT EXISTS doctor_notifications (
    id INT AUTO_INCREMENT PRIMARY KEY,
    doctor_id INT NOT NULL,
    title VARCHAR(255),
    message TEXT,
    type ENUM('info','success','warning','danger') DEFAULT 'info',
    is_read TINYINT(1) DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");

// Mark notification as read
if (isset($_GET['mark_read']) && isset($_GET['id'])) {
    $notif_id = intval($_GET['id']);
    $conn->query("UPDATE doctor_notifications SET is_read = 1 WHERE id = '$notif_id' AND doctor_id = '$doctor_id'");
    header("Location: notifications.php");
    exit();
}

// Mark all as read
if (isset($_GET['mark_all_read'])) {
    $conn->query("UPDATE doctor_notifications SET is_read = 1 WHERE doctor_id = '$doctor_id'");
    header("Location: notifications.php");
    exit();
}

// Delete notification
if (isset($_GET['delete']) && isset($_GET['id'])) {
    $notif_id = intval($_GET['id']);
    $conn->query("DELETE FROM doctor_notifications WHERE id = '$notif_id' AND doctor_id = '$doctor_id'");
    header("Location: notifications.php");
    exit();
}

// Get all notifications
$notifications = $conn->query("
    SELECT * FROM doctor_notifications 
    WHERE doctor_id = '$doctor_id' 
    ORDER BY created_at DESC 
    LIMIT 50
");

// Get counts
$unread_count = $conn->query("SELECT COUNT(*) as cnt FROM doctor_notifications WHERE doctor_id = '$doctor_id' AND is_read = 0")->fetch_assoc()['cnt'] ?? 0;
$total_count = $conn->query("SELECT COUNT(*) as cnt FROM doctor_notifications WHERE doctor_id = '$doctor_id'")->fetch_assoc()['cnt'] ?? 0;

$page_title = 'Notifications | UHD';
include '../include/header.php';
?>

<style>
    .notification-card {
        background: white;
        border-radius: 16px;
        padding: 0;
        margin-bottom: 15px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.05);
        transition: all 0.2s;
        overflow: hidden;
    }
    .notification-card:hover {
        transform: translateX(5px);
        box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    }
    .notification-card.unread {
        border-left: 4px solid #d4a017;
        background: #fffef7;
    }
    .notification-card.read {
        border-left: 4px solid #e0e0e0;
        background: white;
    }
    .notification-icon {
        width: 50px;
        height: 50px;
        border-radius: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.3rem;
    }
    .notification-icon.info { background: #e3f2fd; color: #2196f3; }
    .notification-icon.success { background: #e8f5e9; color: #4caf50; }
    .notification-icon.warning { background: #fff3e0; color: #ff9800; }
    .notification-icon.danger { background: #ffebee; color: #f44336; }
    .notification-title {
        font-weight: 600;
        margin-bottom: 5px;
        color: #333;
    }
    .notification-message {
        color: #666;
        font-size: 0.85rem;
        margin: 0;
        line-height: 1.4;
    }
    .notification-time {
        font-size: 0.7rem;
        color: #999;
    }
    .btn-action-icon {
        background: none;
        border: none;
        color: #999;
        cursor: pointer;
        transition: 0.2s;
        padding: 5px;
    }
    .btn-action-icon:hover {
        color: #d4a017;
    }
    .empty-state {
        text-align: center;
        padding: 60px 20px;
    }
    .empty-state i {
        font-size: 4rem;
        color: #ccc;
        margin-bottom: 20px;
    }
    .filter-btn {
        padding: 8px 20px;
        border-radius: 30px;
        background: #f0f0f0;
        color: #333;
        text-decoration: none;
        transition: 0.2s;
        display: inline-block;
    }
    .filter-btn.active {
        background: #d4a017;
        color: white;
    }
    .filter-btn:hover:not(.active) {
        background: #e0e0e0;
    }
</style>

<div class="container mt-4">
    <!-- Header -->
    <div class="d-flex justify-content-between align-items-center flex-wrap mb-4">
        <div>
            <h2><i class="fas fa-bell me-2" style="color: #d4a017;"></i> Notifications</h2>
            <p class="text-muted mb-0">Stay updated with your latest alerts and reminders</p>
        </div>
        <div class="mt-2 mt-md-0">
            <?php if ($unread_count > 0): ?>
                <a href="?mark_all_read=1" class="btn btn-outline-success btn-sm rounded-pill me-2" onclick="return confirm('Mark all notifications as read?')">
                    <i class="fas fa-check-double"></i> Mark All Read
                </a>
            <?php endif; ?>
            <a href="dashboard.php" class="btn btn-secondary btn-sm rounded-pill">
                <i class="fas fa-arrow-left"></i> Back to Dashboard
            </a>
        </div>
    </div>

    <!-- Stats Summary -->
    <div class="row g-3 mb-4">
        <div class="col-md-4">
            <div class="card border-0 shadow-sm rounded-4">
                <div class="card-body text-center">
                    <i class="fas fa-bell fa-2x text-warning mb-2"></i>
                    <h3 class="mb-0"><?= $unread_count ?></h3>
                    <p class="text-muted">Unread Notifications</p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card border-0 shadow-sm rounded-4">
                <div class="card-body text-center">
                    <i class="fas fa-envelope-open fa-2x text-success mb-2"></i>
                    <h3 class="mb-0"><?= $total_count - $unread_count ?></h3>
                    <p class="text-muted">Read Notifications</p>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card border-0 shadow-sm rounded-4">
                <div class="card-body text-center">
                    <i class="fas fa-database fa-2x text-info mb-2"></i>
                    <h3 class="mb-0"><?= $total_count ?></h3>
                    <p class="text-muted">Total Notifications</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Notifications List -->
    <?php if ($notifications && $notifications->num_rows > 0): ?>
        <?php while ($notif = $notifications->fetch_assoc()): 
            $icon_class = '';
            $icon_symbol = '';
            switch($notif['type']) {
                case 'info':
                    $icon_class = 'info';
                    $icon_symbol = 'fa-info-circle';
                    break;
                case 'success':
                    $icon_class = 'success';
                    $icon_symbol = 'fa-check-circle';
                    break;
                case 'warning':
                    $icon_class = 'warning';
                    $icon_symbol = 'fa-exclamation-triangle';
                    break;
                case 'danger':
                    $icon_class = 'danger';
                    $icon_symbol = 'fa-times-circle';
                    break;
                default:
                    $icon_class = 'info';
                    $icon_symbol = 'fa-bell';
            }
        ?>
            <div class="notification-card <?= $notif['is_read'] ? 'read' : 'unread' ?>">
                <div class="p-3">
                    <div class="row align-items-center">
                        <div class="col-auto">
                            <div class="notification-icon <?= $icon_class ?>">
                                <i class="fas <?= $icon_symbol ?>"></i>
                            </div>
                        </div>
                        <div class="col">
                            <div class="notification-title">
                                <?= htmlspecialchars($notif['title']) ?>
                                <?php if (!$notif['is_read']): ?>
                                    <span class="badge bg-warning ms-2">New</span>
                                <?php endif; ?>
                            </div>
                            <p class="notification-message"><?= nl2br(htmlspecialchars($notif['message'])) ?></p>
                            <div class="notification-time">
                                <i class="far fa-clock me-1"></i> <?= date('M d, Y h:i A', strtotime($notif['created_at'])) ?>
                            </div>
                        </div>
                        <div class="col-auto">
                            <div class="d-flex gap-2">
                                <?php if (!$notif['is_read']): ?>
                                    <a href="?mark_read=1&id=<?= $notif['id'] ?>" class="btn-action-icon" title="Mark as read">
                                        <i class="fas fa-check-circle"></i>
                                    </a>
                                <?php endif; ?>
                                <a href="?delete=1&id=<?= $notif['id'] ?>" class="btn-action-icon" title="Delete" onclick="return confirm('Delete this notification?')">
                                    <i class="fas fa-trash-alt"></i>
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        <?php endwhile; ?>
    <?php else: ?>
        <div class="empty-state">
            <i class="fas fa-bell-slash"></i>
            <h4>No Notifications</h4>
            <p class="text-muted">You're all caught up! New notifications will appear here.</p>
            <a href="dashboard.php" class="btn btn-gold mt-3 rounded-pill">
                <i class="fas fa-home"></i> Return to Dashboard
            </a>
        </div>
    <?php endif; ?>
</div>

<style>
.btn-gold {
    background: #d4a017;
    color: white;
    border: none;
    padding: 10px 25px;
    border-radius: 40px;
    transition: 0.3s;
}
.btn-gold:hover {
    background: #b8860b;
    color: white;
    transform: translateY(-2px);
}
</style>

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