<?php

use Webuccino\Install\Core\Session;
use Webuccino\Install\Core\Database;


$driver = $_POST['driver'];
$host = $_POST['host'];
$db_name = $_POST['db_name'];
$db_user = $_POST['db_user'];
$db_password = $_POST['db_password'];
$db_port = is_numeric($_POST['db_port']) ? $_POST['db_port'] : "3306";


$database = new Database($driver, $host, $db_name, $db_user, $db_password, $db_port);


if ($database->checkConnection()) {
  Session::put('connection-success', true);

  write_srm_db_config_native($driver, $host, $db_name, $db_user, $db_password, $db_port);

  $msg = "Connection success";

  echo json_encode([
    "msg" => $msg
  ]);
} else {

  Session::put('connection-success', false);

  $msg = "Connection Failed " . $database->getConnectionError();

  echo json_encode([
    "msg" => $msg
  ]);
}