<?php

namespace App\Models;

use Core\Model;

class Krs extends Model
{
    protected $tableKrs = 'tb_krs';

    // Ambil data KRS berdasarkan nim
    public function getKrsByNim($nim)
    {
        $stmt = $this->query("SELECT * FROM {$this->tableKrs} WHERE nim = ?", [$nim]);
        return $stmt->fetchAll();
    }

    // Update kolom accept di tb_krs
    public function updateKrsAccept($id_krs, $accept)
    {
        $sql = "UPDATE {$this->tableKrs} SET accept = ? WHERE id_krs = ?";
        $this->query($sql, [$accept, $id_krs]);
    }

    // Cek token aktif mahasiswa
    public function hasValidToken($nim)
    {
        // Ambil tahun akademik aktif
        $tahun = $this->query("SELECT tahun_akademik FROM tb_akademik WHERE status = 'aktif' LIMIT 1")->fetch();
        if (!$tahun) return false;

        $tahun_aktif = $tahun['tahun_akademik'];

        // Cek token krs untuk mahasiswa
        $stmt = $this->query("SELECT * FROM tb_token_krs WHERE nim = ? AND tahun_akademik = ? LIMIT 1", [$nim, $tahun_aktif]);
        return $stmt->fetch() !== false;
    }

    // Ambil data matakuliah berdasarkan semester dan prodi aktif mahasiswa
    public function getDataKontrakMataKuliah($nim)
    {
        // Ambil semester aktif dari tb_kelas
        $stmt = $this->query("SELECT smt FROM tb_kelas WHERE nim = ? AND status = 'Aktif' LIMIT 1", [$nim]);
        $row = $stmt->fetch();
        if (!$row) return null;
        $smt = $row['smt'];

        // Ambil prodi dari tb_mahasiswa
        $stmt = $this->query("SELECT id_prodi FROM tb_mahasiswa WHERE nim = ? LIMIT 1", [$nim]);
        $row = $stmt->fetch();
        if (!$row) return null;
        $prodi = $row['id_prodi'];

        // Ambil matakuliah di semester dan prodi tersebut
        $stmt = $this->query("SELECT * FROM tb_mk WHERE smt = ? AND id_prodi = ?", [$smt, $prodi]);
        $matakuliahList = $stmt->fetchAll();

        $result = [];

        foreach ($matakuliahList as $mk) {
            // Cek apakah MK sudah dikontrak
            $cek = $this->query("SELECT * FROM tb_krs WHERE nim = ? AND kode_mk = ?", [$nim, $mk['kode_mk']]);
            $is_kontrak = $cek->rowCount() > 0 ? 'Dikontrak' : 'Belum Dikontrak';

            $result[] = [
                'kode_mk' => $mk['kode_mk'],
                'nama_mk' => $mk['nama_mk'],
                'sks' => $mk['sks'],
                'status' => $is_kontrak,
            ];
        }

        return $result;
    }

    // Ambil tahun akademik aktif
    public function getTahunAktif()
    {
        $stmt = $this->query("SELECT tahun_akademik FROM tb_akademik WHERE status = 'aktif' LIMIT 1");
        $row = $stmt->fetch();
        return $row ? $row['tahun_akademik'] : null;
    }

    // Cek token input ada di tb_token
    public function cekTokenDiTbToken($token)
    {
        $stmt = $this->query("SELECT * FROM tb_token WHERE token = ? AND kegiatan = 'KRS' LIMIT 1", [$token]);
        return $stmt->fetch() ? true : false;
    }

    // Cek apakah token sudah pernah dipakai oleh user pada tahun aktif
    public function cekTokenKrsSudahAda($nim, $tahun)
    {
        $stmt = $this->query("SELECT * FROM tb_token_krs WHERE nim = ? AND tahun_akademik = ? LIMIT 1", [$nim, $tahun]);
        return $stmt->fetch() ? true : false;
    }

    public function simpanTokenKrs($nim, $token, $tahun)
    {
        // Simpan token ke tb_token_krs
        $stmt = $this->query("INSERT INTO tb_token_krs (nim, token, tahun_akademik) VALUES (?, ?, ?)", [$nim, $token, $tahun]);

        // Jika penyimpanan berhasil, hapus token dari tb_token
        if ($stmt) {
            $this->query("DELETE FROM tb_token WHERE token = ? AND kegiatan = 'KRS'", [$token]);
            return true;
        }

        return false;
    }
}
