<?php
namespace EasyChat\Controller;

use EasyChat\Library\InstallService;

class Install extends Base {

    protected $layouts = 'layoutclean';

    public function __construct($registry) {
        parent::__construct($registry);
         
        if (is_file(SITE_PATH . DS . 'config.php') && defined('DB_DATABASE') && DB_DATABASE !== '') {
            header('Location: index.php?route=index');
            exit;
        }
    }

    public function index() {
        $this->vars['title'] = 'Установка EasyChat';
        $this->template->view('install');
    }

    public function testConnection() {
        header('Content-Type: application/json; charset=utf-8');

        echo json_encode(InstallService::testConnection(\EasyChat\Library\RequestContext::post()));
        exit;
    }

    public function setup() {
        header('Content-Type: application/json; charset=utf-8');
        $params = \EasyChat\Library\RequestContext::post();
        $params['app_path'] = SITE_PATH;
        $params['runtime_path'] = SITE_PATH;
        $params['site_url'] = HTTP_SERVER;
        $params['edition'] = 'standalone';
        $params['cms_type'] = 'none';
        $params['cms_cron_mode'] = 'standalone';
        $params['write_config'] = '1';
        $params['skip_schema'] = '0';
        echo json_encode(InstallService::install($params));
        exit;
    }
}
