<?php

class Notification
{
    public function create($userId, $restaurantId, $title, $message, $type, $url = null)
    {
        $db = Database::getInstance();
        return $db->insert('notifications', [
            'user_id'       => $userId,
            'restaurant_id' => $restaurantId,
            'title'         => $title,
            'message'       => $message,
            'type'          => $type,
            'url'           => $url,
            'is_read'       => 0,
            'created_at'    => date('Y-m-d H:i:s'),
        ]);
    }

    public function getUnread($userId, $limit = 10)
    {
        $db = Database::getInstance();
        $stmt = $db->query(
            "SELECT * FROM notifications WHERE user_id = ? AND is_read = 0 ORDER BY created_at DESC LIMIT ?",
            [$userId, (int) $limit]
        );
        return $stmt->fetchAll();
    }

    public function getUnreadCount($userId)
    {
        $db = Database::getInstance();
        $result = $db->count('notifications', 'user_id = ? AND is_read = 0', [$userId]);
        return $result;
    }

    public function markAsRead($notificationId, $userId)
    {
        $db = Database::getInstance();
        return $db->update(
            'notifications',
            ['is_read' => 1],
            'id = ? AND user_id = ?',
            [$notificationId, $userId]
        );
    }

    public function markAllAsRead($userId)
    {
        $db = Database::getInstance();
        return $db->update(
            'notifications',
            ['is_read' => 1],
            'user_id = ? AND is_read = 0',
            [$userId]
        );
    }

    public function delete($notificationId)
    {
        $db = Database::getInstance();
        return $db->delete('notifications', 'id = ?', [$notificationId]);
    }
}
