<?php

declare(strict_types=1);

use App\Framework\MagicLinks\ValueObjects\ActionResultData;

describe('ActionResultData', function () {
    it('creates empty result data', function () {
        $data = ActionResultData::empty();

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

    it('creates from array', function () {
        $data = ActionResultData::fromArray(['user_id' => 123, 'email' => 'test@example.com']);

        expect($data->isEmpty())->toBeFalse();
        expect($data->toArray())->toBe(['user_id' => 123, 'email' => 'test@example.com']);
    });

    it('adds values immutably', function () {
        $data = ActionResultData::empty();
        $updated = $data->with('status', 'success');

        expect($data->has('status'))->toBeFalse();
        expect($updated->has('status'))->toBeTrue();
        expect($updated->get('status'))->toBe('success');
    });

    it('retrieves values with defaults', function () {
        $data = ActionResultData::fromArray(['status' => 'success']);

        expect($data->get('status'))->toBe('success');
        expect($data->get('missing', 'default'))->toBe('default');
    });

    it('merges data', function () {
        $data1 = ActionResultData::fromArray(['user_id' => 123]);
        $data2 = ActionResultData::fromArray(['email' => 'test@example.com']);
        $merged = $data1->merge($data2);

        expect($merged->toArray())->toBe([
            'user_id' => 123,
            'email' => 'test@example.com',
        ]);
    });

    it('checks key existence', function () {
        $data = ActionResultData::fromArray(['user_id' => 123]);

        expect($data->has('user_id'))->toBeTrue();
        expect($data->has('missing'))->toBeFalse();
    });
});
