<?php

namespace App\Dto\Input;

use ApiPlatform\Metadata\ApiProperty;
use App\Entity\View;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;

final class ViewInput
{
    #[Assert\NotBlank(message: 'validators.view.name.not_blank')]
    #[Groups(['view:write'])]
    #[ApiProperty(description: 'The name of the view', example: "View 1")]
    public ?string $name = null;

    #[Groups(['view:write'])]
    #[ApiProperty(description: 'The favourite status of the view', example: true)]
    public ?bool $favourite = null;

    #[Groups(['view:write'])]
    #[ApiProperty(description: 'The filters of the view', example: ["filter1" => "value1"])]
    public ?array $filters = null;

    public function __construct(?View $view = null)
    {
        if (!$view) {
            return;
        }

        $this->name = $view->getName();
        $this->favourite = $view->isFavourite();
        $this->filters= $view->getFilters();
    }

    public function createOrUpdateEntity(?View $view = null): View
    {
        if (!$view) {
            $view = new View();
        }

        $view->setName($this->name);
        $view->setFavourite($this->favourite);
        $view->setFilters($this->filters);

        return $view;
    }

}