<?php

declare(strict_types=1);

namespace App\Services;

use App\Contracts\Services\PermissionServiceInterface;
use App\Models\User;
use Illuminate\Support\Collection;
use Spatie\Permission\Models\Role;

final class PermissionService implements PermissionServiceInterface
{
    /**
     * Assign a role to a user
     */
    public function assignRole(User $user, string $role): void
    {
        $user->assignRole($role);
    }

    /**
     * Assign multiple roles to a user
     *
     * @param  array<string>  $roles
     */
    public function assignRoles(User $user, array $roles): void
    {
        $user->assignRole($roles);
    }

    /**
     * Remove a role from a user
     */
    public function removeRole(User $user, string $role): void
    {
        $user->removeRole($role);
    }

    /**
     * Check if user has a specific role
     */
    public function hasRole(User $user, string $role): bool
    {
        return $user->hasRole($role);
    }

    /**
     * Check if user has any of the given roles
     *
     * @param  array<string>  $roles
     */
    public function hasAnyRole(User $user, array $roles): bool
    {
        return $user->hasAnyRole($roles);
    }

    /**
     * Check if user has all of the given roles
     *
     * @param  array<string>  $roles
     */
    public function hasAllRoles(User $user, array $roles): bool
    {
        return $user->hasAllRoles($roles);
    }

    /**
     * Get all roles for a user
     *
     * @return Collection<int, \Spatie\Permission\Models\Role>
     */
    public function getUserRoles(User $user): Collection
    {
        return $user->roles;
    }

    /**
     * Get all available roles in the system
     *
     * @return Collection<int, \Spatie\Permission\Models\Role>
     */
    public function getAllRoles(): Collection
    {
        return Role::all();
    }

    /**
     * Check if user is an admin
     */
    public function isAdmin(User $user): bool
    {
        return $user->hasRole('admin');
    }

    /**
     * Check if user is a guest
     */
    public function isGuest(User $user): bool
    {
        return $user->hasRole('guest');
    }

    /**
     * Check if user is an owner
     */
    public function isOwner(User $user): bool
    {
        return $user->hasRole('owner');
    }

    /**
     * Check if user is a partner
     */
    public function isPartner(User $user): bool
    {
        return $user->hasRole('partner');
    }
}
