<?php
require_once __DIR__ . '/../config/database.php';
session_start();
header('Content-Type: application/json');

if (!isset($_SESSION['user_id'])) {
    echo json_encode(['count' => 0, 'notifications' => []]);
    exit;
}

$pdo = getDB();
$notifications = [];

// Check low stock items
$stmt = $pdo->query("SELECT COUNT(*) FROM products WHERE current_stock <= low_stock_qty AND status = 1");
$low_stock_count = $stmt->fetchColumn();
if ($low_stock_count > 0) {
    $notifications[] = [
        'message' => "$low_stock_count product(s) are low in stock!",
        'link' => BASE_URL . 'reports/stock_report.php?type=low',
        'time' => 'Now'
    ];
}

// Check pending repairs
$stmt = $pdo->query("SELECT COUNT(*) FROM repair_jobs WHERE repair_status NOT IN ('Delivered','Cancelled','Cannot Repair')");
$pending = $stmt->fetchColumn();
if ($pending > 0) {
    $notifications[] = [
        'message' => "$pending pending repair job(s)",
        'link' => BASE_URL . 'repairs/repair_jobs.php',
        'time' => 'Now'
    ];
}

echo json_encode([
    'count' => count($notifications),
    'notifications' => $notifications
]);
