<?php

namespace App\Providers;

use App\Enums\UserRole;
use App\Models\Event as EventModel;
use App\Models\EventPaymentProof;
use App\Models\EventRegistration;
use App\Models\Game;
use App\Models\PlayerGameProfile;
use App\Models\PlayerProfile;
use App\Models\PlayerRecord;
use App\Models\PlayerVideo;
use App\Events\ContentModerated;
use App\Events\PaymentProofReviewed;
use App\Events\RegistrationStatusChanged;
use App\Listeners\SendContentModerationNotification;
use App\Listeners\SendPaymentProofNotification;
use App\Listeners\SendRegistrationStatusNotification;
use App\Models\User;
use App\ViewComposers\AppShellComposer;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        //
    }

    public function boot(): void
    {
        View::composer('layouts.app', AppShellComposer::class);

        Event::listen(RegistrationStatusChanged::class, SendRegistrationStatusNotification::class);
        Event::listen(PaymentProofReviewed::class, SendPaymentProofNotification::class);
        Event::listen(ContentModerated::class, SendContentModerationNotification::class);

        Gate::define('access-admin', fn (User $user) => $user->isAdmin());
        Gate::define('manage-users', fn (User $user) => $user->isAdmin());
        Gate::define('suspend-users', fn (User $user) => in_array($user->role, [UserRole::Admin, UserRole::SuperAdmin], true));
        Gate::define('manage-games', fn (User $user) => $user->isAdmin());
        Gate::define('manage-events', fn (User $user) => $user->isAdmin());
        Gate::define('publish-events', fn (User $user) => $user->isAdmin());
        Gate::define('review-registrations', fn (User $user) => $user->isAdmin());
        Gate::define('review-payment-proofs', fn (User $user) => $user->isAdmin());
        Gate::define('access-payment-proof', fn (User $user, EventPaymentProof $proof) => $user->isAdmin()
            || $proof->eventRegistration?->user_id === $user->id);
        Gate::define('moderate-records', fn (User $user) => $user->isAdmin());
        Gate::define('moderate-videos', fn (User $user) => $user->isAdmin());
        Gate::define('export-participants', fn (User $user) => $user->isAdmin());
        Gate::define('manage-settings', fn (User $user) => $user->role === UserRole::SuperAdmin);
        Gate::define('view-audit-logs', fn (User $user) => $user->isAdmin());

        Gate::policy(PlayerProfile::class, \App\Policies\PlayerProfilePolicy::class);
        Gate::policy(PlayerGameProfile::class, \App\Policies\PlayerGameProfilePolicy::class);
        Gate::policy(PlayerRecord::class, \App\Policies\PlayerRecordPolicy::class);
        Gate::policy(PlayerVideo::class, \App\Policies\PlayerVideoPolicy::class);
        Gate::policy(Game::class, \App\Policies\GamePolicy::class);
        Gate::policy(EventModel::class, \App\Policies\EventPolicy::class);
        Gate::policy(EventRegistration::class, \App\Policies\EventRegistrationPolicy::class);
        Gate::policy(EventPaymentProof::class, \App\Policies\EventPaymentProofPolicy::class);
        Gate::policy(User::class, \App\Policies\UserPolicy::class);
    }
}
