<?php

namespace Lucent\Record;

use Carbon\Carbon;
use Lucent\Schema\Schema;

readonly class System
{


    function __construct(
        public int    $version,
        public string $createdBy,
        public string $updatedBy,
        public string $createdAt,
        public string $updatedAt,
    )
    {

    }


    public static function fromArray(array $data): System
    {
        return new System(
            version: data_get($data, "version"),
            createdBy: data_get($data, "createdBy"),
            updatedBy: data_get($data, "updatedBy"),
            createdAt: data_get($data, "createdAt"),
            updatedAt: data_get($data, "updatedAt"),
        );
    }


    public static function newRecord(string $userId): System
    {
        $now = Carbon::now()->toJson();
        return new System(
            version: 1,
            createdBy: $userId,
            updatedBy: $userId,
            createdAt: $now,
            updatedAt: $now,
        );
    }


    public function update(string $userId): System
    {
        $now = Carbon::now()->toJson();
        return new System(
            version: $this->version + 1,
            createdBy: $this->createdBy,
            updatedBy: $userId,
            createdAt: $this->createdAt,
            updatedAt: $now,
        );
    }


}
