<?php
session_start();
include_once('../includes/config.php');

if (strlen($_SESSION["aid"]) == 0) {
    echo json_encode(['success' => false, 'message' => 'Unauthorized']);
    exit;
}

// Get notifications
$notifications = [];

// New orders (last 24 hours)
$yesterday = date('Y-m-d H:i:s', strtotime('-24 hours'));
$newOrdersQuery = mysqli_query($con, "SELECT COUNT(*) as count FROM orders WHERE orderDate >= '$yesterday' AND (orderStatus IS NULL OR orderStatus = '')");
$newOrdersResult = mysqli_fetch_assoc($newOrdersQuery);
$newOrdersCount = $newOrdersResult['count'];

if ($newOrdersCount > 0) {
    $notifications[] = [
        'type' => 'order',
        'icon' => 'fa-shopping-cart',
        'color' => 'success',
        'title' => 'New Orders',
        'message' => "$newOrdersCount new order(s) received",
        'time' => 'Recent',
        'link' => 'new-order.php'
    ];
}

// Pending orders
$pendingQuery = mysqli_query($con, "SELECT COUNT(*) as count FROM orders WHERE orderStatus != 'Delivered' OR orderStatus IS NULL");
$pendingResult = mysqli_fetch_assoc($pendingQuery);
$pendingCount = $pendingResult['count'];

if ($pendingCount > 5) {
    $notifications[] = [
        'type' => 'warning',
        'icon' => 'fa-exclamation-triangle',
        'color' => 'warning',
        'title' => 'Pending Orders',
        'message' => "$pendingCount orders need processing",
        'time' => 'Now',
        'link' => 'pending-orders.php'
    ];
}

// Low stock products (if you have inventory)
// $lowStockQuery = mysqli_query($con, "SELECT COUNT(*) as count FROM products WHERE productAvailability = 'Out of Stock'");
// $lowStockResult = mysqli_fetch_assoc($lowStockQuery);
// $lowStockCount = $lowStockResult['count'];

// Today's revenue milestone
$todayRevQuery = mysqli_query($con, "SELECT SUM(totalAmount) as revenue FROM orders WHERE DATE(orderDate) = CURDATE()");
$todayRevResult = mysqli_fetch_assoc($todayRevQuery);
$todayRevenue = $todayRevResult['revenue'] ? $todayRevResult['revenue'] : 0;

if ($todayRevenue > 1000) {
    $notifications[] = [
        'type' => 'success',
        'icon' => 'fa-trophy',
        'color' => 'info',
        'title' => 'Revenue Milestone',
        'message' => 'Great job! Today\'s revenue: $' . number_format($todayRevenue, 2),
        'time' => 'Today',
        'link' => 'dashboard.php'
    ];
}

// New users (last 7 days)
$lastWeek = date('Y-m-d', strtotime('-7 days'));
$newUsersQuery = mysqli_query($con, "SELECT COUNT(*) as count FROM users WHERE regDate >= '$lastWeek'");
$newUsersResult = mysqli_fetch_assoc($newUsersQuery);
$newUsersCount = $newUsersResult['count'];

if ($newUsersCount > 0) {
    $notifications[] = [
        'type' => 'info',
        'icon' => 'fa-user-plus',
        'color' => 'primary',
        'title' => 'New Users',
        'message' => "$newUsersCount new user(s) registered this week",
        'time' => 'This week',
        'link' => 'manage-users.php'
    ];
}

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