<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Concerns\FlashesEntityMessage;
use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\LoginRequest;
use Illuminate\Http\RedirectResponse;
use Inertia\Inertia;
use Inertia\Response;

class LoginController extends Controller
{
    use FlashesEntityMessage;

    public function index(): Response
    {
        return Inertia::render('auth/Login');
    }

    public function store(LoginRequest $request): RedirectResponse
    {
        $request->authenticate();

        $user = $request->user();

        activity()
            ->causedBy($user)
            ->event('login')
            ->log('Berhasil masuk ke aplikasi');

        $request->session()->regenerate();

        $user?->update(['last_login_at' => now()]);

        $this->flashSuccess('Berhasil masuk. Selamat datang kembali!');

        return redirect()->intended(route('admin.dashboard'));
    }
}
