<?php

namespace App\Controllers\PPDB;

use App\Controllers\BaseController;
use App\Models\PpdbSiswaModel;

class Account extends BaseController
{
    protected $ppdb;

    public function __construct()
    {
        $this->ppdb = new PpdbSiswaModel();
    }

    // ==========================
    // FORM LOGIN
    // ==========================
    public function login()
    {
        return view('ppdb/account/login');
    }

    // ==========================
    // PROSES LOGIN
    // ==========================
    public function loginProcess()
    {
        $email = $this->request->getPost('email');
        $password = $this->request->getPost('password');

        $user = $this->ppdb
            ->select('id, nama_siswa, email, password, status')
            ->where('email', $email)
            ->first();

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

        if ((int) $user['status'] !== 1) {
            return redirect()->back()->with('error', 'Akun belum aktif');
        }

        if ($password !== $user['password']) {
            return redirect()->back()->with('error', 'Password salah');
        }

        session()->set([
            'ppdb_user_id' => $user['id'],
            'ppdb_email' => $user['email'],
            'ppdb_name' => $user['nama_siswa'],
            'ppdb_logged_in' => true,
        ]);

        return redirect()->to('/ppdb/dashboard');
    }

    // ==========================
    // LOGOUT
    // ==========================
    public function logout()
    {
        session()->destroy();
        return redirect()->to('/ppdb/login');
    }

    // ==========================
    // FORM GANTI PASSWORD
    // ==========================
    public function password()
    {
        return view('ppdb/account_password');
    }

    // ==========================
    // PROSES GANTI PASSWORD
    // ==========================
    public function updatePassword()
    {
        if (!session()->get('ppdb_logged_in')) {
            return redirect()->to('/ppdb/login');
        }

        $rules = [
            'password_lama' => 'required',
            'password_baru' => 'required|min_length[4]',
            'konfirmasi' => 'required|matches[password_baru]',
        ];

        if (!$this->validate($rules)) {
            return redirect()->back()
                ->withInput()
                ->with('error', $this->validator->getErrors());
        }

        $user = $this->ppdb
            ->where('id', session('ppdb_user_id'))
            ->first();

        if (!$user) {
            return redirect()->to('/ppdb/login')
                ->with('error', 'Sesi login tidak valid');
        }

        // ✅ PLAIN TEXT CHECK
        if ($this->request->getPost('password_lama') !== $user['password']) {
            return redirect()->back()
                ->with('error', 'Password lama tidak sesuai');
        }

        // ✅ SIMPAN PLAIN TEXT
        $this->ppdb->update($user['id'], [
            'password' => $this->request->getPost('password_baru'),
        ]);

        return redirect()->to('/ppdb/dashboard')
            ->with('success', 'Password berhasil diperbarui');
    }

}
