<?php
/**
 * @author Daniel Knudsen <d.knudsen@spawntree.de>
 * @date 12.12.23
 */


namespace App\ApiResource;


use ApiPlatform\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Doctrine\Orm\State\Options;
use ApiPlatform\Metadata\ApiFilter;
use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use App\Entity\MediaObject;
use App\Entity\Partner;
use App\Enum\PartnerType;
use App\Filter\PartnerUnassignedFilter;
use App\State\EntityClassDtoStateProcessor;
use App\State\EntityToDtoStateProvider;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use Symfony\Component\Validator\Constraints\NotBlank;

#[ApiResource(
    shortName: 'Partner',
    operations: [
        new Get(
            security: 'is_granted("ROLE_USER")'
        ),
        new GetCollection(),
        new Post(
            security: 'is_granted("ROLE_ADMIN")',
        ),
        new Patch(
            security: 'is_granted("ROLE_ADMIN")',
        ),
//        new Delete(
//            security: 'is_granted("ROLE_ADMIN")',
//        )
    ],
    order: ['name' => 'ASC'],
    security: 'is_granted("ROLE_USER")',
    provider: EntityToDtoStateProvider::class,
    processor: EntityClassDtoStateProcessor::class,
    stateOptions: new Options(entityClass: Partner::class)
)]
#[ApiFilter(SearchFilter::class, properties: ['type' => 'exact', 'name' => 'ipartial'])]
#[ApiFilter(PartnerUnassignedFilter::class)]
#[ApiFilter(OrderFilter::class, properties: ['name', 'city', 'website'], arguments: ['orderParameterName' => 'order'])]
class PartnerApi
{
    #[ApiProperty(readable: false, writable: false, identifier: true)]
    public ?int $id = null;

    #[NotBlank]
    public string $name;

    public ?int $navisionId;

    public ?string $nameAddition = null;

    #[NotBlank]
    public PartnerType $partnerType;

    public ?string $description = null;

    public ?string $street = null;

    public ?string $addressAddition = null;

    public ?string $streetNo = null;

    public ?string $zip = null;

    public ?string $city = null;

    public ?string $country = null;

    public ?string $phone = null;

    public ?string $language = null;

    public ?string $currency = null;

    public ?string $paymentCondition = null;

    public ?string $deliveryCondition = null;

    public ?string $businessGroup = null;

    public ?string $vatBusinessGroup = null;

    public ?int $creditLimit = null;

    public ?string $routePoint = null;

    public ?string $website = null;

    public ?MediaObject $logo = null;

    #[ApiProperty(writable: false)]
    public ?string $logoUrl = null;

    #[ApiProperty(writable: false)]
    public ?UserApi $createdByIri = null;

    #[ApiProperty(writable: false)]
    public ?\DateTimeImmutable $createdAt = null;
}