<?php
// app/Http/Controllers/AuthController.php

namespace App\Http\Controllers;

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

class AuthController extends Controller
{
    // Show the login form
    public function showLoginForm()
    {
        // User::create([
        //   'username' => 'admin',
          //   'name' => 'admin',
            // 'password' => 'admin123',
        // ]);

        if (Auth::check()) {
            return redirect()->route('home');
        }

        return view('auth.login');
    }
    
    // Handle login submission
    public function login(Request $request)
    {
        $credentials = $request->only('username', 'password');
        if (Auth::attempt($credentials)) {
            $request->session()->regenerate();
            return redirect()->intended('dashboard');
        }
        return redirect()->back()->withInput($request->only('username'))->withErrors([
            'username' => 'Invalid credentials',
        ]);
    }
    // Handle logout
    public function logout(Request $request)
    {
        Auth::logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();

        return redirect('/login');
    }
}
