<?php

declare(strict_types=1);

namespace Apexl\Io\User\Entities;

use Apexl\Io\Entity;
use Apexl\Io\Enums\Casts;
use Apexl\Io\Traits\HasCasts;
use Apexl\Io\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;

    public const string TABLE = 'sessions';
    public const string PRIMARY_KEY = 'sessionId';

    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'),
        ];
    }
}
