<?php

declare(strict_types=1);

namespace apexl\Io\modules\user\entities;

use apexl\entityCore\enums\Casts;
use apexl\entityCore\traits\hasCasts;
use apexl\Io\includes\Entity;
use apexl\Io\modules\user\Dtos\SessionDto;
use DateTimeImmutable;

/**
 * @property string $sessionId
 * @property int $uid
 * @property DateTimeImmutable $created
 * @property bool $ended
 * @property bool $active
 * @property ?array $session_data
 **/
class sessionEntity extends Entity
{
    use hasCasts;

    private const string TABLE = 'sessions';
    private const string KEY = 'sessionId';

    public function __construct()
    {
        parent::__construct(self::TABLE, self::KEY);
    }

    public function casts(): array
    {
        return [
            ...parent::casts(),
            'created' => Casts::DATETIME_STAMP,
            'ended' => Casts::DATETIME_STAMP,
            'session_data' => Casts::SERIALIZE,
        ];
    }

    public function toDto(): SessionDto
    {
        return new SessionDto(
            uid: $this->uid,
            sessionId: $this->sessionId,
            active: $this->active,
            data: $this->session_data ?? [],
        );
    }

    protected function definedRelations(): array
    {
        return [
            $this->hasOne('user', userEntity::class, 'uid'),
        ];
    }
}
