<?php

declare(strict_types=1);

use App\Framework\MagicLinks\ValueObjects\ExecutionContext;

describe('ExecutionContext', function () {
    it('creates empty context', function () {
        $context = ExecutionContext::empty();

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

    it('creates context from array', function () {
        $context = ExecutionContext::fromArray(['ip' => '127.0.0.1', 'user_agent' => 'Test']);

        expect($context->isEmpty())->toBeFalse();
        expect($context->toArray())->toBe(['ip' => '127.0.0.1', 'user_agent' => 'Test']);
    });

    it('adds values immutably', function () {
        $context = ExecutionContext::empty();
        $updated = $context->with('ip', '127.0.0.1');

        expect($context->has('ip'))->toBeFalse();
        expect($updated->has('ip'))->toBeTrue();
        expect($updated->get('ip'))->toBe('127.0.0.1');
    });

    it('retrieves values with defaults', function () {
        $context = ExecutionContext::fromArray(['ip' => '127.0.0.1']);

        expect($context->get('ip'))->toBe('127.0.0.1');
        expect($context->get('missing', 'default'))->toBe('default');
    });

    it('merges contexts', function () {
        $context1 = ExecutionContext::fromArray(['ip' => '127.0.0.1']);
        $context2 = ExecutionContext::fromArray(['user_agent' => 'Test']);
        $merged = $context1->merge($context2);

        expect($merged->toArray())->toBe(['ip' => '127.0.0.1', 'user_agent' => 'Test']);
    });

    it('merges with override', function () {
        $context1 = ExecutionContext::fromArray(['ip' => '127.0.0.1']);
        $context2 = ExecutionContext::fromArray(['ip' => '192.168.1.1']);
        $merged = $context1->merge($context2);

        expect($merged->get('ip'))->toBe('192.168.1.1');
    });
});
