<?php

namespace kernel\services;

use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\helpers\Manifest;
use kernel\helpers\RESTClient;
use kernel\helpers\Version;
use kernel\Request;
use ZipArchive;

class KernelService
{
    protected null|bool $serverAvailable = null;
    protected ModuleService $moduleService;
    protected Files $files;

    public function __construct()
    {
        $this->moduleService = new ModuleService();
        $this->files = new Files();
    }

    public function getKernelInfo(): false|array|string
    {
        $info = [];
        $info['path'] = KERNEL_DIR;
        if (file_exists(KERNEL_DIR . "/manifest.json")) {
            $manifest = json_decode(file_get_contents(KERNEL_DIR . "/manifest.json"), true);
            $manifest = getConst($manifest);
            $info = array_merge($info, $manifest);
        }

        return $info;
    }

    public function isLastVersion(): bool
    {
        if ($this->moduleService->isServerAvailable()) {
            $modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');

            $modules_info = json_decode($modules_info->getBody()->getContents(), true);

            $kernel_info = $this->getKernelInfo();

            $kernelVersion = Version::getIntVersionByString($kernel_info['version']);

            foreach ($modules_info as $mod) {
                $modVersion = Version::getIntVersionByString($mod['version']);
                if ($mod['slug'] === $kernel_info['slug'] && $modVersion <= $kernelVersion) {
                    return true;
                }
            }
        }

        return false;
    }

    public function updateKernel(string $path): bool
    {
        $request = new Request();
        $files = $request->post('files');
        
        $zip = new ZipArchive;
        if (file_exists(ROOT_DIR . $path)) {
            $tmpKernelDir = md5(time());
            $res = $zip->open(ROOT_DIR . $path);
            if ($res === TRUE) {
                $tmpKernelDirFull = RESOURCES_DIR . '/tmp/kernel/' . $tmpKernelDir . "/";
                $zip->extractTo($tmpKernelDirFull);
                $zip->close();
                $this->files->recursiveRemoveKernelDir();
                $this->files->copyKernelFolder($tmpKernelDirFull . 'kernel' , ROOT_DIR . "/kernel");

                foreach ($files as $file) {
                    if ($file === 'bootstrap.php') {
                        $this->files->recursiveRemoveDir(ROOT_DIR . '/bootstrap');
                        $this->files->copyKernelFolder($tmpKernelDirFull . 'bootstrap' , ROOT_DIR . '/bootstrap');
                    }
                    if ($file === 'env.example') {
                        copy($tmpKernelDirFull . $file , ROOT_DIR . '/.' . $file);
                        chmod(ROOT_DIR . '/.' . $file, 0775);
                        continue;
                    }
                    copy($tmpKernelDirFull . $file , ROOT_DIR . '/' . $file);
                    chmod(ROOT_DIR . '/' . $file, 0775);
                }

                $this->files->recursiveRemoveDir($tmpKernelDirFull);
                unlink(ROOT_DIR . $path);

                return true;
            }
        }

        return false;
    }
}