<?php

declare(strict_types=1);

namespace CpCreation\VitiCore\Tests\Commerce\Catalog\Outbox;

use CpCreation\VitiCore\Cart\Model\Cart;
use CpCreation\VitiCore\Commerce\Catalog\CatalogIdentity;
use CpCreation\VitiCore\Commerce\Catalog\Configuration\CatalogIntegrationConfiguration;
use CpCreation\VitiCore\Commerce\Catalog\Model\CatalogChangeType;
use CpCreation\VitiCore\Commerce\Catalog\Model\CatalogResourceType;
use CpCreation\VitiCore\Commerce\Catalog\Outbox\CatalogChangeClassifier;
use CpCreation\VitiCore\Marketing\Model\ProductReview;
use CpCreation\VitiCore\Marketing\Model\ReviewInvitation;
use CpCreation\VitiCore\Marketing\Model\ReviewRequestConfiguration;
use CpCreation\VitiCore\Product\Model\Product;
use PHPUnit\Framework\TestCase;

final class ProductReviewCatalogChangeTest extends TestCase
{
    public function testAReviewChangeTargetsItsProduct(): void
    {
        $configuration = $this->catalogConfiguration();
        $product = new Product();
        $draft = new CatalogChangeClassifier(new CatalogIdentity($configuration))
            ->classify($this->review($product));

        self::assertNotNull($draft);
        self::assertSame(CatalogChangeType::PRODUCT_CHANGED, $draft->type);
        self::assertSame(CatalogResourceType::PRODUCT, $draft->resourceType);
        self::assertSame(
            new CatalogIdentity($configuration)->productId($product->getId()),
            $draft->resourceId,
        );
    }

    public function testChangingReviewActivationRequiresAFullReconciliation(): void
    {
        $configuration = $this->catalogConfiguration();
        $draft = new CatalogChangeClassifier(new CatalogIdentity($configuration))
            ->classify(new ReviewRequestConfiguration());

        self::assertNotNull($draft);
        self::assertSame(CatalogChangeType::RECONCILIATION_REQUIRED, $draft->type);
        self::assertSame(CatalogResourceType::CATALOG, $draft->resourceType);
        self::assertNull($draft->resourceId);
    }

    private function catalogConfiguration(): CatalogIntegrationConfiguration
    {
        return new CatalogIntegrationConfiguration(
            enabled: true,
            instanceId: '7ad9e145-910f-4de4-9cec-5f832494adef',
            apiTokenHash: '',
            currency: 'EUR',
        );
    }

    private function review(Product $product): ProductReview
    {
        $submittedAt = new \DateTimeImmutable('2026-07-23T10:00:00+02:00');
        $invitation = new ReviewInvitation(
            new Cart(),
            'client@example.test',
            hash('sha256', 'token'),
            $submittedAt->modify('-1 day'),
            $submittedAt->modify('+90 days'),
        );

        return new ProductReview(
            $invitation,
            $product,
            null,
            5,
            'Client vérifié',
            'Excellent',
            'Très belle découverte.',
            false,
            $submittedAt,
        );
    }
}
