<?php

declare(strict_types=1);

namespace App\ApiResources\Dolibarr;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Get;
use App\Enum\Dolibarr\DolibarrDocTypeEnum;
use App\State\Provider\Dolibarr\DolibarrDocDownloadProvider;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Demande de téléchargement d'un fichier depuis Dolibarr (facture, bon de commande, etc).
 */
#[ApiResource(
    operations: [
        new Get(
            uriTemplate: '/dolibarr/download/{dolibarrDocType}/{ref}',
            requirements: [
                'ref' => '[\w-]+',
            ],
            security: '(is_granted("ROLE_ADMIN_CORE") or 
                            is_granted("ROLE_ADMINISTRATIF_MANAGER_CORE") or 
                            is_granted("ROLE_PEDAGOGICS_MANAGER_CORE") or 
                            is_granted("ROLE_FINANCIAL_MANAGER_CORE"))',
            provider: DolibarrDocDownloadProvider::class
        ),
    ]
)]
class DolibarrDocDownload
{
    /**
     * Id 'bidon' ajouté par défaut pour permettre la construction
     * de l'IRI par api platform.
     */
    #[ApiProperty(identifier: true)]
    protected int $id = 0;

    /**
     * Type de fichier à télécharger.
     */
    #[Assert\Type(type: DolibarrDocTypeEnum::class)]
    protected DolibarrDocTypeEnum $dolibarrDocType;

    /**
     * The dolibarr reference of the document (ex: CO2502-0380, FA2101-02988, ...)
     * Must be URL Encoded.
     */
    protected string $ref;

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

    public function setId(int $id): self
    {
        $this->id = $id;

        return $this;
    }

    public function getDolibarrDocType(): DolibarrDocTypeEnum
    {
        return $this->dolibarrDocType;
    }

    public function setDolibarrDocType(DolibarrDocTypeEnum $dolibarrDocType): self
    {
        $this->dolibarrDocType = $dolibarrDocType;

        return $this;
    }

    public function getRef(): string
    {
        return $this->ref;
    }

    public function setRef(string $ref): self
    {
        $this->ref = $ref;

        return $this;
    }
}
