<?php

declare(strict_types=1);

namespace App\Module\Integration\Infrastructure\ApiPlatform\Resource;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Post;
use App\Module\Integration\Domain\Entity\TaskBookStackLink;
use App\Module\Integration\Infrastructure\ApiPlatform\State\BookStackLinkProcessor;
use App\Module\Integration\Infrastructure\ApiPlatform\State\BookStackLinkProvider;
use App\Module\ProjectManagement\Domain\Entity\Task;
use Symfony\Component\Serializer\Attribute\Groups;

#[ApiResource(
    operations: [
        new GetCollection(
            uriTemplate: '/tasks/{taskId}/bookstack/links',
            uriVariables: [
                'taskId' => new Link(fromClass: Task::class, identifiers: ['id']),
            ],
            normalizationContext: ['groups' => ['bookstack_link:read']],
            provider: BookStackLinkProvider::class,
            security: "is_granted('ROLE_USER')",
        ),
        new Post(
            uriTemplate: '/tasks/{taskId}/bookstack/links',
            uriVariables: [
                'taskId' => new Link(fromClass: Task::class, identifiers: ['id']),
            ],
            denormalizationContext: ['groups' => ['bookstack_link:write']],
            normalizationContext: ['groups' => ['bookstack_link:read']],
            provider: BookStackLinkProvider::class,
            processor: BookStackLinkProcessor::class,
            security: "is_granted('ROLE_USER')",
        ),
        new Delete(
            uriTemplate: '/tasks/{taskId}/bookstack/links/{id}',
            uriVariables: [
                'taskId' => new Link(fromClass: Task::class, identifiers: ['id']),
                'id'     => new Link(fromClass: TaskBookStackLink::class, identifiers: ['id']),
            ],
            provider: BookStackLinkProvider::class,
            processor: BookStackLinkProcessor::class,
            security: "is_granted('ROLE_USER')",
        ),
    ],
)]
final class BookStackLink
{
    #[Groups(['bookstack_link:read'])]
    public ?int $id = null;

    #[Groups(['bookstack_link:read', 'bookstack_link:write'])]
    public int $bookstackId = 0;

    #[Groups(['bookstack_link:read', 'bookstack_link:write'])]
    public string $bookstackType = '';

    #[Groups(['bookstack_link:read', 'bookstack_link:write'])]
    public string $title = '';

    #[Groups(['bookstack_link:read', 'bookstack_link:write'])]
    public string $url = '';

    #[Groups(['bookstack_link:read'])]
    public ?string $createdAt = null;
}
