<?php

declare(strict_types=1);

namespace App\Framework\MagicLinks\Actions;

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

final readonly class ActionResult
{
    public readonly ActionResultData $data;
    public readonly ErrorCollection $errors;

    public function __construct(
        public bool $success,
        public string $message = '',
        ?ActionResultData $data = null,
        public ?string $redirectUrl = null,
        ?ErrorCollection $errors = null
    ) {
        $this->data = $data ?? ActionResultData::empty();
        $this->errors = $errors ?? ErrorCollection::empty();
    }

    public static function success(
        string $message = '',
        ?ActionResultData $data = null,
        ?string $redirectUrl = null
    ): self {
        return new self(
            success: true,
            message: $message,
            data: $data ?? ActionResultData::empty(),
            redirectUrl: $redirectUrl,
            errors: ErrorCollection::empty()
        );
    }

    public static function failure(
        string $message,
        ?ErrorCollection $errors = null
    ): self {
        return new self(
            success: false,
            message: $message,
            data: ActionResultData::empty(),
            redirectUrl: null,
            errors: $errors ?? ErrorCollection::empty()
        );
    }

    public function isSuccess(): bool
    {
        return $this->success;
    }

    public function hasRedirect(): bool
    {
        return $this->redirectUrl !== null;
    }

    public function hasErrors(): bool
    {
        return $this->errors->hasErrors();
    }
}
