<?php

declare(strict_types=1);

use App\Framework\MagicLinks\ValueObjects\Metadata;

describe('Metadata', function () {
    it('creates empty metadata', function () {
        $metadata = Metadata::empty();

        expect($metadata->isEmpty())->toBeTrue();
        expect($metadata->toArray())->toBe([]);
    });

    it('creates from array', function () {
        $metadata = Metadata::fromArray(['source' => 'email', 'campaign' => 'welcome']);

        expect($metadata->isEmpty())->toBeFalse();
        expect($metadata->toArray())->toBe(['source' => 'email', 'campaign' => 'welcome']);
    });

    it('adds values immutably', function () {
        $metadata = Metadata::empty();
        $updated = $metadata->with('source', 'email');

        expect($metadata->has('source'))->toBeFalse();
        expect($updated->has('source'))->toBeTrue();
        expect($updated->get('source'))->toBe('email');
    });

    it('removes values immutably', function () {
        $metadata = Metadata::fromArray(['source' => 'email', 'campaign' => 'welcome']);
        $updated = $metadata->without('campaign');

        expect($metadata->has('campaign'))->toBeTrue();
        expect($updated->has('campaign'))->toBeFalse();
        expect($updated->has('source'))->toBeTrue();
    });

    it('merges metadata', function () {
        $meta1 = Metadata::fromArray(['source' => 'email']);
        $meta2 = Metadata::fromArray(['campaign' => 'welcome']);
        $merged = $meta1->merge($meta2);

        expect($merged->toArray())->toBe(['source' => 'email', 'campaign' => 'welcome']);
    });

    it('returns keys', function () {
        $metadata = Metadata::fromArray(['source' => 'email', 'campaign' => 'welcome']);

        expect($metadata->keys())->toBe(['source', 'campaign']);
    });
});
