<?php

namespace App\Providers;

use App\Services\Attendance\AttendancePolicyService;
use App\Services\Attendance\AttendanceRecordSyncService;
use App\Services\Attendance\SemesterRangeService;
use App\Services\Attendance\StudentAttendanceWriterService;
use App\Services\Attendance\TeacherAttendanceSubmissionService;
use App\Services\Invoices\InvoiceConfigService;
use App\Services\Invoices\InvoiceGradeService;
use App\Services\Invoices\InvoiceTuitionService;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Routing\Redirector;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton(AttendancePolicyService::class);
        $this->app->singleton(SemesterRangeService::class);
        $this->app->singleton(AttendanceRecordSyncService::class);
        $this->app->singleton(StudentAttendanceWriterService::class);
        $this->app->singleton(TeacherAttendanceSubmissionService::class);

        $this->app->singleton(InvoiceConfigService::class);
        $this->app->bind(InvoiceGradeService::class, function ($app) {
            $config = $app->make(InvoiceConfigService::class);
            return new InvoiceGradeService($config->getGradeFee());
        });
        $this->app->bind(InvoiceTuitionService::class, function ($app) {
            $config = $app->make(InvoiceConfigService::class);
            return new InvoiceTuitionService(
                $app->make(InvoiceGradeService::class),
                $config->getGradeFee(),
                $config->getFirstStudentFee(),
                $config->getSecondStudentFee(),
                $config->getYouthFee(),
                $config->getTimezone()
            );
        });
    }

    public function boot(): void
    {
        $this->app->resolving(FormRequest::class, function (FormRequest $request, $app): void {
            $request->setContainer($app)->setRedirector($app->make(Redirector::class));
        });
    }
}
