<?php

namespace App\Http\Controllers;

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

class AuthController extends Controller
{
    // 🔹 tampilkan halaman login
    public function loginview()
    {
        return view('auth.login');
    }

    // 🔹 proses login
    public function login(Request $request)
    {
        $request->validate([
            'userid' => 'required',
            'password' => 'required'
        ]);

        // 🔹 cari user
        $user = User::where('userid', $request->userid)->first();

        // 🔥 bandingkan langsung plaintext
        if ($user && $request->password === $user->pass) {

            Auth::login($user); // login manual
            $request->session()->regenerate();

            return redirect('/registrasi');
        }

        return back()->with('error', 'UserID atau Password salah');
    }
    // 🔹 logout
    public function logout(Request $request)
    {
        Auth::logout(); // hapus login
        $request->session()->invalidate(); // hapus session
        $request->session()->regenerateToken(); // token baru

        return redirect('/');
    }
}