<?php
/**
 * @package     MefisAssetBridge
 * @subpackage  MefisAssetBridgeCore_Auth
 *
 * @copyright   Copyright (C) 2025 Goran Usljebrka. All rights reserved.
 * @license     Proprietary — No resale or incorporation into other projects without explicit written permission. see LICENSE.txt
 */

namespace MefisAssetBridgeCore\Auth;

use MefisAssetBridgeCore\DTO\AuthenticatedUserDto;

/**
 * Authenticated User class
 */
class AuthenticatedUser
{
    /** @var AuthenticatedUserDto User DTO */
    private AuthenticatedUserDto $userData;

    /**
     * Create a new AuthenticatedUser
     *
     * @param AuthenticatedUserDto $userData User data
     */
    public function __construct(AuthenticatedUserDto $userData)
    {
        $this->userData = $userData;
    }

    /**
     * Check if user is authenticated
     *
     * @return bool
     */
    public function isAuthenticated(): bool
    {
        return !$this->isTokenExpired();
    }

    /**
     * Check if token is expired
     *
     * @return bool
     */
    public function isTokenExpired(): bool
    {
        $expiresAt = $this->userData->getToken()->getTokenExpiresAt();
        return $expiresAt <= new \DateTime();
    }

    /**
     * Check if refresh token is expired
     *
     * @return bool
     */
    public function isRefreshTokenExpired(): bool
    {
        $expiresAt = $this->userData->getToken()->getRefreshTokenExpiresAt();
        return $expiresAt <= new \DateTime();
    }

    /**
     * Check if refresh is needed/possible
     *
     * @return bool True if token is expired but refresh token is still valid
     */
    public function canRefresh(): bool
    {
        return $this->isTokenExpired() && !$this->isRefreshTokenExpired();
    }

    /**
     * Get user data
     *
     * @return AuthenticatedUserDto
     */
    public function getUserData(): AuthenticatedUserDto
    {
        return $this->userData;
    }

    /**
     * Get user ID
     *
     * @return int
     */
    public function getUserId(): int
    {
        return $this->userData->getUser()->getUserId();
    }

    /**
     * Get access level
     *
     * @return string
     */
    public function getAccessLevel(): string
    {
        return $this->userData->getUser()->getAccessLevel();
    }

    /**
     * Get token
     *
     * @return string
     */
    public function getToken(): string
    {
        return $this->userData->getToken()->getToken();
    }

    /**
     * Get refresh token
     *
     * @return string
     */
    public function getRefreshToken(): string
    {
        return $this->userData->getToken()->getRefreshToken();
    }

    /**
     * Get minutes until token expiration
     *
     * @return int
     */
    public function getMinutesUntilExpiration(): int
    {
        $expiresAt = $this->userData->getToken()->getTokenExpiresAt();
        $now = new \DateTime();

        if ($expiresAt <= $now) {
            return 0;
        }

        $diff = $expiresAt->getTimestamp() - $now->getTimestamp();
        return (int)($diff / 60);
    }
}