<?php

namespace App\Controllers;

use App\Models\KelasPerkuliahanModel;
use App\Models\DosenPengajarKelasModel;
use App\Models\ProgramStudiModel;
use App\Models\MataKuliahModel;
use App\Models\DosenModel;
use App\Services\NeoFeederService;

use App\Models\KurikulumModel;

class KelasPerkuliahan extends BaseController
{
    protected $kelasModel;
    protected $dosenPengajarModel;
    protected $programStudiModel;
    protected $mataKuliahModel;
    protected $dosenModel;
    protected $neoFeederService;
    protected $kurikulumModel;

    public function __construct()
    {
        $this->kelasModel = new KelasPerkuliahanModel();
        $this->dosenPengajarModel = new DosenPengajarKelasModel();
        $this->programStudiModel = new ProgramStudiModel();
        $this->mataKuliahModel = new MataKuliahModel();
        $this->dosenModel = new DosenModel();
        $this->neoFeederService = new NeoFeederService();
        $this->kurikulumModel = new KurikulumModel();
    }

    public function index()
    {
        $search = $this->request->getGet('search');
        $prodi = $this->request->getGet('prodi');
        $kurikulum = $this->request->getGet('kurikulum');
        $periode = $this->request->getGet('periode');
        $semester = $this->request->getGet('semester');
        $perPage = $this->request->getGet('per_page') ?? 10;

        $this->kelasModel->select('kelas_perkuliahan.*, program_studi.nama_prodi, mata_kuliah.sks_mata_kuliah as sks_asli');
        $this->kelasModel->join('program_studi', 'program_studi.id = kelas_perkuliahan.id_program_studi', 'left');
        $this->kelasModel->join('mata_kuliah', 'mata_kuliah.nama_mata_kuliah = kelas_perkuliahan.nama_mata_kuliah AND mata_kuliah.id_program_studi = kelas_perkuliahan.id_program_studi', 'left', false);

        if (!empty($search)) {
            $this->kelasModel->groupStart()
                ->like('kelas_perkuliahan.nama_mata_kuliah', $search)
                ->orLike('kelas_perkuliahan.nama_kelas_kuliah', $search)
                ->groupEnd();
        }

        if (!empty($prodi)) {
            $this->kelasModel->where('kelas_perkuliahan.id_program_studi', $prodi);
        }
        if (!empty($kurikulum)) {
            $this->kelasModel->where('mata_kuliah.id_kurikulum', $kurikulum);
        }
        if (!empty($semester)) {
            $this->kelasModel->where('mata_kuliah.semester', $semester);
        }
        
        if (!empty($periode)) {
            $this->kelasModel->where('kelas_perkuliahan.id_semester', $periode);
        }

        // Hitung total SKS berdasarkan filter sebelum dipaginate
        $db = \Config\Database::connect();
        $builderSum = $db->table('kelas_perkuliahan');
        $builderSum->select('kelas_perkuliahan.nama_mata_kuliah, MAX(COALESCE(mata_kuliah.sks_mata_kuliah, kelas_perkuliahan.sks)) as sks_mk');
        $builderSum->join('mata_kuliah', 'mata_kuliah.nama_mata_kuliah = kelas_perkuliahan.nama_mata_kuliah AND mata_kuliah.id_program_studi = kelas_perkuliahan.id_program_studi', 'left', false);
        if (!empty($search)) {
            $builderSum->groupStart()
                ->like('kelas_perkuliahan.nama_mata_kuliah', $search)
                ->orLike('kelas_perkuliahan.nama_kelas_kuliah', $search)
                ->groupEnd();
        }
        if (!empty($prodi)) {
            $builderSum->where('kelas_perkuliahan.id_program_studi', $prodi);
        }
        if (!empty($kurikulum)) {
            $builderSum->where('mata_kuliah.id_kurikulum', $kurikulum);
        }
        if (!empty($semester)) {
            $builderSum->where('mata_kuliah.semester', $semester);
        }
        if (!empty($periode)) {
            $builderSum->where('kelas_perkuliahan.id_semester', $periode);
        }
        $builderSum->groupBy('kelas_perkuliahan.nama_mata_kuliah');
        $distinctMatkul = $builderSum->get()->getResultArray();
        $totalSks = array_sum(array_column($distinctMatkul, 'sks_mk'));

        $kelas = $this->kelasModel->paginate($perPage, 'kelas_perkuliahan');
        
        $pesertaKelasModel = new \App\Models\PesertaKelasModel();
        
        // Ambil data dosen pengajar dan hitung peserta untuk setiap kelas
        foreach($kelas as &$k) {
            $dosenList = $this->dosenPengajarModel->where('id_kelas_kuliah', $k['id_kelas_kuliah_pddikti'])->findAll();
            $dosenNames = array_column($dosenList, 'nama_dosen');
            $k['dosen_pengajar'] = !empty($dosenNames) ? implode(', ', $dosenNames) : '-';
            
            // Hitung peserta riil dari database lokal
            $k['jumlah_mahasiswa'] = $pesertaKelasModel->where('id_kelas_kuliah_pddikti', $k['id_kelas_kuliah_pddikti'])->countAllResults();
        }

        // Get list of distinct periode from database
        $queryPeriode = $db->query("SELECT DISTINCT id_semester FROM kelas_perkuliahan WHERE id_semester IS NOT NULL AND id_semester != '' ORDER BY id_semester DESC");
        $listPeriode = $queryPeriode->getResultArray();

        $data = [
            'title' => 'Data Kelas Perkuliahan | SIAKAD ITKA',
            'kelas' => $kelas,
            'pager' => $this->kelasModel->pager,
            'list_prodi' => $this->programStudiModel->findAll(),
            'list_kurikulum' => $this->kurikulumModel->findAll(),
            'list_periode' => $listPeriode,
            'per_page' => $perPage,
            'total_sks' => $totalSks
        ];

        return view('kelas_perkuliahan/index', $data);
    }

    public function sync()
    {
        if (!$this->neoFeederService->isConfigured()) {
            session()->setFlashdata('error', 'Konfigurasi PDDIKTI belum diatur. Silakan atur di menu Pengaturan PDDIKTI.');
            return redirect()->to('/kelas-perkuliahan');
        }

        // 1. Sync Kelas Kuliah
        $resultKelas = $this->neoFeederService->request('GetListKelasKuliah');
        $successKelas = 0;
        $updateKelas = 0;

        if (isset($resultKelas['error_code']) && $resultKelas['error_code'] == 0 && isset($resultKelas['data'])) {
            $prodiLocal = $this->programStudiModel->findAll();
            $prodiMap = [];
            foreach ($prodiLocal as $p) {
                if(!empty($p['id_prodi_pddikti'])) {
                    $prodiMap[$p['id_prodi_pddikti']] = $p['id'];
                }
            }

            foreach ($resultKelas['data'] as $pddikti) {
                $idProdiPddikti = $pddikti['id_prodi'] ?? '';
                $idProdi = $prodiMap[$idProdiPddikti] ?? null;

                $existing = $this->kelasModel->where('id_kelas_kuliah_pddikti', $pddikti['id_kelas_kuliah'])->first();

                $data = [
                    'id_kelas_kuliah_pddikti' => $pddikti['id_kelas_kuliah'],
                    'id_program_studi'   => $idProdi,
                    'id_semester'        => $pddikti['id_semester'] ?? '',
                    'id_mata_kuliah'     => null, 
                    'nama_mata_kuliah'   => $pddikti['nama_mata_kuliah'] ?? '',
                    'nama_kelas_kuliah'  => $pddikti['nama_kelas_kuliah'] ?? '',
                    'sks'                => $pddikti['sks_mk'] ?? $pddikti['sks'] ?? $pddikti['sks_mata_kuliah'] ?? 0,
                    'jumlah_mahasiswa'   => $pddikti['jumlah_mahasiswa'] ?? 0
                ];

                if ($existing) {
                    $this->kelasModel->update($existing['id'], $data);
                    $updateKelas++;
                } else {
                    $this->kelasModel->insert($data);
                    $successKelas++;
                }
            }
        } else {
            session()->setFlashdata('error', "Gagal menarik data Kelas Kuliah: " . ($resultKelas['error_desc'] ?? 'Unknown Error'));
            return redirect()->to('/kelas-perkuliahan');
        }

        // 2. Sync Dosen Pengajar Kelas Kuliah
        $resultDosen = $this->neoFeederService->request('GetDosenPengajarKelasKuliah');
        $successDosen = 0;
        $updateDosen = 0;

        if (isset($resultDosen['error_code']) && $resultDosen['error_code'] == 0 && isset($resultDosen['data'])) {
            $dosenLocal = $this->dosenModel->findAll();
            $dosenMap = [];
            foreach ($dosenLocal as $d) {
                if(!empty($d['id_dosen_pddikti'])) {
                    $dosenMap[$d['id_dosen_pddikti']] = $d['id'];
                }
            }

            foreach ($resultDosen['data'] as $pddikti) {
                $idDosenPddikti = $pddikti['id_dosen'] ?? '';
                $idDosen = $dosenMap[$idDosenPddikti] ?? null;

                $existing = $this->dosenPengajarModel->where('id_aktivitas_mengajar_pddikti', $pddikti['id_aktivitas_mengajar'])->first();

                $data = [
                    'id_aktivitas_mengajar_pddikti' => $pddikti['id_aktivitas_mengajar'],
                    'id_kelas_kuliah'      => $pddikti['id_kelas_kuliah'],
                    'id_dosen'             => $idDosen,
                    'nama_dosen'           => $pddikti['nama_dosen'] ?? '',
                    'sks_substansi_total'  => $pddikti['sks_substansi_total'] ?? 0,
                    'rencana_tatap_muka'   => $pddikti['rencana_tatap_muka'] ?? 0,
                    'realisasi_tatap_muka' => $pddikti['realisasi_tatap_muka'] ?? 0
                ];

                if ($existing) {
                    $this->dosenPengajarModel->update($existing['id'], $data);
                    $updateDosen++;
                } else {
                    $this->dosenPengajarModel->insert($data);
                    $successDosen++;
                }
            }
        }

        // 3. Sync Matkul Kurikulum for semester info
        $resultMK = $this->neoFeederService->request('GetMatkulKurikulum');
        $kurikulumModel = new \App\Models\KurikulumModel();
        
        // Buat cache lokal untuk kurikulum mapping (pddikti -> local ID)
        $kurikulumData = $kurikulumModel->findAll();
        $kurikulumMap = [];
        foreach ($kurikulumData as $k) {
            $kurikulumMap[$k['id_kurikulum_pddikti']] = $k['id'];
        }

        if (isset($resultMK['error_code']) && $resultMK['error_code'] == 0 && isset($resultMK['data'])) {
            foreach ($resultMK['data'] as $pddikti) {
                $idMatkul = $pddikti['id_matkul'] ?? '';
                $pddiktiIdKurikulum = $pddikti['id_kurikulum'] ?? '';
                $localIdKurikulum = isset($kurikulumMap[$pddiktiIdKurikulum]) ? $kurikulumMap[$pddiktiIdKurikulum] : null;
                $semesterTingkat = $pddikti['semester'] ?? $pddikti['semester_tingkat'] ?? null;

                if (!empty($idMatkul)) {
                    $this->mataKuliahModel->where('id_matkul_pddikti', $idMatkul)
                        ->set(['id_kurikulum' => $localIdKurikulum, 'semester' => $semesterTingkat])
                        ->update();
                }
            }
        }

        session()->setFlashdata('pesan', "Sinkronisasi berhasil. Kelas (Baru: $successKelas, Update: $updateKelas). Dosen Pengajar (Baru: $successDosen, Update: $updateDosen). Info Kurikulum MK juga telah disinkronkan.");
        return redirect()->to('/kelas-perkuliahan');
    }

    public function peserta($id_kelas)
    {
        $kelas = $this->kelasModel->where('id_kelas_kuliah_pddikti', $id_kelas)->first();
        if (!$kelas) {
            session()->setFlashdata('error', 'Kelas tidak ditemukan.');
            return redirect()->to('/kelas-perkuliahan');
        }

        $pesertaKelasModel = new \App\Models\PesertaKelasModel();
        $peserta = $pesertaKelasModel->where('id_kelas_kuliah_pddikti', $id_kelas)->findAll();

        $mahasiswaModel = new \App\Models\MahasiswaModel();
        $prodiModel = new \App\Models\ProgramStudiModel();

        // Get filter values
        $filterProdi = $this->request->getGet('prodi');
        $filterAngkatan = $this->request->getGet('angkatan');

        $mahasiswaQuery = $mahasiswaModel->select('id_mahasiswa_pddikti, nim, nama_lengkap');
        
        if (!empty($filterProdi)) {
            $mahasiswaQuery = $mahasiswaQuery->where('prodi_id', $filterProdi);
        }
        if (!empty($filterAngkatan)) {
            $mahasiswaQuery = $mahasiswaQuery->where('angkatan', $filterAngkatan);
        }

        $mahasiswaList = $mahasiswaQuery->orderBy('nama_lengkap', 'ASC')->findAll();
        
        // Get dropdown data for filters
        $list_prodi = $prodiModel->orderBy('nama_prodi', 'ASC')->findAll();
        
        // Get distinct angkatan
        $db = \Config\Database::connect();
        $list_angkatan = $db->table('mahasiswa')
            ->select('angkatan')
            ->where('angkatan IS NOT NULL')
            ->where('angkatan !=', '')
            ->distinct()
            ->orderBy('angkatan', 'DESC')
            ->get()
            ->getResultArray();

        $data = [
            'title' => 'Detail Peserta Kelas',
            'kelas' => $kelas,
            'peserta' => $peserta,
            'mahasiswaList' => $mahasiswaList,
            'list_prodi' => $list_prodi,
            'list_angkatan' => $list_angkatan,
            'filterProdi' => $filterProdi,
            'filterAngkatan' => $filterAngkatan
        ];

        return view('kelas_perkuliahan/peserta', $data);
    }

    public function addPeserta()
    {
        $id_kelas = $this->request->getPost('id_kelas_kuliah_pddikti');
        $id_mahasiswa = $this->request->getPost('id_mahasiswa_pddikti');

        if (empty($id_kelas) || empty($id_mahasiswa)) {
            session()->setFlashdata('error', 'Pilih kelas dan mahasiswa terlebih dahulu.');
            return redirect()->to('/kelas-perkuliahan/peserta/' . $id_kelas);
        }

        $mahasiswaModel = new \App\Models\MahasiswaModel();
        $mahasiswa = $mahasiswaModel->where('id_mahasiswa_pddikti', $id_mahasiswa)->first();

        if (!$mahasiswa) {
            session()->setFlashdata('error', 'Data mahasiswa tidak valid.');
            return redirect()->to('/kelas-perkuliahan/peserta/' . $id_kelas);
        }

        $pesertaKelasModel = new \App\Models\PesertaKelasModel();
        
        // Cek duplicate
        $existing = $pesertaKelasModel->where('id_kelas_kuliah_pddikti', $id_kelas)
            ->where('id_mahasiswa_pddikti', $id_mahasiswa)
            ->first();

        if ($existing) {
            session()->setFlashdata('error', 'Mahasiswa sudah terdaftar di kelas ini.');
            return redirect()->to('/kelas-perkuliahan/peserta/' . $id_kelas);
        }

        $pesertaKelasModel->insert([
            'id_kelas_kuliah_pddikti' => $id_kelas,
            'id_mahasiswa_pddikti' => $id_mahasiswa,
            'nim' => $mahasiswa['nim'],
            'nama_mahasiswa' => $mahasiswa['nama_lengkap']
        ]);

        session()->setFlashdata('success', 'Mahasiswa berhasil ditambahkan ke kelas.');
        return redirect()->to('/kelas-perkuliahan/peserta/' . $id_kelas);
    }

    public function deletePeserta($id)
    {
        $pesertaKelasModel = new \App\Models\PesertaKelasModel();
        $peserta = $pesertaKelasModel->find($id);

        if ($peserta) {
            $pesertaKelasModel->delete($id);
            session()->setFlashdata('success', 'Peserta berhasil dihapus.');
            return redirect()->to('/kelas-perkuliahan/peserta/' . $peserta['id_kelas_kuliah_pddikti']);
        }

        return redirect()->to('/kelas-perkuliahan');
    }

    public function create()
    {
        $prodiModel = new \App\Models\ProgramStudiModel();
        $tahunAkademikModel = new \App\Models\TahunAkademikModel();

        $data = [
            'title' => 'Tambah Kelas Perkuliahan',
            'list_prodi' => $prodiModel->orderBy('nama_prodi', 'ASC')->findAll(),
            'tahun_aktif' => $tahunAkademikModel->where('status', 'Aktif')->first()
        ];

        return view('kelas_perkuliahan/create', $data);
    }

    public function getMatkulByProdi()
    {
        $id_prodi = $this->request->getGet('id_prodi');
        $semester = $this->request->getGet('semester');

        if (empty($id_prodi)) {
            return $this->response->setJSON([]);
        }

        $matkulModel = new \App\Models\MataKuliahModel();
        
        $query = $matkulModel->where('id_program_studi', $id_prodi);
        
        if (!empty($semester)) {
            // Some databases store semester as string, some as int. 
            // In PDDIKTI it might be "1", "2"
            $query = $query->where('semester', $semester);
        }

        $matkul = $query->orderBy('nama_mata_kuliah', 'ASC')->findAll();

        return $this->response->setJSON($matkul);
    }

    public function store()
    {
        $id_prodi = $this->request->getPost('id_prodi');
        $id_semester = $this->request->getPost('id_semester');
        $id_mata_kuliah = $this->request->getPost('id_mata_kuliah');
        $nama_kelas_kuliah = $this->request->getPost('nama_kelas_kuliah');

        if (empty($id_prodi) || empty($id_semester) || empty($id_mata_kuliah) || empty($nama_kelas_kuliah)) {
            session()->setFlashdata('error', 'Semua kolom wajib diisi.');
            return redirect()->back()->withInput();
        }

        $matkulModel = new \App\Models\MataKuliahModel();
        $matkul = $matkulModel->where('id_matkul_pddikti', $id_mata_kuliah)->first();

        $prodiModel = new \App\Models\ProgramStudiModel();
        $prodi = $prodiModel->find($id_prodi);

        // Generate a random UUID-like string for local usage
        $local_uuid = sprintf(
            '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
            mt_rand(0, 0xffff), mt_rand(0, 0xffff),
            mt_rand(0, 0xffff),
            mt_rand(0, 0x0fff) | 0x4000,
            mt_rand(0, 0x3fff) | 0x8000,
            mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
        );

        $dataInsert = [
            'id_kelas_kuliah_pddikti' => $local_uuid,
            'id_program_studi' => $id_prodi,
            'id_semester' => $id_semester,
            'id_mata_kuliah' => $id_mata_kuliah,
            'nama_kelas_kuliah' => trim($nama_kelas_kuliah),
            'nama_mata_kuliah' => $matkul ? $matkul['nama_mata_kuliah'] : '',
            'nama_program_studi' => $prodi ? $prodi['nama_prodi'] : '',
            'sks_mata_kuliah' => $matkul ? $matkul['sks_mata_kuliah'] : 0,
            'jumlah_mahasiswa' => 0
        ];

        $this->kelasModel->insert($dataInsert);

        session()->setFlashdata('pesan', 'Kelas perkuliahan baru berhasil ditambahkan.');
        return redirect()->to('/kelas-perkuliahan');
    }

    public function edit($id)
    {
        $kelas = $this->kelasModel->where('id_kelas_kuliah_pddikti', $id)->first();
        if (!$kelas) {
            throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        }

        $prodiModel = new \App\Models\ProgramStudiModel();
        $tahunAkademikModel = new \App\Models\TahunAkademikModel();

        $data = [
            'title' => 'Edit Kelas Perkuliahan',
            'kelas' => $kelas,
            'list_prodi' => $prodiModel->orderBy('nama_prodi', 'ASC')->findAll(),
            'tahun_aktif' => $tahunAkademikModel->where('kode_ta', $kelas['id_semester'])->first() ?: $tahunAkademikModel->where('status', 'Aktif')->first()
        ];

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

    public function update($id)
    {
        $kelas = $this->kelasModel->where('id_kelas_kuliah_pddikti', $id)->first();
        if (!$kelas) {
            throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        }

        $id_prodi = $this->request->getPost('id_prodi');
        $id_semester = $this->request->getPost('id_semester');
        $id_mata_kuliah = $this->request->getPost('id_mata_kuliah');
        $nama_kelas_kuliah = $this->request->getPost('nama_kelas_kuliah');

        if (empty($id_prodi) || empty($id_semester) || empty($id_mata_kuliah) || empty($nama_kelas_kuliah)) {
            session()->setFlashdata('error', 'Semua kolom wajib diisi.');
            return redirect()->back()->withInput();
        }

        $matkulModel = new \App\Models\MataKuliahModel();
        $matkul = $matkulModel->where('id_matkul_pddikti', $id_mata_kuliah)->first();

        $prodiModel = new \App\Models\ProgramStudiModel();
        $prodi = $prodiModel->find($id_prodi);

        $dataUpdate = [
            'id_program_studi' => $id_prodi,
            'id_semester' => $id_semester,
            'id_mata_kuliah' => $id_mata_kuliah,
            'nama_kelas_kuliah' => trim($nama_kelas_kuliah),
            'nama_mata_kuliah' => $matkul ? $matkul['nama_mata_kuliah'] : '',
            'nama_program_studi' => $prodi ? $prodi['nama_prodi'] : '',
            'sks_mata_kuliah' => $matkul ? $matkul['sks_mata_kuliah'] : 0,
        ];

        $this->kelasModel->update($kelas['id'], $dataUpdate);

        session()->setFlashdata('pesan', 'Kelas perkuliahan berhasil diperbarui.');
        return redirect()->to('/kelas-perkuliahan');
    }

    public function delete($id)
    {
        $kelas = $this->kelasModel->where('id_kelas_kuliah_pddikti', $id)->first();
        if (!$kelas) {
            throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        }

        $this->kelasModel->delete($kelas['id']);
        session()->setFlashdata('pesan', 'Kelas perkuliahan berhasil dihapus.');
        return redirect()->to('/kelas-perkuliahan');
    }

    public function dosen($id_kelas)
    {
        $kelas = $this->kelasModel->where('id_kelas_kuliah_pddikti', $id_kelas)->first();
        if (!$kelas) {
            throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        }

        $dosenKelasModel = new \App\Models\DosenPengajarKelasModel();
        $dosenPengajar = $dosenKelasModel->where('id_kelas_kuliah', $id_kelas)->findAll();

        $dosenModel = new \App\Models\DosenModel();
        $list_dosen = $dosenModel->orderBy('nama_lengkap', 'ASC')->findAll();

        $data = [
            'title' => 'Kelola Dosen Pengajar',
            'kelas' => $kelas,
            'dosenPengajar' => $dosenPengajar,
            'list_dosen' => $list_dosen
        ];

        return view('kelas_perkuliahan/dosen', $data);
    }

    public function addDosen()
    {
        $id_kelas = $this->request->getPost('id_kelas_kuliah');
        $id_dosen = $this->request->getPost('id_dosen');

        if (empty($id_kelas) || empty($id_dosen)) {
            session()->setFlashdata('error', 'Silakan pilih dosen.');
            return redirect()->back();
        }

        $dosenKelasModel = new \App\Models\DosenPengajarKelasModel();
        
        // Cek duplikasi
        $cek = $dosenKelasModel->where([
            'id_kelas_kuliah' => $id_kelas,
            'id_dosen' => $id_dosen
        ])->first();

        if ($cek) {
            session()->setFlashdata('error', 'Dosen tersebut sudah terdaftar di kelas ini.');
            return redirect()->back();
        }

        $dosenModel = new \App\Models\DosenModel();
        $dosenData = $dosenModel->where('id_dosen_pddikti', $id_dosen)->first();

        $dosenKelasModel->insert([
            'id_kelas_kuliah' => $id_kelas,
            'id_dosen' => $id_dosen,
            'nama_dosen' => $dosenData ? $dosenData['nama_lengkap'] : '',
            'sks_substansi_total' => 0,
            'rencana_tatap_muka' => 0,
            'realisasi_tatap_muka' => 0
        ]);

        session()->setFlashdata('success', 'Dosen berhasil ditugaskan ke kelas.');
        return redirect()->back();
    }

    public function deleteDosen($id)
    {
        $dosenKelasModel = new \App\Models\DosenPengajarKelasModel();
        $dosenKelasModel->delete($id);
        
        session()->setFlashdata('success', 'Penugasan dosen berhasil dihapus dari kelas.');
        return redirect()->back();
    }
}
