<?php

namespace App\Domains\Admin\Controllers;

use App\Scopes\CommonController;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class UserListApiController extends CommonController
{
    public function __invoke(Request $request): JsonResponse
    {
        $tenantNames = $this->adminTenants->getTenantNames();
        $users = $this->adminUsers->getListForTenant($this->tenant->slug);

        $mapped = $users->map(function ($user) use ($tenantNames) {
            return [
                'id' => $user->id,
                'firstname' => $user->firstname,
                'lastname' => $user->lastname,
                'nickname' => $user->nickname,
                'local_group' => $user->local_group,
                'local_group_name' => $tenantNames[$user->local_group] ?? $user->local_group,
                'active' => $user->active,
            ];
        });

        return response()->json(['users' => $mapped]);
    }
}
