<?php
 
 
namespace App\Entity;
 
 
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
 
use ApiPlatform\Metadata\ApiFilter;
 
use ApiPlatform\Metadata\ApiResource;
 
use App\Controller\Action\EvaluationController;
 
use App\Controller\Action\EvaluationDuplicatePutAction;
 
use App\Controller\Action\EvaluationHistoryActionController;
 
use App\Controller\Action\EvaluationPutAction;
 
use App\Controller\Action\EvaluationReportController;
 
use App\Controller\Action\StatisticsEvaluationsReportController;
 
use App\Repository\EvaluationRepository;
 
use App\State\EvaluationPutProcessor;
 
use Doctrine\Common\Collections\ArrayCollection;
 
use Doctrine\Common\Collections\Collection;
 
use Doctrine\ORM\Mapping as ORM;
 
use Symfony\Component\Serializer\Annotation\Groups;
 
use Symfony\Component\Serializer\Annotation\SerializedName;
 
use ApiPlatform\Metadata\Delete;
 
use ApiPlatform\Metadata\Get;
 
use ApiPlatform\Metadata\GetCollection;
 
use ApiPlatform\Metadata\Post;
 
use ApiPlatform\Metadata\Put;
 
 
#[ORM\Table(name: 'evaluation')]
 
#[ORM\Index(name: 'compliance_id', columns: ['compliance_id'])]
 
#[ORM\Index(name: 'site_id', columns: ['site_id'])]
 
#[ORM\Index(name: 'user_id', columns: ['user_id'])]
 
#[ORM\Entity(repositoryClass: EvaluationRepository::class)]
 
#[ApiResource(
 
    operations: [
 
    new GetCollection(),
 
    new GetCollection(
 
        name: '_api_/evaluations/{id}/histories',
 
        uriTemplate: '/evaluations/{id}/histories',
 
        controller: EvaluationHistoryActionController::class,
 
        write: false,
 
        read: false
 
    ),
 
    new Post(
 
        name: '_api_/evaluations_report',
 
        uriTemplate: '/evaluations_report',
 
        controller: EvaluationController::class,
 
        write: false,
 
        read: false
 
    ),
 
    new Post(
 
        uriTemplate: '/evaluations_report_optimized',
 
        controller: EvaluationReportController::class,
 
        read: false,
 
        write: false,
 
        name: '_api_/evaluations_report_optimized'
 
    ),
 
    new Post(
 
        security: 'user.isMasterAdmin()'
 
    ),
 
    new Get(),
 
    new Put(
 
        controller: EvaluationPutAction::class,
 
    ),
 
    new Put(
 
        name: '_api_/evaluation_duplicate',
 
        uriTemplate: '/evaluation_duplicate',
 
        controller: EvaluationDuplicatePutAction::class,
 
        write: false,
 
        read: false
 
    ),
 
    new Delete(),
 
    new Post(
 
        uriTemplate: '/report_evaluations_statistics',
 
        controller: StatisticsEvaluationsReportController::class,
 
        read: false,
 
        write: false,
 
        name: '_api_/report_evaluations_statistics'
 
    ),
 
],
 
    normalizationContext: ['skip_null_values' => false, 'datetime_format' => 'Y-m-d', 'groups' => ['compliance.get', 'evaluation.get']],
 
    denormalizationContext: ['groups' => ['evaluation.write']]
 
)]
 
 
#[ApiFilter(
 
    SearchFilter::class,
 
    properties: [
 
        'site' => 'exact',
 
        'compliance.document' => 'exact',
 
        'compliance.section' => 'exact',
 
        'document' => 'exact',
 
    ]
 
)]
 
class Evaluation
 
{
 
    #[ORM\Column(name: 'id', type: 'bigint', nullable: false)]
 
    #[ORM\Id]
 
    #[ORM\GeneratedValue(strategy: 'IDENTITY')]
 
    #[Groups(['compliance.get','plan.get'])]
 
    private int $id;
 
 
    #[ORM\Column(name: 'old_id', type: 'string', length: 255, nullable: true)]
 
    private ?string $oldId = null;
 
 
    #[ORM\JoinColumn(name: 'compliance_id', referencedColumnName: 'id')]
 
    #[ORM\ManyToOne(targetEntity: 'Compliance', inversedBy: 'evaluations')]
 
    #[Groups(['evaluation.get','plan.get'])]
 
    private ?\App\Entity\Compliance $compliance = null;
 
 
    #[ORM\JoinColumn(name: 'site_id', referencedColumnName: 'id')]
 
    #[ORM\ManyToOne(targetEntity: 'Site')]
 
    #[Groups(['compliance.get', 'evaluation.get'])]
 
    private ?\App\Entity\Site $site = null;
 
 
    #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id')]
 
    #[ORM\ManyToOne(targetEntity: 'User')]
 
    #[Groups(['compliance.get', 'evaluation.get','evaluation.write'])]
 
    private ?\App\Entity\User $user = null;
 
 
    #[ORM\Column(name: 'annotation_nvs', type: 'string', length: 16777215, nullable: true)]
 
    #[Groups(['compliance.get', 'evaluation.get', 'evaluation.write'])]
 
    private ?string $annotationNvs;
 
 
    #[ORM\Column(name: 'annotation', type: 'string', length: 16777215, nullable: true)]
 
    #[Groups(['compliance.get', 'evaluation.get', 'evaluation.write','plan.get'])]
 
    private ?string $annotation;
 
 
    #[ORM\Column(name: 'comment', type: 'string', length: 16777215, nullable: true)]
 
    #[Groups(['compliance.get', 'evaluation.get', 'evaluation.write','plan.get'])]
 
    private ?string $comment;
 
 
    #[ORM\ManyToOne(inversedBy: 'evaluations')]
 
    #[ORM\JoinColumn(nullable: false)]
 
    private ?Section $section = null;
 
 
    #[ORM\ManyToOne(inversedBy: 'evaluations')]
 
    #[ORM\OrderBy(['updatedAt' => 'DESC', 'datedAt' => 'DESC'])]
 
    #[ORM\JoinColumn(nullable: false)]
 
    #[Groups(['compliance.get', 'evaluation.get'])]
 
    private ?Document $document = null;
 
 
    #[ORM\Column(nullable: true)]
 
    #[Groups(['compliance.get', 'evaluation.get', 'evaluation.write','plan.get'])]
 
    private ?\DateTimeImmutable $evaluationDate = null;
 
 
    #[ORM\ManyToOne(inversedBy: 'evaluations', targetEntity: 'EvaluationStatus')]
 
    #[ORM\JoinColumn(nullable: true)]
 
    #[Groups(['compliance.get', 'evaluation.get', 'evaluation.write', 'compliance:evaluation:get','plan.get'])]
 
    private $status = null;
 
 
    #[ORM\ManyToMany(targetEntity: ActionPlan::class, inversedBy: 'evaluation', orphanRemoval: false, cascade: ['persist'])]
 
    private Collection $actionPlans;
 
 
    #[ORM\ManyToOne(inversedBy: 'evaluation_file')]
 
    #[Groups(['evaluation.get', 'evaluation.write'])]
 
    private ?Media $evaluation_file = null;
 
 
    public function __construct()
 
    {
 
        $this->actionPlans = new ArrayCollection();
 
    }
 
 
    public function getId(): ?int
 
    {
 
        return $this->id;
 
    }
 
 
    public function getOldId(): ?string
 
    {
 
        return $this->oldId;
 
    }
 
 
    public function setOldId(?string $oldId): self
 
    {
 
        $this->oldId = $oldId;
 
 
        return $this;
 
    }
 
 
    public function getCompliance(): ?Compliance
 
    {
 
        return $this->compliance;
 
    }
 
 
    public function setCompliance(?Compliance $compliance): self
 
    {
 
        $this->compliance = $compliance;
 
 
        return $this;
 
    }
 
 
    public function getSite(): ?Site
 
    {
 
        return $this->site;
 
    }
 
 
    public function setSite(?Site $site): self
 
    {
 
        $this->site = $site;
 
 
        return $this;
 
    }
 
 
    public function getUser(): ?User
 
    {
 
        return $this->user;
 
    }
 
 
    public function setUser(?User $user): self
 
    {
 
        $this->user = $user;
 
 
        return $this;
 
    }
 
 
    public function getAnnotationNvs()
 
    {
 
        return $this->annotationNvs;
 
    }
 
 
 
    public function getAnnotation()
 
    {
 
        return $this->annotation;
 
    }
 
 
    public function getComment()
 
    {
 
        return $this->comment;
 
    }
 
 
    public function setAnnotationNvs($annotationNvs)
 
    {
 
        $this->annotationNvs = $annotationNvs;
 
 
        return $this;
 
    }
 
 
    public function setAnnotation($annotation)
 
    {
 
        $this->annotation = $annotation;
 
 
        return $this;
 
    }
 
 
    public function setComment($comment)
 
    {
 
        $this->comment = $comment;
 
 
        return $this;
 
    }
 
 
    public function getSection(): ?Section
 
    {
 
        return $this->section;
 
    }
 
 
    public function setSection(?Section $section): self
 
    {
 
        $this->section = $section;
 
 
        return $this;
 
    }
 
 
    public function getDocument(): ?Document
 
    {
 
        return $this->document;
 
    }
 
 
    public function setDocument(?Document $document): self
 
    {
 
        $this->document = $document;
 
 
        return $this;
 
    }
 
 
 
    public function getEvaluationDate(): ?\DateTimeImmutable
 
    {
 
        return $this->evaluationDate;
 
    }
 
 
    public function setEvaluationDate(\DateTimeImmutable $evaluationDate): self
 
    {
 
        $this->evaluationDate = $evaluationDate;
 
 
        return $this;
 
    }
 
 
    public function getStatus(): ?EvaluationStatus
 
    {
 
        return $this->status;
 
    }
 
 
    public function setStatus(?EvaluationStatus $status): self
 
    {
 
        $this->status = $status;
 
 
        return $this;
 
    }
 
 
    /**
 
     * @return Collection<int, ActionPlan>
 
     */
 
    public function getActionPlans(): Collection
 
    {
 
        return $this->actionPlans;
 
    }
 
 
    public function addActionPlans(ActionPlan $actionPlan): self
 
    {
 
            $this->actionPlans->add($actionPlan);
 
            $actionPlan->addEvaluation($this);
 
 
        return $this;
 
    }
 
 
 
    public function removeActionPlan(ActionPlan $actionPlan): self
 
    {
 
        $this->actionPlans->removeElement($actionPlan);
 
 
        return $this;
 
    }
 
//
 
//    #[Groups(['compliance.get', 'evaluation.get'])]
 
//    public function getLiveStatus()
 
//    {
 
//        $lastUpdateEvaluation = $this->getEvaluationDate()? (int) (new \DateTimeImmutable('now'))->diff($this->getEvaluationDate())->format('%a') : 0;
 
//        $year = $this->site->getCustomer()?->getYear() ? $this->site->getCustomer()?->getYear() : 3;
 
//        $nbDays = $year ? $year*360 : 0;
 
//        if(($nbDays && $lastUpdateEvaluation >= $nbDays)){
 
//            $status = DocumentStatus::WARNING_STATUS;
 
//        }
 
//        else{
 
//            $status = 39;
 
//        }
 
//        return  DocumentStatus::STATUSES[$status];
 
//    }
 
 
    public function getEvaluationFile(): ?Media
 
    {
 
        return $this->evaluation_file;
 
    }
 
 
    public function setEvaluationFile(?Media $evaluation_file): self
 
    {
 
        $this->evaluation_file = $evaluation_file;
 
 
        return $this;
 
    }
 
 
}