<?php

namespace App\Controllers\API\V1;

use App\Controllers\BaseController;
use CodeIgniter\API\ResponseTrait;

class PresenceReminder extends BaseController
{
    use ResponseTrait;
    public function index($hash, $person, $type)
    {
        $secret = options("PANELJWTKEY") . options("PANELCN");
        if ($hash != md5(base64_encode(md5($secret)))) return $this->failUnauthorized("Secret Invalid");
        $db = db_connect();
        $sdt = strtotime(" 00:00:00");
        $edt = strtotime(" 23:59:59");
        if ($type == 'keluar' && $person == 'staff') {
            $notif['url'] = base_url('panel/daily-presence/map');
            $select = ["GROUP_CONCAT(MD5(CONCAT('s',id))) uid"];
            $builder = $db->table('users u')->join('daily_presence_guru dpg', "dpg.user_id=u.id AND ((dpg.timeIn>=$sdt AND dpg.timeIn<=$edt) OR (dpg.timeOut>=$sdt AND dpg.timeOut<=$edt))", 'left');
            $builder->select(implode(',', $select));
            $builder->join('users_roles ur', 'ur.user_id=u.id AND role_id=2');
            $builder->where('dpg.timeOut', null);
            $builder->where('dpg.status', '1');
        } else if ($type == 'keluar' && $person == 'siswa') {
            $notif['url'] = base_url('daily-presence/map');
            $select = ["GROUP_CONCAT(MD5(CONCAT('m',id))) uid"];
            $builder = $db->table('m_siswa ms')->join('daily_presence_siswa dps', "dps.siswa_id=ms.id AND ((dps.timeIn>=$sdt AND dps.timeIn<=$edt) OR (dps.timeOut>=$sdt AND dps.timeOut<=$edt))", 'left');
            $builder->select(implode(',', $select));
            $builder->where('dps.timeOut', null);
            $builder->where('dps.status', '1');
        } else if ($type == 'masuk' && $person == 'staff') {
            $notif['url'] = base_url('panel/daily-presence/map');
            $select = ["GROUP_CONCAT(MD5(CONCAT('s',id))) uid"];
            $builder = $db->table('users u')->join('daily_presence_guru dpg', "dpg.user_id=u.id AND ((dpg.timeIn>=$sdt AND dpg.timeIn<=$edt) OR (dpg.timeOut>=$sdt AND dpg.timeOut<=$edt))", 'left');
            $builder->select(implode(',', $select));
            $builder->join('users_roles ur', 'ur.user_id=u.id AND role_id=2');
            $builder->where('dpg.timeIn', null);
            $builder->where('dpg.status', '1');
            $builder->orWhere('dpg.status', '1');
        } else if ($type == 'masuk' && $person == 'siswa') {
            $notif['url'] = base_url('daily-presence/map');
            $select = ["GROUP_CONCAT(MD5(CONCAT('m',id))) uid"];
            $builder = $db->table('m_siswa ms')->join('daily_presence_siswa dps', "dps.siswa_id=ms.id AND ((dps.timeIn>=$sdt AND dps.timeIn<=$edt) OR (dps.timeOut>=$sdt AND dps.timeOut<=$edt))", 'left');
            $builder->select(implode(',', $select));
            $builder->where('dps.timeIn', null);
            $builder->orWhere('dps.status', '1');
        } else {
            return $this->failUnauthorized("Request Tidak Valid");
        }
        $audience = $builder->get()->getRowArray();
        if ($audience) {
            $text = "Jangan sampai telat presensi, klik disini untuk presensi";
            $heading = "Presensi Kehadiran";
            $notif['contents'] = [
                "en" => $text, "id" => $text,
            ];
            $notif['headings'] = [
                "en" => $heading, "id" => $heading,
            ];
            $notif['channel_for_external_user_ids'] = "push";
            $notif['include_external_user_ids'] = explode(',', $audience['uid']);
            sendOneSignal($notif);
        }
        return $this->respond(['message' => "Berhasil"]);
    }
}
