<?php

namespace App\Providers;

use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\Rules\Password;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        $this->configureDefaults();
        $this->defineGates();
    }

    /**
     * Gates privilege berbasis grup (RPD FR-AUT-07, BR-10, BR-12).
     * Admin/developer dapat mengelola seluruh data; member hanya data miliknya.
     */
    protected function defineGates(): void
    {
        Gate::define('manage-all', fn ($user) => $user->isAdmin());

        Gate::define('manage-users', fn ($user) => $user->isAdmin());

        Gate::define('manage-billing', fn ($user) => $user->isAdmin());

        Gate::define('manage-master', fn ($user) => $user->isAdmin());

        // Campaign: admin lihat semua, member lihat miliknya.
        Gate::define('view-campaign', fn ($user, $ad = null) => $user->isAdmin() || optional($ad)->user_id === $user->id);

        Gate::define('manage-campaign', fn ($user, $ad = null) => $user->isAdmin() || optional($ad)->user_id === $user->id);
    }

    /**
     * Configure default behaviors for production-ready applications.
     */
    protected function configureDefaults(): void
    {
        Date::use(CarbonImmutable::class);

        DB::prohibitDestructiveCommands(
            app()->isProduction(),
        );

        Password::defaults(fn (): ?Password => app()->isProduction()
            ? Password::min(12)
                ->mixedCase()
                ->letters()
                ->numbers()
                ->symbols()
                ->uncompromised()
            : null,
        );
    }
}
