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

use CpCreation\VitiCore\Commerce\Catalog\Model\CatalogChange;
use CpCreation\VitiCore\Commerce\Catalog\Model\CatalogChangeType;
use CpCreation\VitiCore\Commerce\Catalog\Model\CatalogDeliveryStatus;
use CpCreation\VitiCore\Commerce\Catalog\Model\CatalogResourceType;
use PHPUnit\Framework\TestCase;
use Ramsey\Uuid\Uuid;

final class CatalogChangeTest extends TestCase
{
    public function testRetryableDeliveryRemainsQueuedThenCanSucceed(): void
    {
        $change = $this->change();
        $queuedAt = new \DateTimeImmutable('2026-07-22T10:00:00+00:00');
        $failedAt = new \DateTimeImmutable('2026-07-22T10:00:10+00:00');
        $deliveredAt = new \DateTimeImmutable('2026-07-22T10:00:20+00:00');

        $change->markQueued($queuedAt);
        $change->markRetryableFailure($failedAt, 'network');

        self::assertSame(CatalogDeliveryStatus::QUEUED, $change->getDeliveryStatus());
        self::assertSame(1, $change->getDeliveryAttempts());
        self::assertSame('network', $change->getLastError());

        $change->markDelivered($deliveredAt);

        self::assertSame(CatalogDeliveryStatus::DELIVERED, $change->getDeliveryStatus());
        self::assertSame(2, $change->getDeliveryAttempts());
        self::assertSame($deliveredAt, $change->getDeliveredAt());
        self::assertNull($change->getLastError());
    }

    public function testPermanentFailureCannotBeOverwrittenByALateSuccess(): void
    {
        $change = $this->change();
        $failedAt = new \DateTimeImmutable('2026-07-22T10:00:00+00:00');

        $change->markPermanentFailure($failedAt, 'HTTP 401');
        $change->markDelivered($failedAt->modify('+1 second'));

        self::assertTrue($change->isDeliveryComplete());
        self::assertSame(CatalogDeliveryStatus::FAILED_PERMANENT, $change->getDeliveryStatus());
        self::assertSame(1, $change->getDeliveryAttempts());
        self::assertNull($change->getDeliveredAt());
    }

    public function testExhaustedRetriesCloseTheQueuedDeliveryWithoutAddingAnAttempt(): void
    {
        $change = $this->change();
        $failedAt = new \DateTimeImmutable('2026-07-22T10:00:00+00:00');

        $change->markQueued($failedAt->modify('-1 minute'));
        $change->markRetryableFailure($failedAt, 'network');
        $change->markRetriesExhausted($failedAt, 'retries exhausted');

        self::assertSame(CatalogDeliveryStatus::FAILED_PERMANENT, $change->getDeliveryStatus());
        self::assertSame(1, $change->getDeliveryAttempts());
        self::assertSame($failedAt, $change->getPermanentlyFailedAt());
        self::assertSame('retries exhausted', $change->getLastError());
    }

    private function change(): CatalogChange
    {
        return new CatalogChange(
            instanceId: '7ad9e145-910f-4de4-9cec-5f832494adef',
            type: CatalogChangeType::PRODUCT_CHANGED,
            resourceType: CatalogResourceType::PRODUCT,
            resourceId: 'vc_7ad9e145910f4de49cec5f832494adef_product_de239cbda2bd4431a1d632b73f9c35d1',
            eventId: Uuid::fromString('018f6f62-5279-7ae0-a17f-b0fdd21b6880'),
        );
    }
}
