<?php

/**
 * Route pour marquer une notification comme lue.
 * Délègue au bon provider selon le type de notification.
 */
return [
    'pattern' => '(:all)mark-notification-read.json',
    'method' => 'POST',
    'action' => function () {
        $json = file_get_contents('php://input');
        $data = json_decode($json);

        if (!$data || !isset($data->type) || !isset($data->id)) {
            return json_encode([
                'status' => 'error',
                'message' => 'Missing required fields: type, id'
            ]);
        }

        try {
            $collector = kirby()->option('adrienpayet.pdc-notifications.collector');

            if (!$collector) {
                throw new Exception('NotificationCollector not initialized');
            }

            $success = $collector->markAsRead(
                $data->type,
                $data->id,
                (array) $data,
                kirby()->user()
            );

            return json_encode([
                'status' => $success ? 'success' : 'error',
                'message' => $success ? 'Notification marked as read' : 'Failed to mark notification as read'
            ]);
        } catch (\Throwable $th) {
            return json_encode([
                'status' => 'error',
                'message' => $th->getMessage()
            ]);
        }
    }
];
