<?php

declare(strict_types=1);

namespace App\Entity;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use App\Repository\ContractSuspensionRepository;
use App\State\ContractSuspensionWriteProcessor;
use DateTimeImmutable;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Attribute\Context;
use Symfony\Component\Serializer\Attribute\Groups;
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;

#[ApiResource(
    operations: [
        new GetCollection(),
        new Post(processor: ContractSuspensionWriteProcessor::class),
        new Patch(processor: ContractSuspensionWriteProcessor::class),
    ],
    normalizationContext: ['groups' => ['suspension:read']],
    denormalizationContext: ['groups' => ['suspension:write']],
    paginationEnabled: false,
    security: "is_granted('ROLE_ADMIN')",
)]
#[ORM\Entity(repositoryClass: ContractSuspensionRepository::class)]
#[ORM\Table(name: 'contract_suspensions')]
#[ORM\Index(columns: ['contract_period_id', 'start_date'], name: 'idx_suspension_period_start')]
class ContractSuspension
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    #[Groups(['suspension:read', 'employee:read'])]
    private ?int $id = null;

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

    #[Groups(['suspension:write'])]
    private ?int $contractPeriodId = null;

    #[ORM\Column(type: 'date_immutable')]
    #[Groups(['suspension:read', 'suspension:write', 'employee:read'])]
    #[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
    private DateTimeImmutable $startDate;

    #[ORM\Column(type: 'date_immutable', nullable: true)]
    #[Groups(['suspension:read', 'suspension:write', 'employee:read'])]
    #[Context([DateTimeNormalizer::FORMAT_KEY => 'Y-m-d'])]
    private ?DateTimeImmutable $endDate = null;

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

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

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

    public function getContractPeriodId(): ?int
    {
        return $this->contractPeriodId;
    }

    public function setContractPeriodId(?int $contractPeriodId): self
    {
        $this->contractPeriodId = $contractPeriodId;

        return $this;
    }

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

    public function getContractPeriod(): ?EmployeeContractPeriod
    {
        return $this->contractPeriod;
    }

    public function setContractPeriod(?EmployeeContractPeriod $contractPeriod): self
    {
        $this->contractPeriod = $contractPeriod;

        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 getCreatedAt(): DateTimeImmutable
    {
        return $this->createdAt;
    }
}
