<?php
/**
 * @package     MefisAssetBridge
 * @subpackage  MefisAssetBridgeCore_DTO
 *
 * @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\DTO;

/**
 * Token Data DTO
 */
class TokenData
{
    /** @var string The authentication token */
    private string $token;

    /** @var \DateTime When the token expires */
    private \DateTime $tokenExpiresAt;

    /** @var string The refresh token */
    private string $refreshToken;

    /** @var \DateTime When the refresh token expires */
    private \DateTime $refreshTokenExpiresAt;

    /**
     * Create a new TokenData instance
     *
     * @param string $token The authentication token
     * @param string $tokenExpiresAt Token expiration date (ISO 8601 format)
     * @param string $refreshToken Refresh token
     * @param string $refreshTokenExpiresAt Refresh token expiration date (ISO 8601 format)
     */
    public function __construct(
        string $token,
        string $tokenExpiresAt,
        string $refreshToken,
        string $refreshTokenExpiresAt
    ) {
        $this->token = $token;
        $this->tokenExpiresAt = new \DateTime($tokenExpiresAt);
        $this->refreshToken = $refreshToken;
        $this->refreshTokenExpiresAt = new \DateTime($refreshTokenExpiresAt);
    }

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

    /**
     * Get token expiration datetime
     *
     * @return \DateTime
     */
    public function getTokenExpiresAt(): \DateTime
    {
        return $this->tokenExpiresAt;
    }

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

    /**
     * Get refresh token expiration datetime
     *
     * @return \DateTime
     */
    public function getRefreshTokenExpiresAt(): \DateTime
    {
        return $this->refreshTokenExpiresAt;
    }

    public function setToken(string $token): void
    {
        $this->token = $token;
    }

    public function setTokenExpiresAt(string $tokenExpiresAt): void
    {
        $this->tokenExpiresAt = new \DateTime($tokenExpiresAt);
    }

    public function setRefreshToken(string $refreshToken): void
    {
        $this->refreshToken = $refreshToken;
    }

    public function setRefreshTokenExpiresAt(string $refreshTokenExpiresAt): void
    {
        $this->refreshTokenExpiresAt = new \DateTime($refreshTokenExpiresAt);
    }

    /**
     * Create from array
     *
     * @param array $data The data array
     * @return self
     */
    public static function fromArray(array $data): self
    {
        return new self(
            $data['token'] ?? '',
            $data['token_expires_at'] ?? 'now',
            $data['refresh_token'] ?? '',
            $data['refresh_token_expires_at'] ?? 'now'
        );
    }

    /**
     * Convert to array
     *
     * @return array
     */
    public function toArray(): array
    {
        return [
            'token' => $this->token,
            'token_expires_at' => $this->tokenExpiresAt->format('c'),
            'refresh_token' => $this->refreshToken,
            'refresh_token_expires_at' => $this->refreshTokenExpiresAt->format('c')
        ];
    }
}