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

/**
 * Subscription Info DTO
 */
class SubscriptionInfo
{
    /** @var string Subscription level name */
    private string $levelName;

    /** @var int Token limit */
    private int $tokenLimit;

    /** @var int Maximum assets allowed */
    private int $maxAssets;

    /** @var int Maximum teams allowed */
    private int $maxTeams;

    /** @var int Maximum users per team */
    private int $maxUsersPerTeam;

    /** @var bool|null Whether the subscription will auto-renew; null for free tier */
    private ?bool $willRenew;

    /** @var string|null Subscription expiry/renewal date in ISO 8601 UTC; null for free tier */
    private ?string $validUntilZulu;

    /**
     * Create a new SubscriptionInfo instance
     *
     * @param string $levelName Subscription level name
     * @param int $tokenLimit Token limit
     * @param int $maxAssets Maximum assets allowed
     * @param int $maxTeams Maximum teams allowed
     * @param int $maxUsersPerTeam Maximum users per team
     * @param bool|null $willRenew Whether the subscription will auto-renew; null for free tier
     * @param string|null $validUntilZulu Subscription expiry/renewal date in ISO 8601 UTC; null for free tier
     */
    public function __construct(
        string $levelName,
        int $tokenLimit,
        int $maxAssets,
        int $maxTeams,
        int $maxUsersPerTeam,
        ?bool $willRenew = null,
        ?string $validUntilZulu = null
    ) {
        $this->levelName = $levelName;
        $this->tokenLimit = $tokenLimit;
        $this->maxAssets = $maxAssets;
        $this->maxTeams = $maxTeams;
        $this->maxUsersPerTeam = $maxUsersPerTeam;
        $this->willRenew = $willRenew;
        $this->validUntilZulu = $validUntilZulu;
    }

    /**
     * Get level name
     *
     * @return string
     */
    public function getLevelName(): string
    {
        return $this->levelName;
    }

    /**
     * Get token limit
     *
     * @return int
     */
    public function getTokenLimit(): int
    {
        return $this->tokenLimit;
    }

    /**
     * Get maximum assets allowed
     *
     * @return int
     */
    public function getMaxAssets(): int
    {
        return $this->maxAssets;
    }

    /**
     * Get maximum teams allowed
     *
     * @return int
     */
    public function getMaxTeams(): int
    {
        return $this->maxTeams;
    }

    /**
     * Get maximum users per team
     *
     * @return int
     */
    public function getMaxUsersPerTeam(): int
    {
        return $this->maxUsersPerTeam;
    }

    /**
     * Get whether the subscription will auto-renew
     *
     * @return bool|null
     */
    public function getWillRenew(): ?bool
    {
        return $this->willRenew;
    }

    /**
     * Get subscription expiry/renewal date in ISO 8601 UTC
     *
     * @return string|null
     */
    public function getValidUntilZulu(): ?string
    {
        return $this->validUntilZulu;
    }

    /**
     * Create from array
     *
     * @param array $data The data array
     * @return self
     */
    public static function fromArray(array $data): self
    {
        return new self(
            $data['level_name'] ?? 'free',
            (int)($data['token_limit'] ?? 0),
            (int)($data['max_assets'] ?? 0),
            (int)($data['max_teams'] ?? 0),
            (int)($data['max_users_per_team'] ?? 0),
            isset($data['will_renew']) ? (bool)$data['will_renew'] : null,
            $data['valid_until_zulu'] ?? null
        );
    }

    /**
     * Convert to array
     *
     * @return array
     */
    public function toArray(): array
    {
        return [
            'level_name'         => $this->levelName,
            'token_limit'        => $this->tokenLimit,
            'max_assets'         => $this->maxAssets,
            'max_teams'          => $this->maxTeams,
            'max_users_per_team' => $this->maxUsersPerTeam,
            'will_renew'         => $this->willRenew,
            'valid_until_zulu'   => $this->validUntilZulu,
        ];
    }
}