<?php
/**
 * Model Kelas
 */
class KelasModel {
    private $db;

    public function __construct() {
        $this->db = getDB();
    }

    public function getAll($search = '') {
        $where = '1=1';
        $params = [];
        $types = '';

        if ($search) {
            $where = '(k.nama_kelas LIKE ? OR k.tingkat LIKE ?)';
            $like = '%' . $search . '%';
            $params = [$like, $like];
            $types = 'ss';
        }

        $sql = "SELECT k.*, g.nama as nama_wali_kelas,
                COUNT(s.id) as jumlah_siswa
                FROM kelas k
                LEFT JOIN guru g ON k.wali_kelas_id = g.id
                LEFT JOIN siswa s ON k.id = s.kelas_id AND s.status = 'Aktif'
                WHERE $where
                GROUP BY k.id
                ORDER BY k.tingkat, k.nama_kelas";

        $stmt = $this->db->prepare($sql);
        if ($params) $stmt->bind_param($types, ...$params);
        $stmt->execute();
        return stmt_get_result($stmt)->fetch_all(MYSQLI_ASSOC);
    }

    public function getById($id) {
        $stmt = $this->db->prepare(
            "SELECT k.*, g.nama as nama_wali_kelas FROM kelas k
             LEFT JOIN guru g ON k.wali_kelas_id = g.id
             WHERE k.id = ?"
        );
        $stmt->bind_param('i', $id);
        $stmt->execute();
        return stmt_get_result($stmt)->fetch_assoc();
    }

    public function getAllSimple() {
        return query_fetch_all($this->db,
            "SELECT id, nama_kelas, tingkat FROM kelas ORDER BY tingkat, nama_kelas"
        );
    }

    public function getSiswaByKelas($kelasId) {
        $stmt = $this->db->prepare(
            "SELECT s.*, k.nama_kelas FROM siswa s
             LEFT JOIN kelas k ON s.kelas_id = k.id
             WHERE s.kelas_id = ? AND s.status = 'Aktif'
             ORDER BY s.nama"
        );
        $stmt->bind_param('i', $kelasId);
        $stmt->execute();
        return stmt_get_result($stmt)->fetch_all(MYSQLI_ASSOC);
    }

    public function insert($data) {
        $stmt = $this->db->prepare(
            "INSERT INTO kelas (nama_kelas, tingkat, jurusan, wali_kelas_id, tahun_ajaran, kapasitas)
             VALUES (?, ?, ?, ?, ?, ?)"
        );
        $stmt->bind_param('sssisi',
            $data['nama_kelas'], $data['tingkat'], $data['jurusan'],
            $data['wali_kelas_id'], $data['tahun_ajaran'], $data['kapasitas']
        );
        $stmt->execute();
        return $this->db->insert_id;
    }

    public function update($id, $data) {
        $stmt = $this->db->prepare(
            "UPDATE kelas SET nama_kelas=?, tingkat=?, jurusan=?, wali_kelas_id=?,
             tahun_ajaran=?, kapasitas=? WHERE id=?"
        );
        $stmt->bind_param('sssisii',
            $data['nama_kelas'], $data['tingkat'], $data['jurusan'],
            $data['wali_kelas_id'], $data['tahun_ajaran'], $data['kapasitas'], $id
        );
        return $stmt->execute();
    }

    public function delete($id) {
        $stmt = $this->db->prepare("DELETE FROM kelas WHERE id=?");
        $stmt->bind_param('i', $id);
        return $stmt->execute();
    }

    public function count() {
        $row = query_fetch_assoc($this->db, "SELECT COUNT(*) as total FROM kelas");
        return (int)($row['total'] ?? 0);
    }
}
