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

/**
 * Authenticated User Data Transfer Object
 */
class AuthenticatedUserDto
{
    /** @var TokenData Token data */
    private TokenData $token;

    /** @var UserInfo User information */
    private UserInfo $user;

    /** @var SubscriptionInfo Subscription information */
    private SubscriptionInfo $subscription;

    /** @var string Master Key to encode asset data */
    private string $masterKey;

    /**
     * Create a new AuthenticatedUserDto instance
     *
     * @param TokenData $token Token data
     * @param UserInfo $user User information
     * @param SubscriptionInfo $subscription Subscription information
     */
    public function __construct(
        TokenData $token,
        UserInfo $user,
        SubscriptionInfo $subscription,
        string $masterKey = ''
    ) {
        $this->token = $token;
        $this->user = $user;
        $this->subscription = $subscription;
        $this->masterKey = $masterKey;
    }
    public function setMasterKey(string $masterKey): void
    {
        $this->masterKey = $masterKey;
    }
    public function getMasterKey(): string
    {
        return $this->masterKey;
    }
    /**
     * Get token data
     *
     * @return TokenData
     */
    public function getToken(): TokenData
    {
        return $this->token;
    }

    /**
     * Get user information
     *
     * @return UserInfo
     */
    public function getUser(): UserInfo
    {
        return $this->user;
    }

    /**
     * Get subscription information
     *
     * @return SubscriptionInfo
     */
    public function getSubscription(): SubscriptionInfo
    {
        return $this->subscription;
    }

    /**
     * Create from array
     *
     * @param array $data The data array
     * @return self
     */
    public static function fromArray(array $data): self
    {
        return new self(
            TokenData::fromArray($data['token'] ?? []),
            UserInfo::fromArray($data['user'] ?? []),
            SubscriptionInfo::fromArray($data['subscription'] ?? []),
            $data['masterKey'] ?? ''
        );
    }

    /**
     * Convert to array
     *
     * @return array
     */
    public function toArray(): array
    {
        return [
            'token' => $this->token->toArray(),
            'user' => $this->user->toArray(),
            'subscription' => $this->subscription->toArray(),
            'masterKey' => $this->masterKey
        ];
    }
}