<?php

namespace App\Models;

use Core\Model;

class Maba extends Model
{
    protected $table = 'tb_pmb'; // tabel utama data awal pendaftaran

    public function findByNisn($nisn)
    {
        $stmt = $this->query("SELECT * FROM {$this->table} WHERE nisn = ?", [$nisn]);
        return $stmt->fetch();
    }

    public function all()
    {
        return $this->query("SELECT * FROM {$this->table}")->fetchAll();
    }

    public function findByUsername($username)
    {
        return $this->findByNisn($username);
    }

    public function checkMahasiswa($nim)
    {
        $stmt = $this->query("SELECT * FROM tb_mahasiswa WHERE nim = ?", [$nim]);
        return $stmt->fetch();
    }

    public function insertToMahasiswa($pmb)
    {
        $id_kurikulum = null;
        if ($pmb['id_prodi'] == 86206) {
            $id_kurikulum = 3;
        } elseif ($pmb['id_prodi'] == 86207) {
            $id_kurikulum = 4;
        }
    
        $sql = "INSERT INTO tb_mahasiswa
            (nim, nisn, nama, id_prodi, tahun_masuk, no_hp, email, id_kurikulum)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
    
        $this->query($sql, [
            $pmb['nisn'],          // nim sementara = nisn
            $pmb['nisn'],          // nisn
            $pmb['nama_camaba'],   // nama
            $pmb['id_prodi'],      // id_prodi
            $pmb['tahun_masuk'],   // tahun_masuk
            $pmb['wa'],            // no_hp
            null,                  // email default null
            $id_kurikulum
        ]);
    }

    public function updateProfile($nim, $data)
    {
        $sql = "UPDATE tb_mahasiswa SET 
                    alamat = ?, telpon = ?, tmpt_lahir = ?, tgl_lahir = ?, agama = ?, 
                    jenis_kelamin = ?, nik = ?, jalur_pendaftaran = ?, npwp = ?, kewarganegaraan = ?, 
                    jenis_pendaftaran = ?, tgl_masuk_kuliah = ?, mulai_semester = ?, 
                    jalan = ?, rt = ?, rw = ?, nama_dusun = ?, kelurahan = ?, kecamatan = ?, 
                    kode_pos = ?, jenis_tinggal = ?, alat_transportasi = ?, telp_rumah = ?, 
                    no_hp = ?, email = ?, terima_kps = ?, no_kps = ?
                WHERE nim = ?";
        
        $this->query($sql, [
            $data['alamat'], $data['telpon'], $data['tmpt_lahir'], $data['tgl_lahir'], $data['agama'],
            $data['jenis_kelamin'], $data['nik'], $data['jalur_pendaftaran'], $data['npwp'], $data['kewarganegaraan'],
            $data['jenis_pendaftaran'], $data['tgl_masuk_kuliah'], $data['mulai_semester'],
            $data['jalan'], $data['rt'], $data['rw'], $data['nama_dusun'], $data['kelurahan'], $data['kecamatan'],
            $data['kode_pos'], $data['jenis_tinggal'], $data['alat_transportasi'], $data['telp_rumah'],
            $data['no_hp'], $data['email'], $data['terima_kps'], $data['no_kps'],
            $nim
        ]);
    }

    public function updateOrangTua($nim, $data)
    {
        $sql = "UPDATE tb_mahasiswa SET 
                    nik_ayah = ?, nama_ayah = ?, tgl_lahir_ayah = ?, pendidikan_ayah = ?, pekerjaan_ayah = ?, penghasilan_ayah = ?, 
                    nik_ibu = ?, nama_ibu = ?, tgl_lahir_ibu = ?, pendidikan_ibu = ?, pekerjaan_ibu = ?, penghasilan_ibu = ?, 
                    nama_wali = ?, tgl_lahir_wali = ?, pendidikan_wali = ?, pekerjaan_wali = ?, penghasilan_wali = ?
                WHERE nim = ?";
        
        $this->query($sql, [
            $data['nik_ayah'], $data['nama_ayah'], $data['tgl_lahir_ayah'], $data['pendidikan_ayah'], $data['pekerjaan_ayah'], $data['penghasilan_ayah'],
            $data['nik_ibu'], $data['nama_ibu'], $data['tgl_lahir_ibu'], $data['pendidikan_ibu'], $data['pekerjaan_ibu'], $data['penghasilan_ibu'],
            $data['nama_wali'], $data['tgl_lahir_wali'], $data['pendidikan_wali'], $data['pekerjaan_wali'], $data['penghasilan_wali'],
            $nim
        ]);
    }

    public function updateKelas($nim, $data)
    {
        $sql = "UPDATE tb_mahasiswa SET kelas = ?, jenis_pembiayaan = ?, jumlah_biaya_masuk = ?, pusat_pokjar = ? WHERE nim = ?";
        $this->query($sql, [$data['kelas'], $data['jenis_pembiayaan'], $data['jumlah_biaya_masuk'], $data['pusat_pokjar'], $nim]);
    }

    public function findMahasiswaByNim($nim)
    {
        $stmt = $this->query("SELECT * FROM tb_mahasiswa WHERE nim = ?", [$nim]);
        return $stmt->fetch();
    }

    public function isProfileComplete($nisn)
    {
        $data = $this->findMahasiswaByNim($nisn);
        return !empty($data['alamat']) && !empty($data['tgl_lahir']) && !empty($data['nik']);
    }
    
    public function isOrangTuaComplete($nisn)
    {
        $data = $this->findMahasiswaByNim($nisn);
        return !empty($data['nama_ayah']) && !empty($data['nama_ibu']);
    }
    
    public function hasKelas($nisn)
    {
        $data = $this->findMahasiswaByNim($nisn);
        return !empty($data['kelas']);
    }

    public function hasTokenMaba($nisn)
    {
        $stmt = $this->query("SELECT * FROM tb_token_maba WHERE nisn = ?", [$nisn]);
        return $stmt->fetch() ? true : false;
    }
    
    public function validateToken($token)
    {
        $stmt = $this->query("SELECT * FROM tb_token WHERE token = ? AND kegiatan = 'Camaba'", [$token]);
        return $stmt->fetch();
    }
    
    public function insertTokenMaba($nisn, $token)
    {
        // simpan ke tb_token_maba
        $sql = "INSERT INTO tb_token_maba (nisn, token) VALUES (?, ?)";
        $this->query($sql, [$nisn, $token]);
    
        // hapus token dari tb_token
        $this->query("DELETE FROM tb_token WHERE token = ?", [$token]);
    }

    public function searchWilayah($keyword, $levelWil)
    {
        $sql = "SELECT k.id_wil, k.nm_wil AS kecamatan, kab.nm_wil AS kabupaten
                FROM tb_wilayah k
                JOIN tb_wilayah kab ON kab.id_wil = k.id_induk_wilayah
                WHERE k.id_level_wil = ?
                  AND k.nm_wil LIKE ?
                LIMIT 10";
        $stmt = $this->query($sql, [$levelWil, "%$keyword%"]);
        return $stmt->fetchAll();
    }

}
