<?php

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
{
    // ================= LOGIN FORM =================
    public function loginForm()
    {
        return view('auth.login');
    }

    // ================= PROSES LOGIN =================
    public function login(Request $request)
    {
        // VALIDASI
        $request->validate([
            'email' => 'required|email',
            'password' => 'required'
        ]);

        $credentials = $request->only('email', 'password');

        if (Auth::attempt($credentials)) {

            // 🔥 REDIRECT SESUAI ROLE
            if(auth()->user()->role == 'admin'){
                return redirect('/admin/dashboard');
            }

            if(auth()->user()->role == 'penyuluh'){
                return redirect('/penyuluh/dashboard');
            }

            return redirect('/petani/dashboard');
        }

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

    // ================= REGISTER FORM =================
    public function registerForm()
    {
        return view('auth.register');
    }

    // ================= PROSES REGISTER =================
    public function register(Request $request)
    {
        // VALIDASI
        $request->validate([
            'name' => 'required|min:3',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:6|confirmed'
        ]);

        // SIMPAN USER
        User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
            'role' => 'petani' // default role
        ]);

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

    // ================= FORM LUPA PASSWORD =================
    public function forgotForm()
    {
        return view('auth.forgot');
    }

    // ================= PROSES RESET PASSWORD =================
    public function resetPassword(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
            'password' => 'required|min:6|confirmed'
        ]);

        $user = User::where('email', $request->email)->first();

        if (!$user) {
            return back()->with('error', 'Email tidak ditemukan');
        }

        $user->update([
            'password' => Hash::make($request->password)
        ]);

        return redirect('/login')->with('success', 'Password berhasil diubah');
    }

    // ================= UPDATE PROFIL =================
    public function updateProfile(Request $request)
    {
        $user = Auth::user();

        $request->validate([
            'name' => 'required',
            'email' => 'required|email',
            'photo' => 'image|mimes:jpg,jpeg,png|max:2048'
        ]);

        // upload foto
        if ($request->hasFile('photo')) {
            $path = $request->file('photo')->store('profiles', 'public');
            $user->photo = $path;
        }

        $user->name = $request->name;
        $user->email = $request->email;

        $user->save();

        return back()->with('success', 'Profil berhasil diupdate');
    }

    // ================= LOGOUT =================
    public function logout()
    {
        Auth::logout();
        return redirect('/');
    }
}