<?php

namespace App\Http\Middleware;

use App\Models\DailyUserActivity;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * يسجّل نشاطاً يومياً لكل مستخدم مصادق (DAU) — خفيف وآمن للإنتاج.
 */
final class RecordDailyActiveUser
{
    public function handle(Request $request, Closure $next): Response
    {
        $response = $next($request);

        $user = $request->user();
        if ($user === null) {
            return $response;
        }

        $today = now()->toDateString();
        DailyUserActivity::query()->firstOrCreate(
            [
                'user_id' => $user->id,
                'activity_date' => $today,
            ],
            []
        );

        return $response;
    }
}
