<?php

namespace Kylin\Payment\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Container\Container;
use Kylin\Payment\Http\Middleware\MiddlewareServiceProvider;
use Kylin\Payment\Payment;
use Kylin\Payment\PaymentManager;
use Kylin\Payment\Services\ConfigCacheService;
use Kylin\Payment\Services\PaymentRecordService;
use Kylin\Payment\Services\PaymentCallbackLogService;
use Kylin\Payment\Services\PaymentStatisticsService;
use Kylin\Payment\Services\LanguageService;
use Kylin\Payment\Services\QueueService;
use Kylin\Payment\Services\PaymentScenarioService;
use Kylin\Payment\Services\SecurityService;
use Kylin\Payment\Services\VersionManagerService;
use Kylin\Payment\Services\TestToolService;
use Kylin\Payment\Services\NotificationService;
use Kylin\Payment\Services\QrCodeService;
use Kylin\Payment\Services\PaymentLinkService;
use Kylin\Payment\Services\ProfitSharingService;
use Kylin\Payment\Services\CouponService;
use Kylin\Payment\Services\ReportService;
use Kylin\Payment\Services\PaymentRetryService;
use Kylin\Payment\Services\PaymentTimeoutService;
use Kylin\Payment\Services\PaymentPreCreateService;
use Kylin\Payment\Services\PaymentDataMaskingService;
use Kylin\Payment\Services\PaymentRateLimitService;
use Kylin\Payment\Services\PaymentWebhookService;
use Kylin\Payment\Services\PaymentIntelligentRoutingService;
use Kylin\Payment\Services\PaymentBatchOperationService;
use Kylin\Payment\Services\PaymentRiskControlService;

/**
 * 支付服务提供者
 */
class PaymentServiceProvider extends ServiceProvider
{
    /**
     * 注册服务
     */
    public function register(): void
    {
        // 合并配置
        $this->mergeConfigFrom(
            __DIR__.'/../../config/payment.php', 'payment'
        );

        // 注册服务
        $this->registerServices();

        // 注册Payment实例到容器
        $this->app->singleton('kylin.payment', fn (Container $app) => $app->make(Payment::class));

        // 注册门面
        $this->app->alias('kylin.payment', Payment::class);

        // 注册中间件服务提供者
        $this->app->register(MiddlewareServiceProvider::class);
    }

    /**
     * 注册服务
     */
    protected function registerServices(): void
    {
        // 注册配置缓存服务
        $this->app->singleton(ConfigCacheService::class, fn (Container $app) => new ConfigCacheService($app));

        // 注册支付管理器
        $this->app->singleton(PaymentManager::class, fn (Container $app) => new PaymentManager(
            $app,
            $app->make(ConfigCacheService::class)
        ));

        // 注册服务类
        $services = [
            PaymentRecordService::class,
            PaymentCallbackLogService::class,
            PaymentStatisticsService::class,
            LanguageService::class,
            QueueService::class,
            PaymentScenarioService::class,
            SecurityService::class,
            VersionManagerService::class,
            TestToolService::class,
            NotificationService::class,
            QrCodeService::class,
            PaymentLinkService::class,
            ProfitSharingService::class,
            CouponService::class,
            ReportService::class,
            PaymentRetryService::class,
            PaymentTimeoutService::class,
            PaymentPreCreateService::class,
            PaymentDataMaskingService::class,
            PaymentRateLimitService::class,
            PaymentWebhookService::class,
            PaymentIntelligentRoutingService::class,
            PaymentBatchOperationService::class,
            PaymentRiskControlService::class,
        ];

        foreach ($services as $service) {
            $this->app->singleton($service, fn (Container $app) => new $service($app));
        }

        // 注册Payment类
        $this->app->singleton(Payment::class, fn (Container $app) => new Payment(
            $app,
            $app->make(PaymentManager::class),
            $app->make(PaymentRecordService::class),
            $app->make(PaymentCallbackLogService::class),
            $app->make(PaymentStatisticsService::class),
            $app->make(LanguageService::class),
            $app->make(ConfigCacheService::class),
            $app->make(QueueService::class),
            $app->make(PaymentScenarioService::class),
            $app->make(SecurityService::class),
            $app->make(VersionManagerService::class),
            $app->make(TestToolService::class),
            $app->make(NotificationService::class),
            $app->make(QrCodeService::class),
            $app->make(PaymentLinkService::class),
            $app->make(ProfitSharingService::class),
            $app->make(CouponService::class),
            $app->make(ReportService::class),
            $app->make(PaymentRetryService::class),
            $app->make(PaymentTimeoutService::class),
            $app->make(PaymentPreCreateService::class),
            $app->make(PaymentDataMaskingService::class),
            $app->make(PaymentRateLimitService::class),
            $app->make(PaymentWebhookService::class),
            $app->make(PaymentIntelligentRoutingService::class),
            $app->make(PaymentBatchOperationService::class),
            $app->make(PaymentRiskControlService::class)
        ));
    }

    /**
     * 引导服务
     */
    public function boot(): void
    {
        // 发布配置文件
        $this->publishes([
            __DIR__.'/../../config/payment.php' => config_path('payment.php'),
        ], 'config');

        // 发布迁移文件
        $this->publishes([
            __DIR__.'/../../database/migrations/' => database_path('migrations'),
        ], 'migrations');

        // 发布语言文件
        $this->publishes([
            __DIR__.'/../../resources/lang/' => resource_path('lang/vendor/kylin-payment'),
        ], 'lang');

        // 发布视图文件
        $this->publishes([
            __DIR__.'/../../resources/views/' => resource_path('views/vendor/kylin-payment'),
        ], 'views');

        // 加载语言文件
        $this->loadTranslationsFrom(__DIR__.'/../../resources/lang/', 'kylin-payment');

        // 加载视图文件
        $this->loadViewsFrom(__DIR__.'/../../resources/views/', 'kylin-payment');

        // 加载路由
        $this->loadRoutesFrom(__DIR__.'/../Http/Routes/dashboard.php');
        $this->loadRoutesFrom(__DIR__.'/../Http/Routes/report.php');
    }
}
