<?php

namespace App\Controllers\Master;

use App\Controllers\BaseController;
use App\Models\MajorModel;

class Major extends BaseController
{
    protected $major;

    public function __construct()
    {
        $this->major = new MajorModel();
    }

    public function index()
    {
        return view('master/major/index');
    }

    public function data()
    {
        return $this->response->setJSON([
            'data' => $this->major->orderBy('id', 'DESC')->findAll()
        ]);
    }

    public function create()
    {
        return view('master/major/create');
    }

    public function store()
    {
        $this->major->insert([
            'code' => $this->request->getPost('code'),
            'name' => $this->request->getPost('name'),
            'level' => $this->request->getPost('level'),
            'quota' => $this->request->getPost('quota'),
            'description' => $this->request->getPost('description'),
            'is_active' => 1
        ]);

        return redirect()->to('/master/major')->with('success', 'Jurusan berhasil ditambahkan');
    }

    public function edit($id)
    {
        $major = $this->major->find($id);

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

        return view('master/major/edit', [
            'major' => $major,
            'locked' => $this->isUsedInPPDB($id)
        ]);
    }

    public function update($id)
    {
        $major = $this->major->find($id);
        $locked = $this->isUsedInPPDB($id);

        if (!$major) {
            return redirect()->to('master/major');
        }

        // 🔥 JIKA SUDAH DIPAKAI PPDB
        if ($locked) {
            return redirect()->back()
                ->with('error', 'Jurusan sudah digunakan pada PPDB, tidak dapat diubah');
        }

        $this->major->update($id, [
            'name' => $this->request->getPost('name'),
            'level' => $this->request->getPost('level'),
            'quota' => $this->request->getPost('quota'),
            'description' => $this->request->getPost('description'),
            'is_active' => $this->request->getPost('is_active')
        ]);

        return redirect()->to('master/major')
            ->with('success', 'Jurusan berhasil diperbarui');
    }
    private function isUsedInPPDB($majorId)
    {
        $major = $this->major->find($majorId);

        if (!$major) {
            return false;
        }

        return db_connect()
            ->table('ppdb_registrations')
            ->where('school_level', $major['level']) // 🔥 SESUAI DATA
            ->countAllResults() > 0;
    }

    public function delete($id)
    {
        $major = $this->major->find($id);

        if (!$major) {
            return redirect()->back()
                ->with('error', 'Data jurusan tidak ditemukan.');
        }

        // 🔒 cek apakah level jurusan sudah dipakai PPDB
        $used = db_connect()
            ->table('ppdb_registrations')
            ->where('school_level', $major['level'])
            ->countAllResults();

        if ($used > 0) {
            return redirect()->back()
                ->with('error', 'Jurusan tidak dapat dihapus karena sudah digunakan PPDB.');
        }

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

        return redirect()->to('master/major')
            ->with('success', 'Jurusan berhasil dihapus.');
    }

}
