<?php
// AuthController.php - FULL METHODS yang perlu di-UPDATE

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use App\Models\User;

class AuthController extends Controller
{
    // Method showLogin() tetap sama
    public function showLogin()
    {
        return view('auth.login');
    }

    // GANTI method login() yang sudah ada dengan ini
    public function login(Request $request)
    {
        $credentials = $request->validate([
            'username' => 'required',
            'password' => 'required'
        ]);

        if (Auth::attempt($credentials)) {
            $request->session()->regenerate();
            
            // Redirect berdasarkan role user
            if (Auth::user()->isAdmin()) {
                return redirect()->route('admin.dashboard');
            } elseif (Auth::user()->isPembimbingMagang()) {
                return redirect()->route('pembimbing-magang.dashboard');
            } elseif (Auth::user()->isPembimbingSekolah()) {
                return redirect()->route('pembimbing-sekolah.dashboard');
            } else {
                return redirect()->route('siswa.dashboard');
            }
        }

        return back()->with('error', 'Username atau password salah');
    }

    // Method logout() tetap sama
    public function logout()
    {
        Auth::logout();
        return redirect()->route('login');
    }
}