<?php

namespace App\Http\Controllers;

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

class AuthPelangganController extends Controller
{
    // Menampilkan Form Login
    public function showLogin()
    {
        return view('auth.login');
    }

    // Memproses Autentikasi Login Pelanggan
    // Memproses Autentikasi Login Pelanggan & Admin
    // Memproses Autentikasi Login Pelanggan & Admin
    // Memproses Autentikasi Login Pelanggan, Admin & Owner
    public function login(Request $request)
    {
        $credentials = $request->validate([
            'email' => 'required|email',
            'password' => 'required',
        ]);

        // 🛡️ 1. JALUR BYPASS ADMIN (Tetap Pertahankan Sistem Lo)
        if ($credentials['email'] === 'admin@gmail.com' && $credentials['password'] === 'password') {
            $adminData = Pelanggan::where('email', 'admin@gmail.com')->first();

            session([
                'admin_logged' => true,
                'admin_id'     => $adminData ? $adminData->id : 1,
                'admin_nama'   => $adminData ? $adminData->nama : 'Admin Utama Mega Mart'
            ]);

            session()->save();
            return redirect('/admin/dashboard')->with('success', 'Bypass Sukses! Selamat datang Admin.');
        }

        if ($credentials['email'] === 'owner@gmail.com' && $credentials['password'] === 'owner123') {

            session([
                'owner_logged' => true,
                'owner_id'     => 999,
                'owner_nama'   => 'Owner Mega Mart'
            ]);

            session()->save();
            return redirect('/admin/laporan')->with('success', 'Bypass Sukses! Selamat datang Owner.');
        }

        // 👤 3. JALUR NORMAL (Untuk Pelanggan Biasa)
        $pelanggan = Pelanggan::where('email', $credentials['email'])->first();

        if ($pelanggan && Hash::check($credentials['password'], $pelanggan->password)) {
            session([
                'pelanggan_logged' => true,
                'pelanggan_id'     => $pelanggan->id,
                'pelanggan_nama'   => $pelanggan->nama
            ]);

            session()->save();
            return redirect('/katalog')->with('success', 'Selamat datang kembali, ' . $pelanggan->nama);
        }

        return back()->withErrors(['email' => 'Email atau password salah!'])->withInput();
    }

    // Menampilkan Form Registrasi
    public function showRegister()
    {
        return view('auth.register');
    }

    // Memproses Registrasi Pelanggan Baru
    public function register(Request $request)
    {
        $request->validate([
            'nama' => 'required|string|max:255',
            'email' => 'required|email|unique:pelanggans,email',
            'password' => 'required|min:6|confirmed',
            'no_hp' => 'required|string|max:20',
            'alamat' => 'required|string',
        ]);

        // Simpan data pelanggan baru ke database
        Pelanggan::create([
            'nama' => $request->nama,
            'email' => $request->email,
            'password' => Hash::make($request->password),
            'no_hp' => $request->no_hp,
            'alamat' => $request->alamat,
            'poin_loyalitas' => 0, // Awal daftar poin bernilai 0 sesuai CRM
        ]);

        return redirect('/login')->with('success', 'Registrasi berhasil! Silakan login.');
    }

    // Proses Logout Pelanggan
    public function logout()
    {
        session()->forget(['pelanggan_logged', 'pelanggan_id', 'pelanggan_nama']);
        return redirect('/login')->with('success', 'Berhasil keluar akun.');
    }
}
