<?php

namespace App\Libraries;

use Carbon\Carbon;

class UserAuth
{
    public static function setAuth($user): void
    {
        $session = session();
        $data = [
            'logged_in' => true,
            'user' => $user,
            'last_checked_time' => null,
        ];
        $session->set($data);
    }

    public static function check(): bool
    {
        $session = session();
        return $session->has('logged_in');
    }

    public static function forget(): void
    {
        $session = session();
        $session->remove('logged_in');
        $session->remove('user');
        $session->remove('last_checked_time');
    }
    public static function getLastCheckedTime()
    {
        $session = session();
        if ($session->has('logged_in')) {
            if ($session->has('last_checked_time')) {
                return $session->get('last_checked_time');
            }
        }
        return null;
    }
    public static function setLastCheckedTime()
    {
        $session = session();

        $session->set(['last_checked_time' => Carbon::now()]);
    }

    public static function id()
    {
        $session = session();
        if ($session->has('logged_in')) {
            if ($session->has('user')) {
                return $session->get('user')->id;
            }
        }
        return null;
    }



    public static function user()
    {
        $session = session();
        if ($session->has('logged_in')) {
            if ($session->has('user')) {
                return $session->get('user');
            }
        }
        return null;
    }
}
