<?php

declare(strict_types=1);

use App\Framework\MagicLinks\Actions\ActionResult;
use App\Framework\MagicLinks\ValueObjects\ActionResultData;
use App\Framework\MagicLinks\ValueObjects\ErrorCollection;

describe('ActionResult', function () {
    it('creates success result', function () {
        $result = ActionResult::success('Operation successful');

        expect($result->isSuccess())->toBeTrue();
        expect($result->success)->toBeTrue();
        expect($result->message)->toBe('Operation successful');
        expect($result->data)->toBeInstanceOf(ActionResultData::class);
        expect($result->data->isEmpty())->toBeTrue();
        expect($result->errors)->toBeInstanceOf(ErrorCollection::class);
        expect($result->errors->isEmpty())->toBeTrue();
    });

    it('creates success result with data', function () {
        $data = ActionResultData::fromArray(['user_id' => 123]);
        $result = ActionResult::success('Success', $data);

        expect($result->isSuccess())->toBeTrue();
        expect($result->data)->toBe($data);
        expect($result->data->get('user_id'))->toBe(123);
    });

    it('creates success result with redirect', function () {
        $result = ActionResult::success('Success', null, '/dashboard');

        expect($result->isSuccess())->toBeTrue();
        expect($result->redirectUrl)->toBe('/dashboard');
        expect($result->hasRedirect())->toBeTrue();
    });

    it('creates failure result', function () {
        $result = ActionResult::failure('Operation failed');

        expect($result->isSuccess())->toBeFalse();
        expect($result->success)->toBeFalse();
        expect($result->message)->toBe('Operation failed');
        expect($result->data->isEmpty())->toBeTrue();
        expect($result->errors->isEmpty())->toBeTrue();
    });

    it('creates failure result with errors', function () {
        $errors = ErrorCollection::fromArray(['Error 1', 'Error 2']);
        $result = ActionResult::failure('Failed', $errors);

        expect($result->isSuccess())->toBeFalse();
        expect($result->errors)->toBe($errors);
        expect($result->hasErrors())->toBeTrue();
        expect($result->errors->count())->toBe(2);
    });

    it('checks for redirect', function () {
        $withRedirect = ActionResult::success('Success', null, '/dashboard');
        $withoutRedirect = ActionResult::success('Success');

        expect($withRedirect->hasRedirect())->toBeTrue();
        expect($withoutRedirect->hasRedirect())->toBeFalse();
    });

    it('checks for errors', function () {
        $withErrors = ActionResult::failure('Failed', ErrorCollection::single('Error'));
        $withoutErrors = ActionResult::failure('Failed');

        expect($withErrors->hasErrors())->toBeTrue();
        expect($withoutErrors->hasErrors())->toBeFalse();
    });
});
