<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Rules;

final class RulesRuntimeSnapshot
{
    public function __construct(
        private readonly array $ruleSets = [],
        private readonly array $decisionTables = [],
        private readonly array $results = [],
        private readonly array $statistics = [],
        private readonly string $createdAt = ''
    ) {}

    public static function fromArray(array $data): self
    {
        return new self(
            (array) ($data['ruleSets'] ?? []),
            (array) ($data['decisionTables'] ?? []),
            (array) ($data['results'] ?? []),
            (array) ($data['statistics'] ?? []),
            (string) ($data['createdAt'] ?? '')
        );
    }

    public function ruleSets(): array { return $this->ruleSets; }
    public function decisionTables(): array { return $this->decisionTables; }
    public function results(): array { return $this->results; }
    public function statistics(): array { return $this->statistics; }

    public function toArray(): array
    {
        return [
            'ruleSets' => $this->ruleSets,
            'decisionTables' => $this->decisionTables,
            'results' => $this->results,
            'statistics' => $this->statistics,
            'createdAt' => $this->createdAt !== '' ? $this->createdAt : date('c'),
        ];
    }
}
