<?php

namespace App\Domains\Admin\Controllers;

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

class UserDetailGetController extends CommonController
{
    public function __invoke(int $id, Request $request): JsonResponse
    {
        $user = $this->adminUsers->findById($id);

        $userData = $user->toArray();
        unset($userData['password'], $userData['remember_token'], $userData['activation_token'], $userData['activation_token_expires_at']);

        $tenantNames = $this->adminTenants->getTenantNames();
        $userData['nicename'] = $user->getNicename();
        $userData['fullname'] = $user->getFullName();
        $userData['local_group_name'] = $tenantNames[$user->local_group] ?? $user->local_group;

        return response()->json([
            'user' => $userData,
            'isOwnUser' => auth()->id() === $user->id,
            'isLvTenant' => $this->tenant->slug === 'lv',
            'userRoles' => UserRole::all()->map(fn($role) => ['slug' => $role->slug, 'name' => $role->name]),
            'localGroups' => $this->adminTenants->getActiveLocalGroups()->map(fn($t) => ['slug' => $t->slug, 'name' => $t->name]),
        ]);
    }
}
