<?php
declare(strict_types=1);

namespace App\Service;

use App\Entity\Access\Access;
use App\Entity\Core\File;
use App\Entity\Core\Notification;
use App\Enum\Core\NotificationTypeEnum;
use Doctrine\ORM\EntityManagerInterface;

class Notifier
{
    public function __construct(
        private EntityManagerInterface $em,
        private MercureHub $mercureHub
    ) {}

    protected function createNotification(
        Access $access,
        string $name,
        string $type,
        array $message,
        string $link = null
    ): Notification {
        $now = new \DateTime();

        $notification = new Notification();
        $notification->setName($name)
            ->setRecipientAccess($access)
            ->setRecipientOrganization($access->getOrganization())
            ->setType($type)
            ->setMessage($message)
            ->setLink($link)
            ->setCreateDate($now)
            ->setUpdateDate($now);
        return $notification;
    }

    public function notify(
        Access $access,
        string $name,
        string $type,
        array $message,
        string $link = null
    ): Notification
    {
        $notification = $this->createNotification($access, $name, $type, $message, $link);

        $this->em->persist($notification);
        $this->em->flush();

        $this->mercureHub->publishCreate($access->getId(), $notification);

        return $notification;
    }

    public function notifyExport(
        Access $access,
        File $file
    ): Notification
    {
        return $this->notify(
            $access,
            'export',
            NotificationTypeEnum::FILE()->getValue(),
            ['fileName' => $file->getName()],
            '/api/files/' . $file->getId() . '/download'
        );
    }

    public function notifyMessage(
        Access $access,
        array $message
    ): Notification
    {
        return $this->notify(
            $access,
            'message',
            NotificationTypeEnum::MESSAGE()->getValue(),
            $message
        );
    }

    public function notifySystem(
        Access $access,
        array $message
    ): Notification
    {
        return $this->notify(
            $access,
            'message',
            NotificationTypeEnum::SYSTEM()->getValue(),
            $message
        );
    }

    public function notifyError(
        Access $access,
        string $name,
        array $message
    ): Notification
    {
        return $this->notify(
            $access,
            $name,
            NotificationTypeEnum::ERROR()->getValue(),
            $message
        );
    }

}
