<?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\Get;
use ApiPlatform\Metadata\GetCollection;
use CpCreation\VitiCore\Commerce\Catalog\Api\State\CatalogProductProvider;

#[ApiResource(
    operations: [
        new GetCollection(
            uriTemplate: '/commerce/v1/products',
            itemUriTemplate: '/commerce/v1/products/{id}',
            stateless: true,
            paginationEnabled: false,
            security: 'is_granted("ROLE_COMMERCE_SYNC")',
            provider: CatalogProductProvider::class,
            name: 'viti_commerce_products_get_collection',
        ),
        new Get(
            uriTemplate: '/commerce/v1/products/{id}',
            requirements: ['id' => 'vc_[a-f0-9]{32}_product_[a-f0-9]{32}'],
            stateless: true,
            security: 'is_granted("ROLE_COMMERCE_SYNC")',
            provider: CatalogProductProvider::class,
            name: 'viti_commerce_product_get',
        ),
    ],
    formats: ['json' => ['application/json']],
    normalizationContext: ['skip_null_values' => false],
)]
final readonly class CatalogProductResource
{
    /**
     * @param array<string, CatalogProductLocalization> $localizations
     * @param list<CatalogMedia>                         $media
     * @param list<CatalogVariant>                       $variants
     */
    public function __construct(
        public string $schema_version,
        public string $snapshot_at,
        #[ApiProperty(identifier: true)]
        public string $id,
        public CatalogProductSource $source,
        #[ApiProperty(readable: true, writable: false)]
        public CatalogProductLifecycle $lifecycle,
        #[ApiProperty(readable: true, writable: false)]
        public CatalogPublishability $publishability,
        public CatalogProductStatus $status,
        public ?string $sku,
        public CatalogBrand $brand,
        public CatalogSeller $seller,
        public ?CatalogCategory $category,
        public CatalogCompliance $compliance,
        public array $localizations,
        public array $media,
        public array $variants,
        public CatalogProductReviews $reviews,
        public CatalogCommerceConstraints $commerce_constraints,
        public CatalogValidation $validation,
    ) {}
}
