<?php

namespace App\Core;

class Auth
{
    public static function login(array $user): void
    {
        session_regenerate_id(true);

        $_SESSION['admin'] = [
            'id'       => $user['id'],
            'nama'     => $user['nama'],
            'username' => $user['username'],
            'role'     => $user['role'],
        ];
    }

    public static function logout(): void
    {
        unset($_SESSION['admin']);
        session_regenerate_id(true);
    }

    public static function check(): bool
    {
        return isset($_SESSION['admin']);
    }

    public static function user(): ?array
    {
        return $_SESSION['admin'] ?? null;
    }

    public static function id(): ?int
    {
        return $_SESSION['admin']['id'] ?? null;
    }
}
