<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Sourcing\Verification;

final class EventSourcingRuntimeCheck
{
    public function __construct(
        private readonly string $check,
        private readonly bool $passed,
        private readonly string $message = '',
        private readonly array $details = [],
        private readonly string $createdAt = ''
    ) {}

    public static function pass(string $check, string $message = 'check passed', array $details = []): self
    {
        return new self($check, true, $message, $details, date('c'));
    }

    public static function fail(string $check, string $message = 'check failed', array $details = []): self
    {
        return new self($check, false, $message, $details, date('c'));
    }

    public static function fromArray(array $data): self
    {
        return new self(
            (string) ($data['check'] ?? ''),
            (bool) ($data['passed'] ?? false),
            (string) ($data['message'] ?? ''),
            (array) ($data['details'] ?? []),
            (string) ($data['createdAt'] ?? '')
        );
    }

    public function check(): string { return $this->check; }
    public function passed(): bool { return $this->passed; }

    public function toArray(): array
    {
        return [
            'check' => $this->check,
            'passed' => $this->passed,
            'message' => $this->message,
            'details' => $this->details,
            'createdAt' => $this->createdAt !== '' ? $this->createdAt : date('c'),
        ];
    }
}
