<?php

namespace App\Providers;

use App\Addons\Services\AddonRegistrar;
use App\Http\View\Composers\UpdateNotificationComposer;
use App\Models\Quotation;
use App\Models\Receipt;
use App\Policies\QuotationPolicy;
use App\Policies\ReceiptPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider;
use Illuminate\Support\Facades\View;

class AppServiceProvider extends AuthServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array<class-string, class-string>
     */
    protected $policies = [
        Quotation::class => QuotationPolicy::class,
        Receipt::class => ReceiptPolicy::class,
    ];

    /**
     * Register any application services.
     */
    public function register(): void
    {
        // Register enabled addons
        $this->app->singleton(AddonRegistrar::class);
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        // Register policies
        $this->registerPolicies();

        // Force HTTPS in production
        if (config('app.env') === 'production' && config('app.url') && str_starts_with(config('app.url'), 'https://')) {
            \Illuminate\Support\Facades\URL::forceScheme('https');
        }

        // Register view composer for update notifications
        View::composer('layouts.admin', UpdateNotificationComposer::class);

        // Register addons after all service providers are loaded
        $this->app->booted(function () {
            $addonRegistrar = $this->app->make(AddonRegistrar::class);
            $addonRegistrar->registerAddons();
        });
    }
}
