<?php

namespace adrienpayet\notifications\providers;

use adrienpayet\notifications\NotificationProvider;
use Kirby\Cms\Page;
use Kirby\Cms\User;
use Kirby\Data\Yaml;

/**
 * Provider pour les notifications de type "project-request".
 * Dérivé depuis les champs du projet quand isClientRequest est true.
 */
class ProjectRequestProvider implements NotificationProvider
{
    public function getType(): string
    {
        return 'project-request';
    }

    public function collect(Page $project, User $user): array
    {
        // Pas de notification si ce n'est pas une demande client
        if ($project->isClientRequest()->isFalse()) {
            return [];
        }

        // Vérifier que les champs requis existent
        if ($project->requestAuthor()->isEmpty()) {
            return [];
        }

        $userUuid = (string) $user->uuid();
        $authorUuid = $project->requestAuthor()->value();

        // Ne pas notifier l'auteur de sa propre demande
        if ($authorUuid === $userUuid) {
            return [];
        }

        $readby = $project->requestReadby()->isNotEmpty()
            ? Yaml::decode($project->requestReadby()->value())
            : [];

        if (!is_array($readby)) {
            $readby = [];
        }

        return [[
            'id' => 'project-request-' . (string) $project->uuid(),
            'type' => 'project-request',
            'text' => $project->requestDetails()->value() ?? '',
            'author' => [
                'uuid' => $authorUuid,
                'name' => $project->requestAuthorName()->value() ?? '',
                'email' => $project->requestAuthorEmail()->value() ?? '',
                'role' => 'client',
            ],
            'date' => $project->requestDate()->value() ?? '',
            'location' => [
                'page' => [
                    'uri' => $project->uri(),
                    'title' => (string) $project->title(),
                    'template' => 'project',
                ],
                'project' => [
                    'uri' => $project->uri(),
                    'title' => (string) $project->title(),
                ]
            ],
            'readby' => $readby,
            'isRead' => in_array($userUuid, $readby),
            '_projectUri' => $project->uri(),
        ]];
    }

    public function markAsRead(string $id, array $location, User $user): bool
    {
        $projectUri = $location['_projectUri'] ?? null;
        if (!$projectUri) {
            return false;
        }

        $project = page($projectUri);
        if (!$project) {
            return false;
        }

        $readby = $project->requestReadby()->isNotEmpty()
            ? Yaml::decode($project->requestReadby()->value())
            : [];

        if (!is_array($readby)) {
            $readby = [];
        }

        $userUuid = (string) $user->uuid();

        if (in_array($userUuid, $readby)) {
            return true; // Déjà lu
        }

        $readby[] = $userUuid;

        $project->update([
            'requestReadby' => array_unique($readby)
        ]);

        return true;
    }
}
