<?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 "appointment-request".
 * Dérivé depuis les champs du projet quand hasOptimizationRequest est true.
 */
class AppointmentRequestProvider implements NotificationProvider
{
    public function getType(): string
    {
        return 'appointment-request';
    }

    public function collect(Page $project, User $user): array
    {
        // Pas de notification si pas de demande d'optimisation
        if ($project->hasOptimizationRequest()->isFalse()) {
            return [];
        }

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

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

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

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

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

        return [[
            'id' => 'appointment-request-' . (string) $project->uuid(),
            'type' => 'appointment-request',
            'text' => $project->optimizationRequestDetails()->value() ?? '',
            'author' => [
                'uuid' => $authorUuid,
                'name' => $project->optimizationAuthorName()->value() ?? '',
                'email' => $project->optimizationAuthorEmail()->value() ?? '',
                'role' => 'client',
            ],
            'date' => $project->optimizationDate()->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->optimizationReadby()->isNotEmpty()
            ? Yaml::decode($project->optimizationReadby()->value())
            : [];

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

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

        if (in_array($userUuid, $readby)) {
            return true;
        }

        $readby[] = $userUuid;

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

        return true;
    }
}
