<?php

declare(strict_types=1);

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

namespace App\Providers;

use App\Business\Integration\Adapters\DbshopChannelAdapter;
use App\Business\Integration\Contracts\ChannelSyncAdapterInterface;
use App\Business\Inventory\InventoryGovernanceReminderService;
use App\Business\Purchase\PurchaseCollaborationReminderService;
use App\Business\Settlement\SettlementReminderService;
use App\Business\Workbench\BusinessOverviewMetricService;
use App\Business\Workbench\UnifiedWorkbenchReminderService;
use App\Services\DberpOfficialNewsFeedService;
use App\Services\DberpSystemUpdateNoticeService;
use App\Services\Install\DBErpInstallStatus;
use App\Services\Plugin\DberpDatabasePluginRepository;
use App\Services\Plugin\DberpOfficialPluginGuard;
use App\Services\Plugin\DberpPluginBootstrapService;
use App\Services\Plugin\DberpPluginDiscoveryManager;
use App\Services\Plugin\HostPluginAvailabilityPresenter;
use App\Services\Plugin\HostPluginCatalogInstallStateResolver;
use App\Services\Plugin\HostPluginCatalogService;
use App\Services\Plugin\HostPluginClaimCheckService;
use App\Services\Plugin\HostPluginDownloadTokenService;
use App\Services\Plugin\HostPluginEntitlementGate;
use App\Services\Plugin\HostPluginEntryInstallInvoker;
use App\Services\Plugin\HostPluginInstallOrchestrator;
use App\Services\Plugin\HostPluginInstallRollback;
use App\Services\Plugin\HostPluginLifecycleInstallService;
use App\Services\Plugin\HostPluginLifecycleService;
use App\Services\Plugin\HostPluginManifestPermissionCatalog;
use App\Services\Plugin\HostPluginManifestValidator;
use App\Services\Plugin\HostPluginNavigationRegistrar;
use App\Services\Plugin\HostPluginOperationLock;
use App\Services\Plugin\HostPluginOperationLogger;
use App\Services\Plugin\HostPluginPackageDownloadService;
use App\Services\Plugin\HostPluginPackageSignatureVerifier;
use App\Services\Plugin\HostPluginPackageStagingHelper;
use App\Services\Plugin\HostPluginPackageVerifier;
use App\Services\Plugin\HostPluginPermissionResolver;
use App\Services\Plugin\HostPluginReleasePackageSignatureValidator;
use App\Services\Plugin\HostPluginReleasePackageVerifier;
use App\Services\Plugin\HostPluginRuntimeCacheCleaner;
use App\Services\Plugin\HostPluginSettingsRouteResolver;
use App\Services\Plugin\HostPluginStatusPresenter;
use App\Services\Plugin\HostPluginSupportCenterContextResolver;
use App\Services\Plugin\HostPluginUpdateCheckService;
use App\Services\Plugin\HostPluginUpdateOrchestrator;
use App\Services\Plugin\HostPluginUpdateStatusPresenter;
use App\Services\SupportCenter\HostInstallationIdentityService;
use App\Services\SupportCenter\HostInstanceCredentialStore;
use App\Services\SupportCenter\HostInstanceIdentityPresenter;
use App\Services\SupportCenter\HostInstanceIdentityService;
use App\Services\SupportCenter\HostSiteConnectionStatusResolver;
use App\Services\SupportCenter\HostSiteOnboardingConfirmService;
use App\Services\SupportCenter\HostSiteOnboardingHeartbeatService;
use App\Services\SupportCenter\HostSiteOnboardingLastAttemptStore;
use App\Services\SupportCenter\HostSiteOnboardingStatusCacheStore;
use App\Services\SupportCenter\HostSiteOnboardingStatusPresenter;
use App\Services\SupportCenter\HostSiteOnboardingStatusQueryService;
use App\Services\SupportCenter\HostSiteVerificationResponseBuilder;
use App\Services\SupportCenter\HostSiteVerificationStaticPublisher;
use App\Services\SupportCenter\HostSystemAuthorizationEntitlementPresenter;
use App\Services\SupportCenter\HostSystemAuthorizationSnapshotMapper;
use App\Services\SupportCenter\HostSystemAuthorizationStatusPresenter;
use App\Services\SupportCenter\HostSystemAuthorizationSyncService;
use App\Services\SupportCenter\HostSystemUpdateApplyService;
use App\Services\SupportCenter\HostSystemUpdateArchiveExtractor;
use App\Services\SupportCenter\HostSystemUpdateArtisanCommandHandler;
use App\Services\SupportCenter\HostSystemUpdateArtisanCommandRunner;
use App\Services\SupportCenter\HostSystemUpdateAutoCheckStateStore;
use App\Services\SupportCenter\HostSystemUpdateAutoCheckTriggerService;
use App\Services\SupportCenter\HostSystemUpdateBackupManifestBuilder;
use App\Services\SupportCenter\HostSystemUpdateBackupService;
use App\Services\SupportCenter\HostSystemUpdateCacheCleaner;
use App\Services\SupportCenter\HostSystemUpdateCheckService;
use App\Services\SupportCenter\HostSystemUpdateCheckSnapshotMapper;
use App\Services\SupportCenter\HostSystemUpdateComposerDependencyInstaller;
use App\Services\SupportCenter\HostSystemUpdateDatabaseRollbackBoundary;
use App\Services\SupportCenter\HostSystemUpdateDownloadTokenService;
use App\Services\SupportCenter\HostSystemUpdateEntitlementGate;
use App\Services\SupportCenter\HostSystemUpdateEnvRestoreService;
use App\Services\SupportCenter\HostSystemUpdateExecutionContextResolver;
use App\Services\SupportCenter\HostSystemUpdateExecutionCreateService;
use App\Services\SupportCenter\HostSystemUpdateExecutionLock;
use App\Services\SupportCenter\HostSystemUpdateExecutionLogger;
use App\Services\SupportCenter\HostSystemUpdateExecutionLogPruningService;
use App\Services\SupportCenter\HostSystemUpdateExecutionManifestSummarizer;
use App\Services\SupportCenter\HostSystemUpdateExecutionOrchestrator;
use App\Services\SupportCenter\HostSystemUpdateExecutionStateMachine;
use App\Services\SupportCenter\HostSystemUpdateFailureHandler;
use App\Services\SupportCenter\HostSystemUpdateFileChecksumVerifier;
use App\Services\SupportCenter\HostSystemUpdateFileDeletionService;
use App\Services\SupportCenter\HostSystemUpdateFileReplacer;
use App\Services\SupportCenter\HostSystemUpdateFileRestoreService;
use App\Services\SupportCenter\HostSystemUpdateFinalizeService;
use App\Services\SupportCenter\HostSystemUpdateHandlerFactory;
use App\Services\SupportCenter\HostSystemUpdateMaintenanceCommandHandler;
use App\Services\SupportCenter\HostSystemUpdateMaintenanceModeService;
use App\Services\SupportCenter\HostSystemUpdateManifestParseService;
use App\Services\SupportCenter\HostSystemUpdateManifestReader;
use App\Services\SupportCenter\HostSystemUpdateManifestValidator;
use App\Services\SupportCenter\HostSystemUpdateMigrationBatchExecutor;
use App\Services\SupportCenter\HostSystemUpdateMigrationBatchProcessRunner;
use App\Services\SupportCenter\HostSystemUpdateMigrationRunner;
use App\Services\SupportCenter\HostSystemUpdateNotificationSyncService;
use App\Services\SupportCenter\HostSystemUpdateOpcache;
use App\Services\SupportCenter\HostSystemUpdatePackageVerifier;
use App\Services\SupportCenter\HostSystemUpdatePrepareService;
use App\Services\SupportCenter\HostSystemUpdateProtectedPathGuard;
use App\Services\SupportCenter\HostSystemUpdateRollbackService;
use App\Services\SupportCenter\HostSystemUpdateStagingService;
use App\Services\SupportCenter\HostSystemUpdateVendorBundleReplacer;
use App\Services\SupportCenter\HostSystemUpdateVendorReplacer;
use App\Services\SupportCenter\HostSystemUpdateVendorStrategyResolver;
use App\Services\SupportCenter\HostSystemUpdateVersionWriter;
use App\Services\SupportCenter\InProcessHostSystemUpdateArtisanCommandHandler;
use App\Services\SupportCenter\InProcessHostSystemUpdateMaintenanceCommandHandler;
use App\Services\SupportCenter\NativeHostSystemUpdateOpcache;
use App\Services\SupportCenter\ProcessHostSystemUpdateArtisanCommandHandler;
use App\Services\SupportCenter\ProcessHostSystemUpdateMaintenanceCommandHandler;
use App\Services\SupportCenter\SupportCenterAuditLogger;
use App\Services\SupportCenter\SupportCenterCheckOutputSanitizer;
use App\Services\SupportCenter\SupportCenterConnectivityGuard;
use App\Services\SupportCenter\SupportCenterCredentialResolver;
use App\Services\SupportCenter\SupportCenterLocalChecks;
use App\Services\SupportCenter\SupportCenterOpenApiTransport;
use App\Services\SupportCenter\SupportCenterPostInstallCheckService;
use App\Services\SupportCenter\SupportCenterReleaseCredentialProvider;
use App\Services\SupportCenter\SupportCenterReleasePackageCheckService;
use App\Services\SupportCenter\SupportCenterRemoteProbe;
use App\Services\SupportCenter\SupportCenterRequestContextBuilder;
use App\Services\SupportCenter\SupportCenterRuntimeConfig;
use App\Services\SupportCenter\SupportCenterSiteCredentialProvider;
use App\Services\SupportCenter\SupportCenterUserFacingErrors;
use App\Setting\DberpHostSettingDefinitionProvider;
use App\Support\DberpSettlementWriteOffCounterpartyNames;
use App\Support\HostPluginCenterDisplay;
use App\Support\HostPluginCenterSettingsPanelFormatter;
use App\Support\HostPluginSettingsPanelRegistry;
use App\Support\HostSystemAuthorizationCenterDisplay;
use App\Support\HostSystemAuthorizationSummaryDisplay;
use App\Support\SiteOnboardingCenterDisplay;
use DBCore\Plugin\Contracts\PluginManagerInterface;
use DBCore\Plugin\Contracts\PluginRepositoryInterface;
use DBCore\Plugin\Managers\PluginManager as PluginLifecycleManager;
use DBCore\Plugin\PluginManifestReader;
use DBCore\Plugin\PluginRegistry;
use DBCore\Setting\Contracts\SettingManagerInterface;
use Illuminate\Support\ServiceProvider;

/**
 * DBErp 宿主：合并业务配置、绑定后续可替换的集成接口（不写具体同步逻辑）。
 */
class DBErpServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->mergeConfigFrom(base_path('config/dberp.php'), 'dberp');

        $this->registerPluginRuntimeBindings();
        $this->app->register(DberpPluginRuntimeServiceProvider::class);

        $this->app->singleton(
            DberpShortcutRegistry::class,
            static fn (): DberpShortcutRegistry => new DberpShortcutRegistry,
        );

        $this->app->singleton(BusinessOverviewMetricService::class);
        $this->app->singleton(UnifiedWorkbenchReminderService::class);
        $this->app->singleton(SettlementReminderService::class);
        $this->app->singleton(InventoryGovernanceReminderService::class);
        $this->app->singleton(PurchaseCollaborationReminderService::class);
        $this->app->singleton(DberpSettlementWriteOffCounterpartyNames::class);

        $this->app->bind(
            ChannelSyncAdapterInterface::class,
            DbshopChannelAdapter::class
        );

        $this->app->singleton(HostInstanceCredentialStore::class);
        $this->app->singleton(HostInstanceIdentityService::class);
        $this->app->singleton(HostInstanceIdentityPresenter::class);
        $this->app->singleton(HostInstallationIdentityService::class);
        $this->app->singleton(HostSiteConnectionStatusResolver::class);
        $this->app->singleton(HostSiteVerificationResponseBuilder::class);
        $this->app->singleton(HostSiteVerificationStaticPublisher::class);
        $this->app->singleton(SupportCenterUserFacingErrors::class);
        $this->app->singleton(SupportCenterAuditLogger::class);
        $this->app->singleton(SupportCenterReleaseCredentialProvider::class);
        $this->app->singleton(SupportCenterSiteCredentialProvider::class);
        $this->app->singleton(SupportCenterCredentialResolver::class);
        $this->app->singleton(SupportCenterCheckOutputSanitizer::class);
        $this->app->singleton(SupportCenterLocalChecks::class);
        $this->app->singleton(SupportCenterRemoteProbe::class);
        $this->app->singleton(SupportCenterReleasePackageCheckService::class);
        $this->app->singleton(SupportCenterPostInstallCheckService::class);
        $this->app->singleton(SupportCenterRuntimeConfig::class);
        $this->app->singleton(SupportCenterConnectivityGuard::class);
        $this->app->singleton(SupportCenterOpenApiTransport::class);
        $this->app->singleton(SupportCenterRequestContextBuilder::class);
        $this->app->singleton(HostSiteOnboardingStatusPresenter::class);
        $this->app->singleton(HostSiteOnboardingLastAttemptStore::class);
        $this->app->singleton(HostSiteOnboardingStatusCacheStore::class);
        $this->app->singleton(HostSiteOnboardingConfirmService::class);
        $this->app->singleton(HostSiteOnboardingStatusQueryService::class);
        $this->app->singleton(HostSiteOnboardingHeartbeatService::class);
        $this->app->singleton(SiteOnboardingCenterDisplay::class);
        $this->app->singleton(HostSystemAuthorizationStatusPresenter::class);
        $this->app->singleton(HostSystemAuthorizationEntitlementPresenter::class);
        $this->app->singleton(HostSystemAuthorizationSnapshotMapper::class);
        $this->app->singleton(HostSystemAuthorizationSyncService::class);
        $this->app->singleton(HostSystemAuthorizationCenterDisplay::class);
        $this->app->singleton(HostSystemAuthorizationSummaryDisplay::class);
        $this->app->singleton(HostPluginCenterDisplay::class);
        $this->app->singleton(HostPluginSettingsPanelRegistry::class);
        $this->app->singleton(HostPluginCenterSettingsPanelFormatter::class);
        $this->app->singleton(HostSystemUpdateEntitlementGate::class);
        $this->app->singleton(HostSystemUpdateCheckSnapshotMapper::class);
        $this->app->singleton(HostSystemUpdateNotificationSyncService::class);
        $this->app->singleton(DberpSystemUpdateNoticeService::class);
        $this->app->singleton(DberpOfficialNewsFeedService::class);
        $this->app->singleton(HostSystemUpdateAutoCheckStateStore::class);
        $this->app->singleton(HostSystemUpdateAutoCheckTriggerService::class);
        $this->app->singleton(HostSystemUpdateCheckService::class);
        $this->app->singleton(HostSystemUpdatePackageMetadataMapper::class);
        $this->app->singleton(HostSystemUpdateDownloadTokenService::class);
        $this->app->singleton(HostSystemUpdatePackageVerifier::class);
        $this->app->singleton(HostSystemUpdatePackageDownloadService::class);
        $this->app->singleton(HostSystemUpdateProtectedPathGuard::class);
        $this->app->singleton(HostSystemUpdateManifestReader::class);
        $this->app->singleton(HostSystemUpdateManifestValidator::class);
        $this->app->singleton(HostSystemUpdateManifestParseService::class);
        $this->app->singleton(HostSystemUpdateExecutionManifestSummarizer::class);
        $this->app->singleton(HostSystemUpdateExecutionLock::class);
        $this->app->singleton(HostSystemUpdateExecutionLogger::class);
        $this->app->singleton(HostSystemUpdateExecutionLogPruningService::class);
        $this->app->singleton(HostSystemUpdateExecutionCreateService::class);
        $this->app->singleton(HostSystemUpdateExecutionStateMachine::class);
        $this->app->singleton(HostSystemUpdateArchiveExtractor::class);
        $this->app->singleton(HostSystemUpdateStagingService::class);
        $this->app->singleton(HostSystemUpdateBackupManifestBuilder::class);
        $this->app->singleton(HostSystemUpdateBackupService::class);
        $this->app->singleton(HostSystemUpdatePrepareService::class);
        $this->app->singleton(HostSystemUpdateExecutionContextResolver::class);
        $this->app->singleton(HostSystemUpdateFileChecksumVerifier::class);
        $this->app->singleton(HostSystemUpdateVendorStrategyResolver::class);
        $this->app->singleton(HostSystemUpdateFileReplacer::class);
        $this->app->singleton(HostSystemUpdateFileDeletionService::class);
        $this->app->singleton(HostSystemUpdateVendorBundleReplacer::class);
        $this->app->singleton(HostSystemUpdateComposerDependencyInstaller::class);
        $this->app->singleton(HostSystemUpdateVendorReplacer::class);
        $this->app->singleton(HostSystemUpdateApplyService::class);
        $this->app->singleton(InProcessHostSystemUpdateArtisanCommandHandler::class);
        $this->app->singleton(InProcessHostSystemUpdateMaintenanceCommandHandler::class);
        $this->app->singleton(ProcessHostSystemUpdateArtisanCommandHandler::class);
        $this->app->singleton(ProcessHostSystemUpdateMaintenanceCommandHandler::class);
        $this->app->singleton(HostSystemUpdateHandlerFactory::class);
        $this->app->singleton(HostSystemUpdateArtisanCommandHandler::class, function ($app): HostSystemUpdateArtisanCommandHandler {
            return $app->make(HostSystemUpdateHandlerFactory::class)->artisanCommandHandler();
        });
        $this->app->singleton(HostSystemUpdateArtisanCommandRunner::class);
        $this->app->singleton(HostSystemUpdateMigrationBatchRunner::class);
        $this->app->singleton(HostSystemUpdateExecutionBatchProgressStore::class);
        $this->app->singleton(HostSystemUpdateExtractBatchRunner::class);
        $this->app->singleton(HostSystemUpdateBackupBatchRunner::class);
        $this->app->singleton(HostSystemUpdateFileReplaceBatchRunner::class);
        $this->app->singleton(HostSystemUpdateVendorReplaceBatchRunner::class);
        $this->app->singleton(HostSystemUpdateBatchPhaseRunner::class);
        $this->app->singleton(HostSystemUpdateMigrationRunner::class);
        $this->app->singleton(HostSystemUpdateMigrationBatchExecutor::class, HostSystemUpdateMigrationBatchProcessRunner::class);
        $this->app->singleton(HostSystemUpdateOpcache::class, NativeHostSystemUpdateOpcache::class);
        $this->app->singleton(HostSystemUpdateCacheCleaner::class);
        $this->app->singleton(HostSystemUpdateVersionWriter::class);
        $this->app->singleton(HostSystemUpdateFinalizeService::class);
        $this->app->singleton(HostSystemUpdateExecutionOrchestrator::class);
        $this->app->singleton(HostSystemUpdateMaintenanceCommandHandler::class, function ($app): HostSystemUpdateMaintenanceCommandHandler {
            return $app->make(HostSystemUpdateHandlerFactory::class)->maintenanceHandler();
        });
        $this->app->singleton(HostSystemUpdateMaintenanceModeService::class);
        $this->app->singleton(HostSystemUpdateDatabaseRollbackBoundary::class);
        $this->app->singleton(HostSystemUpdateFileRestoreService::class);
        $this->app->singleton(HostSystemUpdateEnvRestoreService::class);
        $this->app->singleton(HostSystemUpdateRollbackService::class);
        $this->app->singleton(HostSystemUpdateFailureHandler::class);
    }

    public function boot(): void
    {
        $this->app->make(SettingManagerInterface::class)
            ->registerProvider(new DberpHostSettingDefinitionProvider);

        if ($this->app->make(DBErpInstallStatus::class)->isInstalled()) {
            try {
                $this->app->make(HostSiteVerificationStaticPublisher::class)->publishIfAbsent();
            } catch (\Throwable) {
                // public/.well-known 不可写时不阻断应用启动；可由安装/接入流程或 HTTP 回退路由补齐
            }
        }

        if ($this->app->runningInConsole()) {
            $this->publishes([
                base_path('config/dberp.php') => config_path('dberp.php'),
            ], 'dberp-config');
        }
    }

    private function registerPluginRuntimeBindings(): void
    {
        $this->app->singleton(PluginRegistry::class);
        $this->app->singleton(PluginManifestReader::class);

        $this->app->singleton(PluginRepositoryInterface::class, DberpDatabasePluginRepository::class);
        $this->app->singleton(DberpDatabasePluginRepository::class);

        $this->app->singleton(PluginLifecycleManager::class);
        $this->app->singleton(PluginManagerInterface::class, PluginLifecycleManager::class);

        $this->app->singleton(DberpPluginDiscoveryManager::class);
        $this->app->singleton(DberpOfficialPluginGuard::class);
        $this->app->singleton(DberpPluginBootstrapService::class);
        $this->app->singleton(HostPluginStatusPresenter::class);
        $this->app->singleton(HostPluginSupportCenterContextResolver::class);
        $this->app->singleton(HostPluginEntitlementGate::class);
        $this->app->singleton(HostPluginAvailabilityPresenter::class);
        $this->app->singleton(HostPluginOperationLogger::class);
        $this->app->singleton(HostPluginCatalogService::class);
        $this->app->singleton(HostPluginCatalogInstallStateResolver::class);
        $this->app->singleton(HostPluginClaimCheckService::class);
        $this->app->singleton(HostPluginDownloadTokenService::class);
        $this->app->singleton(HostPluginPackageVerifier::class);
        $this->app->singleton(HostPluginPackageSignatureVerifier::class);
        $this->app->singleton(HostPluginPackageDownloadService::class);
        $this->app->singleton(HostPluginManifestValidator::class);
        $this->app->singleton(HostPluginEntryInstallInvoker::class);
        $this->app->singleton(HostPluginInstallRollback::class);
        $this->app->singleton(HostPluginLifecycleInstallService::class);
        $this->app->singleton(HostPluginInstallOrchestrator::class);
        $this->app->singleton(HostPluginOperationLock::class);
        $this->app->singleton(HostPluginRuntimeCacheCleaner::class);
        $this->app->singleton(HostPluginPermissionResolver::class);
        $this->app->singleton(HostPluginManifestPermissionCatalog::class);
        $this->app->singleton(HostPluginNavigationRegistrar::class);
        $this->app->singleton(HostPluginSettingsRouteResolver::class);
        $this->app->singleton(HostPluginLifecycleService::class);
        $this->app->singleton(HostPluginUpdateStatusPresenter::class);
        $this->app->singleton(HostPluginUpdateCheckService::class);
        $this->app->singleton(HostPluginPackageStagingHelper::class);
        $this->app->singleton(HostPluginReleasePackageSignatureValidator::class);
        $this->app->singleton(HostPluginReleasePackageVerifier::class);
        $this->app->singleton(HostPluginUpdateOrchestrator::class);
    }
}
