<?php

namespace App\Modules\Settings\Rbac\Repositories;

use App\Modules\Settings\Rbac\Models\Role;
use Illuminate\Database\Eloquent\Collection;

class RoleRepository
{
    public function forTenant(int $tenantId): Collection
    {
        return Role::query()
            ->where('tenant_id', $tenantId)
            ->withCount(['permissions', 'users'])
            ->orderBy('name')
            ->get();
    }

    public function activeForTenant(int $tenantId): Collection
    {
        return Role::query()
            ->where('tenant_id', $tenantId)
            ->where('is_active', true)
            ->orderBy('name')
            ->get();
    }

    public function findForTenant(int $tenantId, int $roleId): ?Role
    {
        return Role::query()
            ->where('tenant_id', $tenantId)
            ->where('id', $roleId)
            ->with('permissions')
            ->withCount('users')
            ->first();
    }

    public function findBySlug(int $tenantId, string $slug): ?Role
    {
        return Role::query()
            ->where('tenant_id', $tenantId)
            ->where('slug', $slug)
            ->where('is_active', true)
            ->first();
    }

    public function slugExists(int $tenantId, string $slug): bool
    {
        return Role::withTrashed()
            ->where('tenant_id', $tenantId)
            ->where('slug', $slug)
            ->exists();
    }

    public function hasAssignedUsers(Role $role): bool
    {
        return $role->users()->exists();
    }

    /**
     * @param  array<string, mixed>  $data
     */
    public function create(array $data): Role
    {
        return Role::query()->create($data);
    }

    /**
     * @param  array<string, mixed>  $data
     */
    public function update(Role $role, array $data): Role
    {
        $role->update($data);

        return $role;
    }

    public function delete(Role $role): void
    {
        $role->delete();
    }

    public function refresh(Role $role): Role
    {
        return $role->fresh(['permissions'])->loadCount('users');
    }
}
