<?php

namespace App\Controllers\Panel;

use App\Controllers\BaseController;

class BC extends BaseController
{
    /***
     * First Table To Insert From Backup
     * Prevent Error For Foreign Table Restore
     */
    protected $allowedRestoreVersion = [APP_VERSION_CODE];
    protected $firstTable = [
        'm_kelas', 'users', 'roles', 'users_roles', 'm_banksoal', 'm_jenisujian', 'm_jurusan',
        'm_mapel', 'm_ruangujian', 'tahun_pelajaran', 'm_siswa', 'm_tingkat',
        'draft_ujian', 'jadwal_ujian', 'ujian_siswa', 'soal_data', 'soal_data_couple',
        'soal_opsi', 'kompetensi_dasar', 'jadwal_pelajaran', 'rpp', 'agenda_kegiatan',
        'group_conversations', 'panel_conversations', 'whatsapp_device'
    ];
    protected $fileLocation = [
        'DATA_MASTER.xlsx' => WRITEPATH . '/file/example/DATA_MASTER.xlsx',
        'DATA_MASTER.zip' => WRITEPATH . '/file/example/DATA_MASTER.zip',
        'DATA_MASTER' => WRITEPATH . '/file/example/DATA_MASTER.xlsx',
        'DATA_GURU.xlsx' => WRITEPATH . '/file/example/DATA_GURU.xlsx',
        'DATA_GURU.zip' => WRITEPATH . '/file/example/DATA_GURU.zip',
        'DATA_GURU' => WRITEPATH . '/file/example/DATA_GURU.xlsx',
        'soal.zip' => WRITEPATH . '/file/example/soal.zip',
        'soal.xlsx' => WRITEPATH . '/file/example/soal.xlsx',
        'soal.docx' => WRITEPATH . '/file/example/soal.docx',
        'photo-siswa.zip' => WRITEPATH . '/file/example/photo-siswa.zip',
    ];
    protected $ignoreTable = ['migrations'];
    public function __construct()
    {
        helper(['app', 'Panel/auth']);
        $minialBrowserVersion = [
            'Google Chrome' => [
                'image' => 'chrome.png',
                'version' => 105
            ],
            'Mozilla Firefox' => [
                'image' => 'mozila.png',
                'version' => 104,
            ],
            'Safari' => [
                'image' => 'safari.png',
                'version' => 12
            ],
            'Apple Safari' => [
                'image' => 'safari.png',
                'version' => 15
            ],
        ];
        $bc['no_footer'] = true;
        $bc['no_header'] = true;
        $allow = [
            'panel/app/install/lc', 'panel/app/install', 'panel/app/install/step/1', 'panel/app/install/step/2', 'panel/utils/header',
            'panel/app/install/step/2/new', 'panel/app/install/step/2/restore', 'panel/app/settings', 'panel/utils/sidebar',
            'panel/auth/login', 'panel/auth/logout'
        ];
        if (redison()) autoload_option_to_redis();
        else autoload_options();
        if ((!options('licensekey') || options('eat') < time()) && $_SERVER['REQUEST_METHOD'] == 'GET') {
            $i = false;
            foreach ($allow as $key => $value) {
                if (strpos(current_url(), $value) !== false) {
                    $i = true;
                    break;
                }
            }
            if (!$i) {
                header("Location: " . base_url('panel/app/settings/secret-key'));
                die;
            }
        }

        if (options('browserCheck') == '1') {
            try {
                $b = getBrowser();
                $bvn = (int) explode('.', $b['version'])[0];
                $mbv = $minialBrowserVersion[$b['name']];
                if ($bvn < $mbv['version']) {
                    $bc['browserName'] = $b['name'];
                    $bc['browserImage'] = $mbv['image'];
                    echo view('Mobile/Pages/outdated-browser', $bc);
                    die;
                }
            } catch (\Throwable $th) {
                if (ENVIRONMENT != 'production') {
                    dd($th->getMessage());
                } else {
                    echo view('Mobile/Pages/outdated-browser', $bc);
                }
                die;
            }
        }
    }
}
