<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Rules;

final class RuleAction
{
    public function __construct(
        private readonly string $actionId,
        private readonly string $type,
        private readonly array $payload = [],
        private readonly string $createdAt = ''
    ) {}

    public static function create(string $actionId, string $type, array $payload = []): self
    {
        return new self($actionId, $type, $payload, date('c'));
    }

    public static function fromArray(array $data): self
    {
        return new self(
            (string) ($data['actionId'] ?? ''),
            (string) ($data['type'] ?? 'setDecision'),
            (array) ($data['payload'] ?? []),
            (string) ($data['createdAt'] ?? '')
        );
    }

    public function actionId(): string { return $this->actionId; }
    public function type(): string { return $this->type; }
    public function payload(): array { return $this->payload; }

    public function toArray(): array
    {
        return [
            'actionId' => $this->actionId,
            'type' => $this->type,
            'payload' => $this->payload,
            'createdAt' => $this->createdAt !== '' ? $this->createdAt : date('c'),
        ];
    }
}
