<?php

namespace App\Controllers\Master;

use App\Controllers\BaseController;
use App\Models\Master\SubjectModel;

class Subject extends BaseController
{
    protected $subject;

    public function __construct()
    {
        $this->subject = new SubjectModel();
    }

    // ===============================
    // INDEX (LIST DATA)
    // ===============================
    public function index()
    {
        return view('master/subject/index', [
            'title' => 'Master Mata Pelajaran'
        ]);
    }

    // ===============================
    // DATA (DATATABLES)
    // ===============================
    public function data()
    {
        $data = $this->subject
            ->orderBy('nama_mapel', 'ASC')
            ->findAll();

        return $this->response->setJSON([
            'data' => $data
        ]);
    }

    // ===============================
    // FORM CREATE
    // ===============================
    public function create()
    {
        return view('master/subject/create', [
            'title' => 'Tambah Mata Pelajaran'
        ]);
    }

    // ===============================
    // PROSES SIMPAN
    // ===============================
    public function store()
    {
        $rules = [
            'kode_mapel' => 'required|is_unique[master_subject.kode_mapel]',
            'nama_mapel' => 'required|min_length[3]',
            'kelompok' => 'required'
        ];

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

        $this->subject->insert([
            'kode_mapel' => strtoupper($this->request->getPost('kode_mapel')),
            'nama_mapel' => $this->request->getPost('nama_mapel'),
            'kelompok' => $this->request->getPost('kelompok'),
            'is_active' => $this->request->getPost('is_active') ?? 1,
            'created_by' => session('user_id') ?? null,
            'created_at' => date('Y-m-d H:i:s')
        ]);

        return redirect()->to(site_url('master/subject'))
            ->with('success', 'Mata pelajaran berhasil ditambahkan');
    }

    // ===============================
// FORM EDIT
// ===============================
    public function edit($id)
    {
        $subject = $this->subject->find($id);

        if (!$subject) {
            throw new \CodeIgniter\Exceptions\PageNotFoundException('Data mapel tidak ditemukan');
        }

        return view('master/subject/edit', [
            'title' => 'Edit Mata Pelajaran',
            'subject' => $subject
        ]);
    }

    // ===============================
// PROSES UPDATE
// ===============================
    public function update($id)
    {
        $subject = $this->subject->find($id);

        if (!$subject) {
            return redirect()->to(site_url('master/subject'))
                ->with('error', 'Data tidak ditemukan');
        }

        $rules = [
            'kode_mapel' => "required|is_unique[master_subject.kode_mapel,id,{$id}]",
            'nama_mapel' => 'required|min_length[3]',
            'kelompok' => 'required'
        ];

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

        $this->subject->update($id, [
            'kode_mapel' => strtoupper($this->request->getPost('kode_mapel')),
            'nama_mapel' => $this->request->getPost('nama_mapel'),
            'kelompok' => $this->request->getPost('kelompok'),
            'is_active' => $this->request->getPost('is_active'),
            'updated_at' => date('Y-m-d H:i:s')
        ]);

        return redirect()->to(site_url('master/subject'))
            ->with('success', 'Mata pelajaran berhasil diperbarui');
    }

    // ===============================
// DELETE DATA
// ===============================
    public function delete($id)
    {
        $subject = $this->subject->find($id);

        if (!$subject) {
            return redirect()->to(site_url('master/subject'))
                ->with('error', 'Data mata pelajaran tidak ditemukan');
        }

        $this->subject->delete($id);

        return redirect()->to(site_url('master/subject'))
            ->with('success', 'Mata pelajaran berhasil dihapus');
    }

}
