<?php

namespace App\Controllers\Panel\LainLain;

use App\Controllers\Panel\BC;
use App\Models\Panel\MasterData\Pengumuman as MasterDataPengumuman;
use CodeIgniter\API\ResponseTrait;

class Pengumuman extends BC
{
    use ResponseTrait;
    public function index()
    {
        $data['page_title'] = "Pengumuman";
        return view('Panel/LainLain/Pengumuman/index', $data);
    }
    public function all()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = (new MasterDataPengumuman())->findAll();
        return $this->respond(['data' => $data, 'csrf' => ['name' => csrf_token(), 'value' => csrf_hash()]]);
    }
    public function add()
    {
        $data['judul'] = $this->request->getVar('judul');
        $data['tujuan'] = $this->request->getVar('tujuan');
        $data['body'] = $this->request->getVar('body');
        if (!(new MasterDataPengumuman())->insert($data)) return $this->respond(['message' => 'Gagal membuat pengumuman']);
        if ($data['tujuan'] == 2) {
            $notif['url'] = base_url('pengumuman');
            $audience = db_connect()->table('m_siswa')->select("GROUP_CONCAT(MD5(CONCAT('m',id))) uid")->get()->getRowArray();
        } else if ($data['tujuan'] == 1) {
            $notif['url'] = base_url('panel/announcement');
            $audience = db_connect()->table('users')->select("GROUP_CONCAT(MD5(CONCAT('s',id))) uid")->get()->getRowArray();
        }
        if ($audience) {
            $text = "Sebuah pengumuman \"$data[judul]\" telah ditambahkan";
            $heading = "Pengumuman Baru";
            $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 membuat pengumuman']);
    }
    public function detail($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data = (new MasterDataPengumuman())->find($id);
        if (!$data) return $this->fail(['message' => "Data Tidak Ditemukan"]);
        return $this->respond($data);
    }
    public function edit($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $data['judul'] = $this->request->getVar('judul');
        $data['tujuan'] = $this->request->getVar('tujuan');
        $data['body'] = $this->request->getVar('body');
        $edited = (new MasterDataPengumuman())->update($id, $data);
        if (!$edited) return $this->fail(['message' => "Tidak Dapat Mengubah Data"]);
        return $this->respond(['message' => "Berhasil Mengubah Data"]);
    }
    public function delete($id)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        if ((new MasterDataPengumuman())->delete($id)) return $this->respond(['deleted' => true, 'message' => 'Berhasil menghapus pengumuman']);
        return $this->respond(['deleted' => false, 'message' => 'Gagal menghapus pengumuman']);
    }
    public function imageUploader()
    {
        $app = config('app');
        $img = $this->request->getFile('file');
        $allowed_mime = ['image/png', 'image/jpg', 'image/jpeg'];
        if (!$img->isValid()) return $this->failForbidden();
        if (!in_array($img->getClientMimeType(), $allowed_mime)) $this->failForbidden();
        if ($img->hasMoved()) return $this->failNotFound();
        $imgName = $img->getRandomName();
        compress_image($img->getTempName(), FCPATH . "$app->imgPath/pengumuman/$imgName");
        rmbfe('php', FCPATH . "$app->imgPath/pengumuman");
        return $this->respond(['location' => base_url("$app->imgPath/pengumuman/$imgName")]);
    }

    public function audioUploader()
    {
        $app = config('app');
        $audio = $this->request->getFile('audio');
        $allowed_mime = ['audio/mpeg'];
        if (!$audio->isValid()) return $this->failForbidden();
        if (!in_array($audio->getClientMimeType(), $allowed_mime)) $this->failForbidden();
        if ($audio->hasMoved()) return $this->failNotFound();
        $audioName = $audio->getRandomName();
        $audio->move(FCPATH . "$app->audioPath/pengumuman", $audioName);
        rmbfe('php', FCPATH . "$app->audioPath/pengumuman");

        return $this->respond(['location' => base_url("$app->audioPath/pengumuman/$audioName")]);
    }
}
