<?php

namespace App\Controllers\Auth;

use App\Controllers\BaseController;
use App\Models\UserModel;

class ForgotPassword extends BaseController
{
    public function index()
    {
        return view('auth/forgot_password');
    }

    public function sendResetLink()
    {
        $email = $this->request->getPost('email');
        $userModel = new UserModel();
        
        $user = $userModel->where('email', $email)->first();

        if ($user) {
            // Generate Token (Sederhana untuk demo)
            $token = bin2hex(random_bytes(32));
            
            // Simpan ke database jika Anda memiliki tabel password_resets
            // $db->table('password_resets')->insert([...]);

            // Kirim Email
            // \Config\Services::email()->setTo($email)->setMessage("Token anda: $token")->send();
            
            return redirect()->to('/login')->with('success', 'Link reset password telah dikirim ke email Anda.');
        }

        return redirect()->back()->with('error', 'Email tidak terdaftar.');
    }
}