<?php

declare(strict_types=1);

namespace App\Support;

use Illuminate\Support\Facades\Schema;

/**
 * 商用/官方授权是否生效（与系统信息页「已购买版权商用许可」判定一致）。
 */
final class DberpCommercialLicense
{
    private ?bool $granted = null;

    public function __construct(
        private readonly HostSystemAuthorizationCenterDisplay $authorizationCenterDisplay,
    ) {}

    public function isGranted(): bool
    {
        if ($this->granted !== null) {
            return $this->granted;
        }

        if ((bool) config('dberp.license.commercial_authorized', false)) {
            return $this->granted = true;
        }

        if (! Schema::hasTable('host_system_authorizations')) {
            return $this->granted = false;
        }

        return $this->granted = $this->authorizationCenterDisplay->isAuthorizationGranted();
    }
}
