<?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 DateTimeImmutable;

/**
 * A one-time 2FA recovery code (Plan 007). Stored only as a hash
 * (password_hash); the plaintext is shown to the user once at enrolment and
 * never persisted. `used_at` burns the code on first use.
 *
 * @property ?int $id
 * @property int $user_id
 * @property string $code_hash
 * @property ?DateTimeImmutable $used_at
 * @property ?DateTimeImmutable $created
 * @property ?string $created_by
 * @property ?DateTimeImmutable $modified
 * @property ?string $modified_by
 */
class TwoFactorRecoveryCodeEntity extends Entity
{
    use hasCasts;

    final public const string TABLE = 'user_two_factor_recovery_codes';

    public function __construct()
    {
        parent::__construct(self::TABLE);
        $this->setSensitiveData(['code_hash']);
    }

    public function casts(): array
    {
        return [
            'id' => Casts::INT,
            'user_id' => Casts::INT,
            'used_at' => Casts::DATETIME_STAMP,
            'created' => Casts::DATETIME_STAMP,
            'modified' => Casts::DATETIME_STAMP,
        ];
    }

    public function isUsed(): bool
    {
        return $this->used_at instanceof DateTimeImmutable;
    }
}
