<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Services\ShjFourCService;
use App\Services\ShjMailSendService;
use App\Services\ShjNineService;
use App\Services\ShjTenService;
use App\Services\ShjSixService;
use Illuminate\Support\Facades\DB;
use App\Services\MenuAccessService;

class AppServiceProvider extends ServiceProvider
{
    /**
     * アプリケーションサービスの登録
     */
    public function register(): void
    {
        // SHJ-4C室割当処理サービスを登録
        $this->app->singleton(ShjFourCService::class, function ($app) {
            return new ShjFourCService(
                $app->make(\App\Models\Park::class),
                $app->make(\App\Models\RegularContract::class),
                $app->make(\App\Models\Batch\BatchLog::class)
            );
        });

        // SHJメール送信処理サービスを登録
        $this->app->singleton(ShjMailSendService::class, function ($app) {
            return new ShjMailSendService(
                $app->make(\App\Models\MailTemplate::class),
                $app->make(\App\Models\Batch\BatchLog::class)
            );
        });

        // SHJ-9売上集計処理サービスを登録
        $this->app->singleton(ShjNineService::class, function ($app) {
            return new ShjNineService(
                $app->make(\App\Models\Park::class),
                $app->make(\App\Models\RegularContract::class),
                $app->make(\App\Models\EarningsSummary::class),
                $app->make(\App\Models\Psection::class),
                $app->make(\App\Models\Batch\BatchLog::class),
                $app->make(\App\Models\OperatorQue::class)
            );
        });

        // SHJ-10財政年度売上集計処理サービスを登録
        $this->app->singleton(ShjTenService::class, function ($app) {
            return new ShjTenService(
                $app->make(\App\Models\Park::class),
                $app->make(\App\Models\RegularContract::class),
                $app->make(\App\Models\EarningsSummary::class),
                $app->make(\App\Models\Psection::class),
                $app->make(\App\Models\Batch\BatchLog::class),
                $app->make(\App\Models\OperatorQue::class)
            );
        });

        // SHJ-6サーバ死活監視処理サービスを登録
        $this->app->singleton(ShjSixService::class, function ($app) {
            return new ShjSixService(
                $app->make(\App\Models\Device::class),
                $app->make(\App\Models\HardwareCheckLog::class),
                $app->make(\App\Models\PrintJobLog::class),
                $app->make(\App\Models\Batch\BatchLog::class),
                $app->make(\App\Models\OperatorQue::class),
                $app->make(\App\Services\ShjMailSendService::class)
            );
        });
    }

    /**
     * アプリケーションサービスの初期化
     */
    public function boot(): void
    {
        view()->composer('layouts.app', function($view){

            // 未対応のみ集計
            $stats = DB::table('operator_que')
                ->selectRaw("
                    SUM(CASE WHEN que_status = 1 AND que_class < 100 THEN 1 ELSE 0 END) AS task_untreated,
                    MAX(CASE WHEN que_status = 1 AND que_class < 100 THEN created_at END) AS task_latest,
                    SUM(CASE WHEN que_status = 1 AND que_class > 99 THEN 1 ELSE 0 END) AS hard_untreated,
                    MAX(CASE WHEN que_status = 1 AND que_class > 99 THEN created_at END) AS hard_latest
                ")
                ->first();

            // 変数名は互換維持（内容は未対応件数）
            $taskCount  = (int)($stats->task_untreated ?? 0);
            $hardCount  = (int)($stats->hard_untreated ?? 0);
            $taskLatest = $stats->task_latest ?? null;
            $hardLatest = $stats->hard_latest ?? null;

            // ドロップダウン最新5件 も未対応のみ
            $latestTasks = DB::table('operator_que')
                ->where('que_status',1)
                ->where('que_class','<',100)
                ->orderByDesc('created_at')
                ->limit(5)
                ->get();

            $latestHards = DB::table('operator_que')
                ->where('que_status',1)
                ->where('que_class','>',99)
                ->orderByDesc('created_at')
                ->limit(5)
                ->get();

        $menuAccessService = app(MenuAccessService::class);
        $isSorin = $menuAccessService->isSorin();
        $visibleCities = $menuAccessService->visibleCities();
    
            $view->with(compact(
                'taskCount','taskLatest',
                'hardCount','hardLatest',
                'latestTasks','latestHards',
                'isSorin', 'visibleCities'
            ));
        });
    }
}
