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

if (!function_exists('az_at_default_start')) { function az_at_default_start(){ return '07:30'; } }
if (!function_exists('az_at_default_end'))   { function az_at_default_end(){ return '20:00'; } }

if (!function_exists('az_at_day_name')) {
    function az_at_day_name($dow){
        $n = array(1=>'Senin',2=>'Selasa',3=>'Rabu',4=>'Kamis',5=>'Jumat',6=>'Sabtu',7=>'Minggu');
        return isset($n[$dow]) ? $n[$dow] : '';
    }
}

if (!function_exists('az_at_to_min')) {
    function az_at_to_min($t){
        $t = trim((string)$t); if ($t == '') $t = '00:00';
        $p = explode(':', $t);
        return ((int)$p[0]) * 60 + (int)(isset($p[1]) ? $p[1] : 0);
    }
}

// saklar utama ON/OFF
if (!function_exists('az_at_enabled')) {
    function az_at_enabled(){
        $ci =& get_instance(); $ci->load->database();
        $r = $ci->db->get_where('access_time_setting', array('setting_key'=>'enabled'))->row();
        if (!$r) return false;
        return ((int)$r->setting_value === 1);
    }
}

// jadwal efektif 1 hari untuk 1 user (pakai default kalau belum diset)
if (!function_exists('az_at_get_day')) {
    function az_at_get_day($iduser, $dow){
        $ci =& get_instance();
        $ci->db->where('iduser', $iduser);
        $ci->db->where('day_of_week', $dow);
        $ci->db->where('status', 1);
        $row = $ci->db->get('user_schedule')->row();
        if ($row) {
            return array('is_off'=>(int)$row->is_off, 'start'=>substr($row->time_start,0,5), 'end'=>substr($row->time_end,0,5), 'custom'=>true);
        }
        return array('is_off'=>0, 'start'=>az_at_default_start(), 'end'=>az_at_default_end(), 'custom'=>false);
    }
}

// CEK UTAMA: bolehkah user ini akses SEKARANG?
if (!function_exists('az_check_access_time')) {
function az_check_access_time($iduser, $role_name){
// Administrator selalu bebas
if (strtolower((string)$role_name) === 'administrator') {
return array('ok'=>true, 'msg'=>'');
}
$ci =& get_instance();
$ci->load->database();
// Cek apakah pembatasan aktif
$ci->db->where('setting_key', 'enabled');
$setting = $ci->db->get('access_time_setting')->row();
if (!$setting || (int)$setting->setting_value !== 1) {
// Pembatasan TIDAK aktif
return array('ok'=>true, 'msg'=>'');
}
$today = date('Y-m-d');
$dow = (int)date('N'); // 1=Senin ... 7=Minggu
// 1) Cek hari libur
$ci->db->where('holiday_date', $today);
$ci->db->where('status', 1);
$holiday = $ci->db->get('holiday')->row();
if ($holiday) {
$lbl = strlen($holiday->label) ? (' ('.$holiday->label.')') : '';
if ((int)$holiday->all_off === 1) {
return array('ok'=>false, 'msg'=>'Hari ini libur'.$lbl.'. Akses ditutup.');
} else {
$ci->db->where('holiday_id', $holiday->id);
$ci->db->where('iduser', $iduser);
$ci->db->where('status', 1);
$exc = $ci->db->get('holiday_exception')->row();
if ($exc) {
return array('ok'=>true, 'msg'=>''); // dikecualikan
} else {
return array('ok'=>false, 'msg'=>'Hari ini libur'.$lbl.'. Anda tidak dijadwalkan aktif.');
}
}
}
// 2) Jadwal mingguan per user
$ci->db->where('iduser', $iduser);
$ci->db->where('day_of_week', $dow);
$ci->db->where('status', 1);
$schedule = $ci->db->get('user_schedule')->row();
if ($schedule) {
// User punya jadwal custom
if ((int)$schedule->is_off === 1) {
return array('ok'=>false, 'msg'=>'Hari '.az_at_day_name($dow).' Anda dijadwalkan LIBUR.');
}
$start = $schedule->time_start;
$end   = $schedule->time_end;
} else {
// ===== PERBAIKAN =====
// Sebelumnya: tanpa jadwal custom = BEBAS (ini yang membuat driver &
// produksi selalu lolos). Sekarang: ikuti jam default global.
$start = az_at_default_start(); // 07:30
$end   = az_at_default_end();   // 20:00
}
$now = az_at_to_min(date('H:i'));
if ($now < az_at_to_min($start) || $now > az_at_to_min($end)) {
return array('ok'=>false, 'msg'=>'Di luar jam aktif hari '.az_at_day_name($dow).' ('.$start.' - '.$end.').');
}
return array('ok'=>true, 'msg'=>'');
}
}