<?php

namespace Kylin\LawCase;

use Illuminate\Support\ServiceProvider;

/**
 * 律所案件管理系统服务提供者
 *
 * 提供案件管理、文档管理、费用管理、日程管理、冲突检索等核心功能。
 */
class LawCaseServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(\Kylin\LawCase\Services\CaseService::class);
        $this->app->singleton(\Kylin\LawCase\Services\ConflictCheckService::class);
        $this->app->singleton(\Kylin\LawCase\Services\DocumentService::class);
        $this->app->singleton(\Kylin\LawCase\Services\ScheduleService::class);
        $this->app->singleton(\Kylin\LawCase\Services\FeeService::class);
        $this->app->singleton(\Kylin\LawCase\Services\ClientService::class);
        $this->app->singleton(\Kylin\LawCase\Services\EncryptionService::class);
        $this->app->singleton(\Kylin\LawCase\Services\DeadlineWarningService::class);
        $this->app->singleton(\Kylin\LawCase\Services\TaskService::class);

        $this->app->singleton(\Kylin\LawCase\Repositories\CaseRepository::class);
        $this->app->singleton(\Kylin\LawCase\Repositories\ClientRepository::class);
        $this->app->singleton(\Kylin\LawCase\Repositories\DocumentRepository::class);
        $this->app->singleton(\Kylin\LawCase\Repositories\ScheduleRepository::class);
        $this->app->singleton(\Kylin\LawCase\Repositories\FeeRepository::class);
    }

    public function boot(): void
    {
        if ($this->app->runningInConsole()) {
            $this->publishes([
                __DIR__ . '/../database/migrations' => database_path('migrations'),
            ], 'kylin-law-case-migrations');

            $this->publishes([
                __DIR__ . '/../config/law-case.php' => config_path('law-case.php'),
            ], 'kylin-law-case-config');

            $this->commands([
                \Kylin\LawCase\Commands\LawCaseInstallCommand::class,
                \Kylin\LawCase\Commands\ScanDeadlinesCommand::class,
            ]);
        }

        $this->registerPolicies();
        $this->registerEventListeners();
    }

    private function registerPolicies(): void
    {
        \Illuminate\Support\Facades\Gate::policy(
            \Kylin\LawCase\Models\CaseModel::class,
            \Kylin\LawCase\Policies\CasePolicy::class
        );
        \Illuminate\Support\Facades\Gate::policy(
            \Kylin\LawCase\Models\Document::class,
            \Kylin\LawCase\Policies\DocumentPolicy::class
        );
        \Illuminate\Support\Facades\Gate::policy(
            \Kylin\LawCase\Models\Client::class,
            \Kylin\LawCase\Policies\ClientPolicy::class
        );
        \Illuminate\Support\Facades\Gate::policy(
            \Kylin\LawCase\Models\Fee::class,
            \Kylin\LawCase\Policies\FeePolicy::class
        );
        \Illuminate\Support\Facades\Gate::policy(
            \Kylin\LawCase\Models\Schedule::class,
            \Kylin\LawCase\Policies\SchedulePolicy::class
        );
    }

    private function registerEventListeners(): void
    {
        \Illuminate\Support\Facades\Event::listen(
            \Kylin\LawCase\Events\CaseCreated::class,
            \Kylin\LawCase\Listeners\RecordCaseCreation::class
        );
        \Illuminate\Support\Facades\Event::listen(
            \Kylin\LawCase\Events\CaseStatusChanged::class,
            \Kylin\LawCase\Listeners\RecordStatusChange::class
        );
        \Illuminate\Support\Facades\Event::listen(
            \Kylin\LawCase\Events\CaseStatusChanged::class,
            \Kylin\LawCase\Listeners\SendCaseNotification::class
        );
        \Illuminate\Support\Facades\Event::listen(
            \Kylin\LawCase\Events\ScheduleReminder::class,
            \Kylin\LawCase\Listeners\SendScheduleReminder::class
        );
    }
}
