<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use App\Repository\FormationRepository;
use App\State\FormationDeleteProcessor;
use App\State\FormationJustificatifDownloadProvider;
use App\State\FormationJustificatifUploadProcessor;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Groups;

#[ApiResource(
    operations: [
        new Get(
            security: "is_granted('ROLE_ADMIN')"
        ),
        new GetCollection(
            security: "is_granted('ROLE_ADMIN')"
        ),
        new Post(
            security: "is_granted('ROLE_ADMIN')"
        ),
        new Patch(
            security: "is_granted('ROLE_ADMIN')"
        ),
        new Delete(
            security: "is_granted('ROLE_ADMIN')",
            processor: FormationDeleteProcessor::class,
        ),
        new Post(
            uriTemplate: '/formations/{id}/justificatif',
            security: "is_granted('ROLE_ADMIN')",
            deserialize: false,
            processor: FormationJustificatifUploadProcessor::class,
        ),
        new Get(
            uriTemplate: '/formations/{id}/justificatif',
            security: "is_granted('ROLE_ADMIN')",
            provider: FormationJustificatifDownloadProvider::class,
        ),
    ],
    normalizationContext: [
        'groups'          => ['formation:read', 'employee:read'],
        'datetime_format' => 'Y-m-d',
    ],
    denormalizationContext: [
        'groups'          => ['formation:write'],
        'datetime_format' => 'Y-m-d',
    ],
    order: ['startDate' => 'DESC'],
    paginationEnabled: false,
)]
#[ApiFilter(DateFilter::class, properties: ['startDate', 'endDate'])]
#[ApiFilter(SearchFilter::class, properties: ['employee' => 'exact'])]
#[ORM\Entity(repositoryClass: FormationRepository::class)]
#[ORM\Table(name: 'formations')]
class Formation
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    #[Groups(['formation:read'])]
    private ?int $id = null;

    #[ORM\ManyToOne(targetEntity: Employee::class)]
    #[ORM\JoinColumn(nullable: false)]
    #[Groups(['formation:read', 'formation:write'])]
    private ?Employee $employee = null;

    #[ORM\Column(type: 'date_immutable')]
    #[Groups(['formation:read', 'formation:write'])]
    private ?DateTimeImmutable $startDate = null;

    #[ORM\Column(type: 'date_immutable')]
    #[Groups(['formation:read', 'formation:write'])]
    private ?DateTimeImmutable $endDate = null;

    #[ORM\Column(type: 'text', nullable: true)]
    #[Groups(['formation:read', 'formation:write'])]
    private ?string $comment = null;

    #[ORM\Column(type: 'string', length: 255, nullable: true)]
    #[Groups(['formation:read'])]
    private ?string $justificatifPath = null;

    #[ORM\Column(type: 'string', length: 255, nullable: true)]
    #[Groups(['formation:read'])]
    private ?string $justificatifName = null;

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

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

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

    public function getEmployee(): ?Employee
    {
        return $this->employee;
    }

    public function setEmployee(?Employee $employee): self
    {
        $this->employee = $employee;

        return $this;
    }

    public function getStartDate(): ?DateTimeImmutable
    {
        return $this->startDate;
    }

    public function setStartDate(?DateTimeImmutable $startDate): self
    {
        $this->startDate = $startDate;

        return $this;
    }

    public function getEndDate(): ?DateTimeImmutable
    {
        return $this->endDate;
    }

    public function setEndDate(?DateTimeImmutable $endDate): self
    {
        $this->endDate = $endDate;

        return $this;
    }

    public function getComment(): ?string
    {
        return $this->comment;
    }

    public function setComment(?string $comment): self
    {
        $this->comment = $comment;

        return $this;
    }

    public function getJustificatifPath(): ?string
    {
        return $this->justificatifPath;
    }

    public function setJustificatifPath(?string $justificatifPath): self
    {
        $this->justificatifPath = $justificatifPath;

        return $this;
    }

    public function getJustificatifName(): ?string
    {
        return $this->justificatifName;
    }

    public function setJustificatifName(?string $justificatifName): self
    {
        $this->justificatifName = $justificatifName;

        return $this;
    }

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