<?php

namespace App\Controllers\Panel\App\Settings\BackupRestore;

use App\Controllers\Panel\BC;
use CodeIgniter\API\ResponseTrait;
use Predis\Client as PredisClient;

class Reset extends BC
{
    use ResponseTrait;
    private $allowed_file = [
        'index.html', 'profile-cover.jpg', 'profile-photo.png',
        'foto.svg', 'kementrian.png', 'logo.png', 'logo.svg', 'ttd.png',
        'favicon.ico', 'notification.mp3', 'no-conversations.png',
        'icon-72x72.png', 'icon-96x96.png', 'icon-128x128.png',
        'icon-144x144.png', 'icon-152x152.png', 'icon-192x192.png',
        'icon-384x384.png', 'icon-512x512.png'
    ];
    public function __construct()
    {
        parent::__construct();
    }
    public function index()
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $db = db_connect();
        $redison = redison();
        if ($redison) {
            try {
                $redisConfig = json_decode(options('redis-config'), true);
            } catch (\Throwable $th) {
                //throw $th;
            }
        }
        $now = date("Y-m-d H:i:s");
        $tables = $db->listTables();
        $db->transStart();
        $db->query("SET FOREIGN_KEY_CHECKS=0");
        foreach ($tables as $table) {
            if (in_array($table, ['roles', 'migrations', 'options'])) continue;
            $db->query("TRUNCATE TABLE $table");
        }
        $db->query("SET FOREIGN_KEY_CHECKS=1");
        $db->table("users")->insert([
            'username' => user()->username,
            'fullname' => user()->fullname,
            'password_hash' => user()->password_hash,
            'status' => 1,
            'created_at' => $now,
            'updated_at' => $now
        ]);
        $uid = $db->insertID();
        $db->table("users_roles")->insert(['role_id' => 1, 'user_id' => $uid]);
        $nd = [
            [
                'option_name' => 'logo',
                'option_value' => 'logo.png',
            ],
            [
                'option_name' => 'kementrian_image',
                'option_value' => 'kementrian.png',
            ],
            [
                'option_name' => 'ttd_image',
                'option_value' => 'ttd.png',
            ],
        ];
        save_options($nd);
        $db->transComplete();
        if ($db->transStatus() === FALSE) return $this->fail(['csrf' => csrf_hash(), 'message' => "Aplikasi Gagal Di Reset"], 500);
        try {
            rmbfe('*', FCPATH . 'assets', $this->allowed_file);
            rmbfe('*', WRITEPATH . 'db/backups');
        } catch (\Throwable $th) {
        }
        if ($redison) {
            try {
                $redisClient = new PredisClient([
                    'scheme' => 'tcp',
                    'host'   => $redisConfig['host'],
                    'port'   => $redisConfig['port'],
                    'password' => $redisConfig['password'],
                    'database' => $redisConfig['database'],
                ]);
                $redisClient->connect();
                $redisClient->executeRaw(['FLUSHALL']);
            } catch (\Throwable $th) {
            }
        }
        return $this->respond(['message' => "Aplikasi Berhasil Di Reset"]);
    }
}
