<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class LoginController extends Controller
{
    // Menampilkan halaman login
    public function index()
    {
        return view('login');
    }

    // Proses autentikasi data dummy
    public function authenticate(Request $request)
    {
        $dummyAdmins = [
            [
                'username' => 'admin',
                'password' => 'admin123',
                'role' => 'admin',
                'nama' => 'Administrator'
            ],
            [
                'username' => 'dimas',
                'password' => 'owner123',
                'role' => 'owner',
                'nama' => 'Dimas Yusuf Ashari'
            ],
            [
                'username' => 'santo',
                'password' => 'dev123',
                'role' => 'pengembang',
                'nama' => 'Santo'
            ]
        ];

        // Ambil input dari form
        $username = $request->input('username');
        $password = $request->input('password');

        // Cek ke semua data dummy
        foreach ($dummyAdmins as $admin) {
            if ($username === $admin['username'] && $password === $admin['password']) {

                // Simpan session
                session([
                    'is_admin' => true,
                    'username' => $admin['username'],
                    'nama' => $admin['nama'],
                    'role' => $admin['role']
                ]);

                return redirect('/dashboard')->with('success', 'Selamat Datang, ' . $admin['nama'] . '!');
            }
        }

        // Jika tidak ada yang cocok
        return back()->with('error', 'Username atau Password salah!');
    }
}