<?php

declare(strict_types=1);

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

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Post;
use App\Module\Integration\Infrastructure\ApiPlatform\State\GiteaBranchProcessor;
use App\Module\Integration\Infrastructure\ApiPlatform\State\GiteaBranchProvider;
use Symfony\Component\Serializer\Attribute\Groups;

#[ApiResource(
    operations: [
        new GetCollection(
            uriTemplate: '/tasks/{taskId}/gitea/branches',
            normalizationContext: ['groups' => ['gitea_branch:read']],
            provider: GiteaBranchProvider::class,
            security: "is_granted('ROLE_USER')",
        ),
        new Post(
            uriTemplate: '/tasks/{taskId}/gitea/branches',
            denormalizationContext: ['groups' => ['gitea_branch:write']],
            normalizationContext: ['groups' => ['gitea_branch:read']],
            provider: GiteaBranchProvider::class,
            processor: GiteaBranchProcessor::class,
            security: "is_granted('ROLE_USER')",
        ),
    ],
)]
final class GiteaBranch
{
    #[Groups(['gitea_branch:read'])]
    public string $name = '';

    #[Groups(['gitea_branch:write'])]
    public string $type = 'feature';

    #[Groups(['gitea_branch:write'])]
    public string $baseBranch = 'main';

    /**
     * @var array<array{sha: string, message: string, author: string, date: string}>
     */
    #[Groups(['gitea_branch:read'])]
    public array $commits = [];
}
