<?php

namespace App\Controllers;

use App\Models\ModelUser;
use App\Models\Model_Foto_Users;
use Config\Services;
use App\Models\ModelLogmasuk;



class Profil extends BaseController
{
    public function __construct()
    {

        // Memuat model pengguna
        $this->ModelUser = new ModelUser();

        // Cek apakah pengguna sudah login
        if (session()->get('isLoggedIn')) {
            return redirect()->to(base_url() . '/Home/index');  // Jika sudah login, redirect ke dashboard
        }
    }

    // Mengambil data profil pengguna berdasarkan ID

    public function index()
    {
        $id_user = session()->get('id_user');  // Ambil id_user pengguna dari session
        return view('profil/index', ['id_user' => $id_user]);
    }

    public function getProfil($id_user)
    {

        // Ambil id_user dari session
        $id_user = session()->get('id_user');  // Ambil id_user dari session

        // Ambil data pengguna berdasarkan id_user
        $profil = $this->ModelUser->getProfil($id_user);

        if ($profil) {
            return $this->response->setJSON([
                'success' => true,
                'profil' => $profil
            ]);
        } else {
            return $this->response->setJSON([
                'success' => false,
                'message' => 'Profil tidak ditemukan'
            ]);
        }
    }

    public function edit()
    {
        $model = new ModelUser();
        $userId = session()->get('id_user');
        $data['profile'] = $model->find($userId);

        return view('profile/edit', $data);
    }

    // Memperbarui data profil pengguna
    public function updateProfile()
    {
        $id_user = $this->request->getPost('id_user');
        $data = [
            'nama'     => $this->request->getPost('namaProfil'),
            'nohape'        => $this->request->getPost('nohapeProfil'),
            'email'         => $this->request->getPost('emailProfil'),
        ];

        // Validasi Password dan Konfirmasi Password
        $password = $this->request->getPost('password');
        $confirmPassword = $this->request->getPost('confirm_password');
        if (!empty($password)) {
            if ($password !== $confirmPassword) {
                return $this->response->setJSON([
                    'success' => false,
                    'message' => 'Password dan Konfirmasi Password tidak sama'
                ]);
            }
            $data['password'] = password_hash($password, PASSWORD_BCRYPT);
        }

        // Cek apakah ada foto yang diunggah
        $file = $this->request->getFile('profile_image');
        if ($file && $file->isValid()) {
            $newName = $file->getRandomName();
            $file->move(FCPATH . '/public/assets/images/faces/', $newName);
            $data['foto'] = $newName;
        }

        // Perbarui data profil
        $updated = $this->ModelUser->updateProfil($id_user, $data);

        if ($updated) {
            return $this->response->setJSON([
                'success' => true,
                'message' => 'Profil berhasil diperbarui',
                'foto'    => $newName ?? null // Kirim foto baru jika diperbarui
            ]);
        } else {
            return $this->response->setJSON([
                'success' => false,
                'message' => 'Gagal memperbarui data'
            ]);
        }
    }
}
