<?php

// Récupérer le collector de notifications pour ce projet individuel
$notificationCollector = $kirby->option('adrienpayet.pdc-notifications.collector');
$notifications = [];

if ($notificationCollector && $kirby->user()) {
    try {
        $notifications = $notificationCollector->collect($page, $kirby->user());
    } catch (\Throwable $e) {
        error_log("Error collecting notifications for project {$page->uri()}: " . $e->getMessage());
        $notifications = [];
    }
}

$project = [
    'title'    => $page->title()->value(),
    'url'      => $page->url(),
    'modified' => $page->modified('Y-MM-d'),
    'status'   => $page->status(),
    'logo'     => $page->client()->isNotEmpty() && $page->client()->toPage()
        ? $page->client()->toPage()->logo()->toFile()->url()
        : null,
    'steps'                  => $page->getSteps(),
    'designToLight'          => $page->isDTLEnabled()->isTrue() ? processDTLProposals($page) : null,
    'hasOptimizationRequest' => $page->hasOptimizationRequest()->isTrue(),
    'notifications'          => $notifications,
];

$pageData = array_merge($genericData, $project);

echo json_encode([
    'page' => $pageData,
    'user' => $userData,
]);
