<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Cek permission user
 * @param string $key    => nama fitur (misal: 'siswa', 'dashboard')
 * @param string $action => action yang dicek (misal: 'can_view', 'can_export')
 * @return bool
 */
function check_permission($key, $action = 'can_view') {
    $CI =& get_instance();
    $permissions = $CI->session->userdata('permissions');

    if (!$permissions || !is_array($permissions)) {
        return false;
    }

    // Pastikan key ada
    if (!isset($permissions[$key])) {
        return false;
    }

    // Jika value berupa array (multiple permissions), cek dengan in_array
    if (is_array($permissions[$key])) {
        return in_array($action, $permissions[$key]);
    }

    // Jika value berupa string tunggal
    return $permissions[$key] === $action;
}
