<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Support;

use App\Services\Install\DBErpInstallStatus;
use App\Services\SupportCenter\SupportCenterRuntimeConfig;

/**
 * 系统信息页只读展示数据（面向业务文案，不暴露配置键名）。
 */
final class DberpSystemInformationDisplay
{
    /**
     * @var array<string, mixed>|null
     */
    private ?array $authorizationOverviewSnapshot = null;

    public function __construct(
        private readonly DberpProductVersion $productVersion,
        private readonly DBErpInstallStatus $installStatus,
        private readonly HostSystemAuthorizationSummaryDisplay $authorizationSummaryDisplay,
        private readonly HostSystemAuthorizationCenterDisplay $authorizationCenterDisplay,
        private readonly SupportCenterRuntimeConfig $runtimeConfig,
        private readonly DberpCommercialLicense $commercialLicense,
    ) {}

    public function productName(): string
    {
        return $this->productVersion->publicProductName();
    }

    public function currentVersion(): string
    {
        return $this->productVersion->publicSemanticVersionLabel();
    }

    public function releaseDate(): ?string
    {
        $date = config('dberp.public_product.release_date');

        return is_string($date) && $date !== '' ? $date : null;
    }

    public function productStatus(): string
    {
        if (! $this->installStatus->isInstalled()) {
            return (string) __('dberp.system_information.values.product_status_pending_install');
        }

        return match (strtolower((string) config('app.env'))) {
            'production' => (string) __('dberp.system_information.values.product_status_running'),
            default => (string) __('dberp.system_information.values.product_status_non_production'),
        };
    }

    /**
     * 本机配置已购买，或支撑中心快照为「已获官方授权」时，视为已购买版权商用许可。
     */
    public function hasCommercialLicense(): bool
    {
        return $this->commercialLicense->isGranted();
    }

    public function usageVersionLabel(): string
    {
        if ($this->hasCommercialLicense()) {
            return (string) __('dberp.system_information.values.usage_version_commercial_licensed');
        }

        return (string) __('dberp.system_information.values.usage_version_free_commercial');
    }

    public function commercialAuthorizationLabel(): string
    {
        if ($this->hasCommercialLicense()) {
            return (string) __('dberp.system_information.values.commercial_authorization_licensed');
        }

        return (string) __('dberp.system_information.values.commercial_authorization_unlicensed');
    }

    public function licenseHolder(): ?string
    {
        if (! $this->hasCommercialLicense()) {
            return null;
        }

        $fromSync = $this->authorizationCenterDisplay->licenseHolder();
        if ($fromSync !== null) {
            return $fromSync;
        }

        if ($this->authorizationCenterDisplay->isAuthorizationGranted()) {
            return null;
        }

        $holder = config('dberp.license.holder');

        return is_string($holder) && $holder !== '' ? $holder : null;
    }

    public function licenseValidUntil(): ?string
    {
        if (! $this->hasCommercialLicense()) {
            return null;
        }

        $fromSync = $this->authorizationCenterDisplay->expiresAtLabel();
        if ($fromSync !== null) {
            return $fromSync;
        }

        if ($this->authorizationCenterDisplay->isAuthorizationGranted()) {
            return null;
        }

        $until = config('dberp.license.valid_until');

        return is_string($until) && $until !== '' ? $until : null;
    }

    public function copyrightNotice(): string
    {
        if ($this->hasCommercialLicense()) {
            return (string) __('dberp.system_information.values.copyright_notice_licensed');
        }

        return (string) __('dberp.system_information.values.copyright_notice_restricted');
    }

    public function licenseExplanation(): ?string
    {
        if ($this->hasCommercialLicense()) {
            return null;
        }

        return (string) __('dberp.system_information.values.license_explanation_free_commercial');
    }

    public function usageVersionBadgeColor(): string
    {
        return $this->hasCommercialLicense() ? 'success' : 'gray';
    }

    public function commercialAuthorizationBadgeColor(): string
    {
        return $this->hasCommercialLicense() ? 'success' : 'warning';
    }

    public function officialServiceCenterUrl(): ?string
    {
        return $this->runtimeConfig->normalizedBaseUrl();
    }

    public function shouldShowCopyrightLicensePurchaseLink(): bool
    {
        return ! $this->hasCommercialLicense() && filled($this->officialServiceCenterUrl());
    }

    /**
     * @return array<string, mixed>
     */
    public function authorizationOverviewSummary(): array
    {
        return $this->authorizationOverviewSnapshot ??= $this->authorizationSummaryDisplay->summary();
    }

    public function siteOfficialBindingLabel(bool $isSiteBound): string
    {
        return $isSiteBound
            ? (string) __('dberp.system_information.authorization_overview.site_bound_yes')
            : (string) __('dberp.system_information.authorization_overview.site_bound_no');
    }

    public function siteOfficialBindingBadgeColor(bool $isSiteBound): string
    {
        return $isSiteBound ? 'success' : 'gray';
    }

    public function hasOfficialServiceExpiry(?string $expiresAtLabel): bool
    {
        return is_string($expiresAtLabel) && $expiresAtLabel !== '';
    }

    public function hasOfficialRemainingServiceDays(?string $remainingDaysLabel): bool
    {
        return is_string($remainingDaysLabel) && $remainingDaysLabel !== '';
    }

    public function productStatusBadgeColor(): string
    {
        if (! $this->installStatus->isInstalled()) {
            return 'warning';
        }

        return strtolower((string) config('app.env')) === 'production' ? 'success' : 'info';
    }
}
