<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\UserLogin;
use Illuminate\Support\Facades\Auth;

class UserController extends Controller
{
    /**
     * Halaman keamanan: menampilkan riwayat login user
     */
    public function security(Request $request)
    {
        // Ambil user yang login saat ini
        $user = Auth::user();

        // Ambil login history user, terbaru terlebih dahulu, dengan pagination
        $logins = UserLogin::where('user_id', $user->id)
                    ->orderBy('created_at', 'desc')
                    ->paginate(10); // 10 record per halaman

        // Tambahkan info tambahan jika perlu
        // Contoh: total login, terakhir login
        $totalLogins = UserLogin::where('user_id', $user->id)->count();
        $lastLogin = UserLogin::where('user_id', $user->id)
                        ->orderBy('created_at', 'desc')
                        ->first();

        // Kirim data ke view
        return view('user.keamanan.security', compact('logins', 'totalLogins', 'lastLogin'));
    }
}

