<?php

declare(strict_types=1);

namespace App\Tests\Unit\Shared\Infrastructure\Messenger;

use App\Shared\Domain\Tenant\TenantId;
use App\Shared\Infrastructure\Messenger\TenantMessageTenantIdResolver;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

#[CoversClass(TenantMessageTenantIdResolver::class)]
final class TenantMessageTenantIdResolverTest extends TestCase
{
    #[Test]
    public function itReadsTenantIdsFromStringProperties(): void
    {
        $resolver = new TenantMessageTenantIdResolver();
        $message = new readonly class('a1b2c3d4-e5f6-7890-abcd-ef1234567890') {
            public function __construct(
                public string $tenantId,
            ) {
            }
        };

        self::assertSame('a1b2c3d4-e5f6-7890-abcd-ef1234567890', $resolver->resolve($message));
    }

    #[Test]
    public function itReadsTenantIdsFromValueObjects(): void
    {
        $resolver = new TenantMessageTenantIdResolver();
        $message = new readonly class(TenantId::fromString('b2c3d4e5-f6a7-8901-bcde-f12345678901')) {
            public function __construct(
                public TenantId $tenantId,
            ) {
            }
        };

        self::assertSame('b2c3d4e5-f6a7-8901-bcde-f12345678901', $resolver->resolve($message));
    }

    #[Test]
    public function itReturnsNullWhenTheMessageIsNotTenantScoped(): void
    {
        $resolver = new TenantMessageTenantIdResolver();
        $message = new readonly class('') {
            public function __construct(
                public string $batchId,
            ) {
            }
        };

        self::assertNull($resolver->resolve($message));
    }
}
