<?php

namespace App\Controllers\Auth;

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

class Profile extends BaseController
{
    public function index()
    {
        $userModel = new UserModel();
        $data['user'] = $userModel->find(session()->get('id_user'));
        return view('auth/profile', $data);
    }

    public function update()
    {
        $userModel = new UserModel();
        $id = session()->get('id_user');
        
        $data = ['nama_lengkap' => $this->request->getPost('nama_lengkap')];
        
        // Update Password jika diisi
        if ($this->request->getPost('password')) {
            $data['password'] = password_hash($this->request->getPost('password'), PASSWORD_BCRYPT);
        }

        $userModel->update($id, $data);

        // Audit Trail
        (new ActivityLogModel())->logActivity($id, "User memperbarui data profil.");

        return redirect()->back()->with('success', 'Profil berhasil diperbarui.');
    }
}