<?php
declare(strict_types=1);

namespace App\Controllers;

use App\Core\Controller;
use App\Core\Response;
use App\Config\Database;

class NotificationController extends Controller
{
    public function index(): void
    {
        $tenantId = (int) $_REQUEST['tenant_id'];
        $db = Database::getInstance();
        $stmt = $db->prepare("SELECT * FROM notifications WHERE tenant_id = ? AND is_read = 0 ORDER BY created_at DESC LIMIT 20");
        $stmt->execute([$tenantId]);
        Response::success($stmt->fetchAll());
    }

    public function markRead(string $id): void
    {
        $tenantId = (int) $_REQUEST['tenant_id'];
        $db = Database::getInstance();
        $stmt = $db->prepare("UPDATE notifications SET is_read = 1 WHERE id = ? AND tenant_id = ?");
        $stmt->execute([$id, $tenantId]);
        Response::success([], 'Notification marked as read');
    }

    public function readAll(): void
    {
        $tenantId = (int) $_REQUEST['tenant_id'];
        $db = Database::getInstance();
        $stmt = $db->prepare("UPDATE notifications SET is_read = 1 WHERE tenant_id = ?");
        $stmt->execute([$tenantId]);
        Response::success([], 'All notifications marked as read');
    }
}
