<?php
declare (strict_types=1);

namespace App\ApiResources\Dolibarr;

use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\ApiProperty;
use App\ApiResources\ApiResourcesInterface;
use App\State\Provider\Dolibarr\DolibarrAccountProvider;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use JetBrains\PhpStorm\Pure;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * Données de l'organization retournées par l'API Dolibarr
 * (aussi nommé 'ThirdParty' ou 'Society' dans dolibarr)
 */
#[ApiResource(
    operations: [
        new Get(
            uriTemplate: '/dolibarr/account/{organizationId}',
            requirements: ['organizationId' => '\\d+'],
            normalizationContext: ['groups' => ['dolibarr_get']],
            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")
                           ) and object.getOrganizationId() == user.getOrganization().getId()',
            provider: DolibarrAccountProvider::class
        )
    ]
)]
class DolibarrAccount implements ApiResourcesInterface
{
    #[ApiProperty(identifier: true)]
    #[Groups('dolibarr_get')]
    private int $organizationId;

    /**
     * Dolibarr societies pk
     */
    #[Groups('dolibarr_get')]
    private ?int $socId = null;

    /**
     * Opentalent client ref
     */
    #[Groups('dolibarr_get')]
    private string $clientNumber = "";

    /**
     * Opentalent product owned
     */
    #[Groups('dolibarr_get')]
    private string $product = "";

    /**
     * Contract and services currently active
     */
    #[Groups('dolibarr_get')]
    private ?DolibarrContract $contract = null;

    /**
     * Last bills
     */
    #[Groups('dolibarr_get')]
    private Collection $bills;

    #[Pure]
    public function __construct()
    {
        $this->bills = new ArrayCollection();
    }

    public function getOrganizationId(): int
    {
        return $this->organizationId;
    }

    public function setOrganizationId(int $organizationId): self
    {
        $this->organizationId = $organizationId;
        return $this;
    }

    public function getSocId(): ?int
    {
        return $this->socId;
    }

    public function setSocId(?int $socId): self
    {
        $this->socId = $socId;
        return $this;
    }

    public function getClientNumber(): string
    {
        return $this->clientNumber;
    }

    public function setClientNumber(string $clientNumber): self
    {
        $this->clientNumber = $clientNumber;
        return $this;
    }

    public function getProduct(): string
    {
        return $this->product;
    }

    public function setProduct(string $product): self
    {
        $this->product = $product;
        return $this;
    }

    public function getContract(): ?object
    {
        return $this->contract;
    }

    public function setContract(?object $contract): self
    {
        $this->contract = $contract;
        return $this;
    }

    public function getBills(): Collection
    {
        return $this->bills;
    }

    public function addBill(DolibarrBill $bill): self
    {
        $this->bills[] = $bill;
        return $this;
    }

    public function removeBill(DolibarrBill $bill): self
    {
        $this->bills->removeElement($bill);
        return $this;
    }
}
