<?php

namespace App\Services;

use App\Models\User;

class UserAccessService
{
    public function isTotallyBanned(User $user): bool
    {
        return (bool) ($user->is_banned ?? false);
    }

    public function serviceIsBanned(User $user, string $serviceKey): bool
    {
        $bannedServices = array_map('strval', (array) ($user->banned_services_json ?? []));

        return in_array($serviceKey, $bannedServices, true);
    }

    public function assertCanUse(User $user, string $serviceKey, string $message): void
    {
        if ($this->isTotallyBanned($user)) {
            abort(403, 'Your account is banned. Please contact support.');
        }

        if ($this->serviceIsBanned($user, $serviceKey)) {
            abort(403, $message);
        }
    }
}
