<?php

namespace App\Http\Controllers;

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

class AdminMonitoringController extends Controller
{
    public function showLoginForm()
    {
        return view('monitoring.login');
    }

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

        // Ubah dari 'monitor' ke 'monitoring' sesuai config/auth.php
        if (Auth::guard('monitoring')->attempt($credentials, $request->filled('remember'))) {
            $request->session()->regenerate();

            return redirect()->intended('/pulse');
        }

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

    public function logout(Request $request)
    {
        // Ubah dari 'monitor' ke 'monitoring'
        Auth::guard('monitoring')->logout();

        $request->session()->invalidate();
        $request->session()->regenerateToken();

        return redirect('/monitoring/login');
    }
}
