<?php

namespace App\Modules\Settings\Rbac\Services;

use App\Models\User;
use App\Modules\Admin\Tenancy\Models\Tenant;
use App\Modules\HR\Employees\Repositories\EmployeeRepository;
use App\Modules\Settings\Rbac\Repositories\TenantUserRepository;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;

class TenantUserDirectoryService
{
    public function __construct(
        private readonly EmployeeRepository $employees,
        private readonly TenantUserRepository $users,
    ) {}

    public function paginateForTenant(Tenant $tenant, int $perPage = 20): LengthAwarePaginator
    {
        return $this->users->paginate((int) $tenant->id, $perPage);
    }

    public function findTenantUser(Tenant $tenant, int $userId): ?User
    {
        return $this->users->find((int) $tenant->id, $userId);
    }

    /**
     * @return array<int, string>
     */
    public function employeeOptionsForTenant(Tenant $tenant): array
    {
        return $this->employees->listForSelect((int) $tenant->id);
    }

    public function emailTakenInTenant(Tenant $tenant, string $email, ?int $exceptUserId = null): bool
    {
        return $this->users->emailTaken($email, $exceptUserId);
    }

    /**
     * @return array<int, string>
     */
    public function unlinkedUserOptionsForTenant(Tenant $tenant): array
    {
        return $this->users->unlinkedOptions((int) $tenant->id);
    }
}
