<?php 
// ১. বেসিক কনফিগারেশন এবং সেশন চেক
ini_set('display_errors', 1); error_reporting(E_ALL);
date_default_timezone_set('Asia/Dhaka');
require_once 'db_connect.php'; 

if (session_status() === PHP_SESSION_NONE) { session_start(); }
if (!isset($_SESSION['student_id'])) { header("Location: login"); exit; }

$student_id = $_SESSION['student_id'];

/**
 * ২. ডাটাবেস অটো-ফিক্স লজিক (যাতে Column Not Found এরর না আসে)
 */
try {
    // is_read কলাম চেক এবং ফিক্স
    $check_read = $pdo->query("SHOW COLUMNS FROM notifications LIKE 'is_read'")->fetch();
    if (!$check_read) {
        $pdo->exec("ALTER TABLE notifications ADD COLUMN is_read TINYINT(1) DEFAULT 0");
    }
    // type কলাম চেক এবং ফিক্স
    $check_type = $pdo->query("SHOW COLUMNS FROM notifications LIKE 'type'")->fetch();
    if (!$check_type) {
        $pdo->exec("ALTER TABLE notifications ADD COLUMN type VARCHAR(50) DEFAULT 'general' AFTER message");
    }
    // link কলাম চেক এবং ফিক্স
    $check_link = $pdo->query("SHOW COLUMNS FROM notifications LIKE 'link'")->fetch();
    if (!$check_link) {
        $pdo->exec("ALTER TABLE notifications ADD COLUMN link VARCHAR(255) DEFAULT '#' AFTER type");
    }
} catch (PDOException $e) {
    // টেবিল না থাকলে বা অন্য কোনো ইস্যু থাকলে এখানে হ্যান্ডেল হবে
}

// ৩. লজিক: সব নোটিফিকেশন রিড (Read) করা
if (isset($_GET['action']) && $_GET['action'] == 'mark_all_read') {
    $update_stmt = $pdo->prepare("UPDATE notifications SET is_read = 1 WHERE student_id = ?");
    $update_stmt->execute([$student_id]);
    header("Location: notifications.php"); exit;
}

// ৪. লজিক: নোটিফিকেশন ডিলিট করা
if (isset($_GET['delete_id'])) {
    $del_stmt = $pdo->prepare("DELETE FROM notifications WHERE id = ? AND student_id = ?");
    $del_stmt->execute([(int)$_GET['delete_id'], $student_id]);
    header("Location: notifications.php"); exit;
}

// ৫. ডাটা ফেচ করা
try {
    $stmt = $pdo->prepare("SELECT * FROM notifications WHERE student_id = ? ORDER BY created_at DESC");
    $stmt->execute([$student_id]);
    $notifications = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) { $notifications = []; }

// হেডার ইনক্লুড
include 'includes/user_header.php'; 
?>

<style>
    :root { --p-green: #064e3b; --p-gold: #d4af37; --p-dark: #022c22; --p-bg: #f8fafc; }
    .main-content { background-color: var(--p-bg); min-height: 100vh; padding: 25px; font-family: 'Outfit', sans-serif; }
    
    /* Header UI */
    .notif-header { background: #fff; padding: 20px 30px; border-radius: 20px; box-shadow: 0 4px 15px rgba(0,0,0,0.03); margin-bottom: 25px; border-left: 5px solid var(--p-green); display: flex; justify-content: space-between; align-items: center; }
    
    /* Card UI */
    .notif-card { background: #fff; border-radius: 18px; border: 1px solid #eef2f6; padding: 18px; margin-bottom: 12px; transition: 0.3s ease; display: flex; gap: 15px; align-items: center; position: relative; }
    .notif-card:hover { transform: translateX(5px); border-color: var(--p-green); box-shadow: 0 10px 30px rgba(0,0,0,0.05); }
    .notif-card.unread { background: #fffcf0; border-left: 4px solid var(--p-gold); }
    
    /* Icons based on type */
    .notif-icon { width: 45px; height: 45px; border-radius: 14px; display: flex; align-items: center; justify-content: center; font-size: 18px; flex-shrink: 0; }
    .type-class { background: rgba(16, 185, 129, 0.1); color: #10b981; }
    .type-exam { background: rgba(239, 68, 68, 0.1); color: #ef4444; }
    .type-notice { background: rgba(59, 130, 246, 0.1); color: #3b82f6; }
    .type-general { background: #f1f5f9; color: #64748b; }

    .notif-body { flex-grow: 1; }
    .notif-title { font-weight: 700; color: var(--p-dark); margin-bottom: 3px; font-size: 15px; }
    .notif-msg { color: #64748b; font-size: 13.5px; line-height: 1.4; margin-bottom: 5px; }
    .notif-time { font-size: 11px; font-weight: 600; color: #94a3b8; text-transform: uppercase; }

    /* Action Buttons */
    .action-btn { width: 32px; height: 32px; border-radius: 8px; border: 1px solid #f1f5f9; background: #fff; display: flex; align-items: center; justify-content: center; color: #cbd5e1; transition: 0.2s; text-decoration: none; }
    .action-btn:hover { background: #fef2f2; color: #ef4444; border-color: #fecaca; }

    .fade-up { animation: fadeInUp 0.6s forwards; }
    @keyframes fadeInUp { from { opacity: 0; transform: translateY(15px); } to { opacity: 1; transform: translateY(0); } }
</style>

<div class="main-content">
    
    <div class="notif-header fade-up">
        <div>
            <h4 class="fw-800 text-dark mb-0">Notifications</h4>
            <p class="text-muted small mb-0 fw-bold">Recent activity and alerts for your account</p>
        </div>
        <a href="?action=mark_all_read" class="btn btn-dark btn-sm rounded-pill px-3 fw-bold shadow-sm">
            <i class="fas fa-check-double me-1"></i> Mark All as Read
        </a>
    </div>

    <div class="notif-container" style="max-width: 850px;">
        <?php if(empty($notifications)): ?>
            <div class="text-center py-5 bg-white rounded-4 border fade-up">
                <div class="mb-3 text-muted opacity-25"><i class="fas fa-bell-slash fa-4x"></i></div>
                <h5 class="fw-bold text-dark">Inbox is Empty</h5>
                <p class="small text-muted">You have no new notifications right now.</p>
                <a href="user_dashboard.php" class="btn btn-outline-success btn-sm rounded-pill px-4 fw-bold">Return Home</a>
            </div>
        <?php else: foreach($notifications as $n): 
            // টাইপ অনুযায়ী আইকন সেট
            $icon = "fa-bell"; $type_class = "type-general";
            if($n['type'] == 'class') { $icon = "fa-video"; $type_class = "type-class"; }
            elseif($n['type'] == 'exam') { $icon = "fa-file-signature"; $type_class = "type-exam"; }
            elseif($n['type'] == 'notice') { $icon = "fa-bullhorn"; $type_class = "type-notice"; }
        ?>
            <div class="notif-card <?= $n['is_read'] == 0 ? 'unread' : '' ?> fade-up shadow-sm">
                <div class="notif-icon <?= $type_class ?>">
                    <i class="fas <?= $icon ?>"></i>
                </div>
                
                <div class="notif-body">
                    <div class="d-flex justify-content-between align-items-start">
                        <div class="notif-title"><?= htmlspecialchars($n['title']) ?></div>
                        <?php if($n['is_read'] == 0): ?>
                            <span class="badge bg-warning text-dark rounded-pill" style="font-size: 8px;">NEW</span>
                        <?php endif; ?>
                    </div>
                    <p class="notif-msg"><?= htmlspecialchars($n['message']) ?></p>
                    <div class="notif-time">
                        <i class="far fa-clock me-1"></i> <?= date('d M, Y - h:i A', strtotime($n['created_at'])) ?>
                    </div>
                </div>

                <div class="notif-actions d-flex flex-column gap-2">
                    <?php if($n['link'] != '#'): ?>
                        <a href="<?= $n['link'] ?>" class="action-btn" title="Open Link" style="color: var(--p-green);"><i class="fas fa-external-link-alt"></i></a>
                    <?php endif; ?>
                    <a href="?delete_id=<?= $n['id'] ?>" class="action-btn" onclick="return confirm('Delete permanently?')" title="Delete"><i class="fas fa-trash-alt"></i></a>
                </div>
            </div>
        <?php endforeach; endif; ?>
    </div>

</div>

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