<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AdminAuthController extends Controller
{
    public function showLoginForm()
    {
        return view('auth.admin-login'); // nanti kita bikin file ini
    }

    public function login(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
            'password' => 'required',
        ]);

        $credentials = $request->only('email', 'password');

        if (Auth::guard('admin')->attempt($credentials)) {
            $request->session()->regenerate();
            return redirect()->route('admin.index');
        }

        return back()->withErrors([
            'email' => 'Email atau password salah.',
        ])->withInput();
    }

    public function logout(Request $request)
{
    Auth::guard('admin')->logout();

    // 🧹 Hapus sesi dan regenerasi token supaya aman
    $request->session()->invalidate();
    $request->session()->regenerateToken();

    // ✅ Arahkan balik ke halaman login admin
    return redirect()->route('admin.login');
}

}
