<?php

namespace App\Models;

use Core\Database;

class Jadwal
{
    public function getJadwalByNim($nim)
    {
        $pdo = Database::getConnection();
        $data = ['jadwal' => [], 'kode_prodi' => [], 'error' => null];

        try {
            $aktif = $pdo->query("SELECT id_akademik FROM tb_akademik WHERE status = 'Aktif'")->fetch();
            if (!$aktif) {
                $data['error'] = "Tidak ada tahun akademik aktif!";
                return $data;
            }

            $stmt = $pdo->prepare("
                SELECT mk.nama_mk, d.nama AS nama_dosen, j.hari, j.jam_mulai, j.jam_selesai, j.ruangan
                FROM tb_jadwal j
                JOIN tb_dosen_mk dm ON j.id_dmk = dm.id_dmk
                JOIN tb_dosen d ON dm.niy = d.niy
                JOIN tb_mk mk ON dm.kode_mk = mk.kode_mk
                JOIN tb_kelas k ON dm.kode_kelas = k.kode_kelas
                JOIN tb_akademik a ON k.tahun_akademik = a.tahun_akademik
                WHERE k.nim = ? AND a.id_akademik = ?
            ");
            $stmt->execute([$nim, $aktif['id_akademik']]);
            $data['jadwal'] = $stmt->fetchAll();
        } catch (\PDOException $e) {
            $data['error'] = $e->getMessage();
        }

        $data['kode_prodi'] = [
            '86207' => 'Pendidikan Guru Sekolah Dasar',
            '86206' => 'Pendidikan Guru Pendidikan Anak Usia Dini',
        ];
        return $data;
    }
}
