<?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\Outbox;

use CpCreation\VitiCore\Commerce\Catalog\Model\CatalogChange;
use CpCreation\VitiCore\Commerce\Catalog\Model\CatalogChangeType;
use CpCreation\VitiCore\Commerce\Catalog\Model\CatalogResourceType;

final readonly class CatalogChangeDraft
{
    public function __construct(
        public CatalogChangeType $type,
        public CatalogResourceType $resourceType,
        public ?string $resourceId,
    ) {}

    public function deduplicationKey(): string
    {
        if (CatalogResourceType::PRODUCT === $this->resourceType) {
            return $this->resourceType->value . ':' . $this->resourceId;
        }

        return $this->type->value . ':' . $this->resourceType->value . ':' . $this->resourceId;
    }

    public function isProductRemoval(): bool
    {
        return CatalogChangeType::PRODUCT_REMOVED === $this->type;
    }

    public function toCatalogChange(string $instanceId, \DateTimeImmutable $occurredAt): CatalogChange
    {
        return new CatalogChange(
            instanceId: $instanceId,
            type: $this->type,
            resourceType: $this->resourceType,
            resourceId: $this->resourceId,
            occurredAt: $occurredAt,
        );
    }
}
