<?php

namespace App\Services;

use App\Models\LoginAttempt;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Str;

class LoginAttemptService
{
    public function record(Request $request, string $status, ?string $identifier = null, ?User $user = null): LoginAttempt
    {
        return LoginAttempt::create([
            'user_id' => $user?->getKey(),
            'login_identifier' => $identifier === null ? null : Str::limit($identifier, 255, ''),
            'status' => $status,
            'ip_address' => $request->ip(),
            'user_agent' => $request->userAgent(),
            'attempted_at' => now(),
        ]);
    }
}
