<?php

namespace App\Http\Controllers\Api\Users;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\JsonResponse;

class UserIndexController extends Controller
{
    public function __invoke(): JsonResponse
    {
        $rows = User::query()
            ->with('roles')
            ->where('tenant_id', request()->user()->tenant_id)
            ->orderBy('full_name')
            ->get()
            ->map(fn (User $user) => [
                'id' => $user->id,
                'fullName' => $user->full_name,
                'email' => $user->email,
                'phone' => $user->phone,
                'status' => $user->status,
                'roles' => $user->roles->pluck('name')->values()->all(),
                'lastLoginAt' => $user->last_login_at?->format('M d, H:i'),
            ])
            ->values();

        return response()->json(['data' => $rows]);
    }
}
