<?php

namespace App\Controllers;

use App\Models\UserModel;
use App\Models\SantriModel;

class AdminController extends BaseController
{
    protected $userModel;
    protected $santriModel;

    public function __construct()
    {
        $this->userModel = new UserModel();
        $this->santriModel = new SantriModel();
    }

    public function dashboard()
    {
        $usersCount = $this->userModel->countAllResults();
        $santriCount = $this->santriModel->countAllResults();

        return view('admin/dashboard', [
            'title'       => 'Dashboard Admin - Sistem Perizinan IBK',
            'usersCount'  => $usersCount,
            'santriCount' => $santriCount,
        ]);
    }

    public function users()
    {
        $users = $this->userModel->orderBy('nama_lengkap', 'ASC')->findAll();

        return view('admin/users', [
            'title' => 'Manajemen Pengguna - Sistem Perizinan IBK',
            'users' => $users,
        ]);
    }

    public function santri()
    {
        $santri = $this->santriModel->db->table('santri')
            ->select('santri.*, users.nama_lengkap as wali_nama')
            ->join('users', 'users.id = santri.wali_id')
            ->orderBy('santri.nama', 'ASC')
            ->get()->getResultArray();

        $walis = $this->userModel->where('role', 'wali')->orderBy('nama_lengkap', 'ASC')->findAll();

        return view('admin/santri', [
            'title'      => 'Manajemen Santri - Sistem Perizinan IBK',
            'santriList' => $santri,
            'walis'      => $walis,
        ]);
    }

    public function usersStore()
    {
        $validation = \Config\Services::validation();
        $validation->setRules([
            'username'     => 'required|is_unique[users.username]|min_length[3]',
            'password'     => 'required|min_length[6]',
            'role'         => 'required|in_list[admin,kesantrian,satpam,wali]',
            'nama_lengkap' => 'required|min_length[3]',
        ]);

        if (!$validation->withRequest($this->request)->run()) {
            return redirect()->to('/admin/users')->with('error', 'Gagal tambah user: ' . implode(' ', $validation->getErrors()));
        }

        $data = [
            'username'     => $this->request->getPost('username'),
            'password'     => password_hash($this->request->getPost('password'), PASSWORD_BCRYPT),
            'role'         => $this->request->getPost('role'),
            'nama_lengkap' => $this->request->getPost('nama_lengkap'),
        ];

        $this->userModel->insert($data);
        return redirect()->to('/admin/users')->with('success', 'User baru berhasil ditambahkan.');
    }

    public function santriStore()
    {
        $validation = \Config\Services::validation();
        $validation->setRules([
            'nis'     => 'required|is_unique[santri.nis]|min_length[3]',
            'nama'    => 'required|min_length[3]',
            'kelas'   => 'required',
            'asrama'  => 'required',
            'wali_id' => 'required|is_not_unique[users.id]',
        ]);

        if (!$validation->withRequest($this->request)->run()) {
            return redirect()->to('/admin/santri')->with('error', 'Gagal tambah santri: ' . implode(' ', $validation->getErrors()));
        }

        $data = [
            'nis'     => $this->request->getPost('nis'),
            'nama'    => $this->request->getPost('nama'),
            'kelas'   => $this->request->getPost('kelas'),
            'asrama'  => $this->request->getPost('asrama'),
            'wali_id' => $this->request->getPost('wali_id'),
        ];

        $this->santriModel->insert($data);
        return redirect()->to('/admin/santri')->with('success', 'Santri baru berhasil ditambahkan.');
    }
}
