<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class NotificationController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $limit = min(max((int) $request->integer('limit', 7), 1), 7);

        $items = User::query()
            ->select('id', 'name', 'email', 'role', 'team', 'created_at')
            ->latest('created_at')
            ->limit($limit)
            ->get()
            ->map(fn (User $user) => [
                'id' => $user->id,
                'title' => "{$user->name} was added",
                'message' => "{$user->email} joined {$user->team} as {$user->role}.",
                'created_at' => $user->created_at,
            ])
            ->values();

        return response()->json([
            'data' => $items,
            'total' => $items->count(),
        ]);
    }
}
