<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use App\Repository\NotificationRepository;
use App\State\MarkAllNotificationsReadProcessor;
use App\State\NotificationHistoryProvider;
use App\State\NotificationTodayProvider;
use App\State\UnreadNotificationsProvider;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;

#[ApiResource(
    operations: [
        new Get(
            uriTemplate: '/notifications/{id}',
            uriVariables: ['id'],
            requirements: ['id' => '\d+'],
            normalizationContext: ['groups' => ['notification:read']],
            security: "is_granted('ROLE_USER')"
        ),
        new GetCollection(
            uriTemplate: '/notifications/unread',
            normalizationContext: ['groups' => ['notification:read']],
            security: "is_granted('ROLE_USER')",
            provider: UnreadNotificationsProvider::class,
            paginationEnabled: false
        ),
        new GetCollection(
            uriTemplate: '/notifications/today',
            normalizationContext: ['groups' => ['notification:read']],
            security: "is_granted('ROLE_USER')",
            provider: NotificationTodayProvider::class,
            paginationEnabled: false
        ),
        new GetCollection(
            uriTemplate: '/notifications/history',
            normalizationContext: ['groups' => ['notification:read']],
            security: "is_granted('ROLE_USER')",
            provider: NotificationHistoryProvider::class,
            paginationEnabled: false
        ),
        new Post(
            uriTemplate: '/notifications/mark-all-read',
            security: "is_granted('ROLE_USER')",
            input: false,
            output: false,
            read: false,
            processor: MarkAllNotificationsReadProcessor::class
        ),
    ]
)]
#[ORM\Entity(repositoryClass: NotificationRepository::class)]
#[ORM\Table(name: 'notifications')]
#[ORM\Index(columns: ['recipient_id', 'is_read', 'created_at'], name: 'idx_notifications_recipient_read_created')]
class Notification
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    #[Groups(['notification:read'])]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: User::class)]
    #[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
    private ?User $recipient = null;

    #[ORM\ManyToOne(targetEntity: User::class)]
    #[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
    private ?User $actor = null;

    #[ORM\Column(type: 'text')]
    #[Groups(['notification:read'])]
    private string $message = '';

    #[ORM\Column(type: 'string', length: 60, options: ['default' => ''])]
    #[Groups(['notification:read'])]
    private string $category = '';

    #[ORM\Column(type: 'string', length: 255, options: ['default' => ''])]
    #[Groups(['notification:read'])]
    private string $target = '';

    #[ORM\Column(type: 'boolean', options: ['default' => false])]
    #[Groups(['notification:read'])]
    private bool $isRead = false;

    #[ORM\Column(type: 'datetime_immutable')]
    #[Groups(['notification:read'])]
    private DateTimeImmutable $createdAt;

    public function __construct()
    {
        $this->createdAt = new DateTimeImmutable();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getRecipient(): ?User
    {
        return $this->recipient;
    }

    public function setRecipient(?User $recipient): self
    {
        $this->recipient = $recipient;

        return $this;
    }

    public function getActor(): ?User
    {
        return $this->actor;
    }

    public function setActor(?User $actor): self
    {
        $this->actor = $actor;

        return $this;
    }

    #[Groups(['notification:read'])]
    public function getActorName(): string
    {
        return $this->actor?->getUsername() ?? '';
    }

    public function getMessage(): string
    {
        return $this->message;
    }

    public function setMessage(string $message): self
    {
        $this->message = $message;

        return $this;
    }

    public function getCategory(): string
    {
        return $this->category;
    }

    public function setCategory(string $category): self
    {
        $this->category = $category;

        return $this;
    }

    public function getTarget(): string
    {
        return $this->target;
    }

    public function setTarget(string $target): self
    {
        $this->target = $target;

        return $this;
    }

    public function isRead(): bool
    {
        return $this->isRead;
    }

    public function getIsRead(): bool
    {
        return $this->isRead;
    }

    public function setIsRead(bool $isRead): self
    {
        $this->isRead = $isRead;

        return $this;
    }

    public function getCreatedAt(): DateTimeImmutable
    {
        return $this->createdAt;
    }
}
