<?php

declare(strict_types=1);

/*
 * This file is part of the VITICORE2 package.
 *
 * (c) CP Creation <web@cpcreation.fr>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CpCreation\VitiCore\Commerce\Catalog\Api\Resource;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\QueryParameter;
use CpCreation\VitiCore\Commerce\Catalog\Api\State\CatalogChangeProvider;

#[ApiResource(
    operations: [
        new GetCollection(
            uriTemplate: '/commerce/v1/changes',
            stateless: true,
            paginationEnabled: false,
            security: 'is_granted("ROLE_COMMERCE_SYNC")',
            provider: CatalogChangeProvider::class,
            parameters: [
                'after' => new QueryParameter(
                    schema: ['type' => 'integer', 'minimum' => 0],
                    description: 'Curseur exclusif. Relire une petite fenêtre et dédupliquer par event_id.',
                    castToNativeType: true,
                    default: 0,
                ),
                'limit' => new QueryParameter(
                    schema: ['type' => 'integer', 'minimum' => 1, 'maximum' => 250],
                    description: 'Nombre maximal de changements retournés.',
                    castToNativeType: true,
                    default: 100,
                ),
            ],
            strictQueryParameterValidation: true,
            name: 'viti_commerce_changes_get_collection',
        ),
    ],
    formats: ['json' => ['application/json']],
    normalizationContext: ['skip_null_values' => false],
)]
final readonly class CatalogChangeResource
{
    public function __construct(
        public string $schema_version,
        public int $cursor,
        #[ApiProperty(identifier: true)]
        public string $event_id,
        public string $instance_id,
        public string $type,
        public string $resource_type,
        public ?string $resource_id,
        public string $occurred_at,
    ) {}
}
