<?php

declare(strict_types=1);

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\Services\DberpSystemUpdateNoticeService;
use App\Services\Install\DBErpInstallStatus;
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\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\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\HostSystemUpdateMigrationRunner;
use App\Services\SupportCenter\HostSystemUpdateNotificationSyncService;
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\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\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\HostSystemAuthorizationCenterDisplay;
use App\Support\HostSystemAuthorizationSummaryDisplay;
use App\Support\SiteOnboardingCenterDisplay;
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->app->singleton(
            DberpShortcutRegistry::class,
            static fn (): DberpShortcutRegistry => new DberpShortcutRegistry,
        );

        $this->app->singleton(BusinessOverviewMetricService::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(HostSystemUpdateEntitlementGate::class);
        $this->app->singleton(HostSystemUpdateCheckSnapshotMapper::class);
        $this->app->singleton(HostSystemUpdateNotificationSyncService::class);
        $this->app->singleton(DberpSystemUpdateNoticeService::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(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(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(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');
        }
    }
}
