<?php
namespace App\Controllers;

use Core\Database;
use PDO;

class DosenLihatSoalController
{
    private $db;

    public function __construct()
    {
        $this->db = Database::getConnection();
    }

    public function index()
    {
        // Pastikan session sudah berisi user
        if (!isset($_SESSION['user']['niy'])) {
            header("Location: /login"); // arahkan ke login jika session tidak tersedia
            exit;
        }

        $niy = $_SESSION['user']['niy'];

        // Tahun akademik aktif
        $stmt = $this->db->prepare("SELECT tahun_akademik FROM tb_akademik WHERE status = 'Aktif'");
        $stmt->execute();
        $active_year = $stmt->fetchColumn();

        // Jenis soal aktif
        $stmt = $this->db->prepare("SELECT jenis_soal FROM tb_jenis_soal WHERE status = 'Aktif'");
        $stmt->execute();
        $active_soal = $stmt->fetchColumn();

        // Mata kuliah yang diampu dosen
        $stmt = $this->db->prepare("
            SELECT dm.kode_mk, mk.nama_mk, dm.kode_kelas
            FROM tb_dosen_mk dm
            INNER JOIN tb_mk mk ON dm.kode_mk = mk.kode_mk
            WHERE dm.niy = :niy AND dm.tahun_akademik = :tahun_akademik
        ");
        $stmt->execute([
            'niy' => $niy,
            'tahun_akademik' => $active_year
        ]);
        $mata_kuliah = $stmt->fetchAll(PDO::FETCH_ASSOC);

        $soal = [];
        if (isset($_GET['kode_mk'], $_GET['kode_kelas'], $_GET['niy'])) {
            $stmt = $this->db->prepare("
                SELECT DISTINCT s.id_soal, s.soal, s.tipe_soal, s.tanggal_upload
                FROM tb_soal s
                WHERE s.kode_mk = :kode_mk AND s.kode_kelas = :kode_kelas AND s.niy = :niy AND s.tipe_soal = :tipe_soal
            ");
            $stmt->execute([
                'kode_mk' => $_GET['kode_mk'],
                'kode_kelas' => $_GET['kode_kelas'],
                'niy' => $_GET['niy'],
                'tipe_soal' => $active_soal
            ]);
            $soal = $stmt->fetchAll(PDO::FETCH_ASSOC);
        }

        // Siapkan data untuk view
        $data = compact('mata_kuliah', 'soal', 'active_soal');

        // Include view dengan path benar (pastikan file ada)
        extract($data);
        include __DIR__ . '/../Views/dosen/lihat_soal.php';
    }

    public function edit()
    {
        // Pastikan session sudah berisi user
        if (!isset($_SESSION['user']['niy'])) {
            header("Location: /login"); // arahkan ke login jika session tidak tersedia
            exit;
        }
        
        $niy = $_SESSION['user']['niy'];

        if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'edit') {
            $stmt = $this->db->prepare("UPDATE tb_soal SET soal = :soal WHERE id_soal = :id_soal AND niy = :niy");
            $success = $stmt->execute([
                'soal' => $_POST['editSoal'],
                'id_soal' => $_POST['editId'],
                'niy' => $niy
            ]);
            // Bisa set session flash message jika ingin
        }

        header("Location: /dashboard/dosen-lihatsoal?kode_mk={$_GET['kode_mk']}&kode_kelas={$_GET['kode_kelas']}&niy={$niy}");
        exit;
    }

    public function delete()
    {
        // Pastikan session sudah berisi user
        if (!isset($_SESSION['user']['niy'])) {
            header("Location: /login"); // arahkan ke login jika session tidak tersedia
            exit;
        }
        
        $niy = $_SESSION['user']['niy'];

        if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'delete') {
            $stmt = $this->db->prepare("DELETE FROM tb_soal WHERE id_soal = :id_soal AND niy = :niy");
            $success = $stmt->execute([
                'id_soal' => $_POST['deleteId'],
                'niy' => $niy
            ]);
        }

        header("Location: /dashboard/dosen-lihatsoal?kode_mk={$_GET['kode_mk']}&kode_kelas={$_GET['kode_kelas']}&niy={$niy}");
        exit;
    }
}
