<?php

class Index extends Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        $this->view->render(__CLASS__ .'/'. __FUNCTION__);
    }

    // AJAX: test the supplied DB credentials (echo 1 = ok, 0 = fail).
    function checkDB(){
        $host = $_POST['dbloca'];
        $user = $_POST['dbuser'];
        $pass = $_POST['dbpass'];
        $db   = $_POST['dbname'];

        $dsn = "mysql:host=$host;dbname=$db;charset=utf8mb4";
        $options = [
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_LAZY,
            PDO::ATTR_EMULATE_PREPARES   => false,
        ];

        try {
            new PDO($dsn, $user, $pass, $options);
            echo 1;
        } catch (\PDOException $e) {
            echo 0;
        }
    }

    // Run the install by delegating to the shared Installer service.
    // Refuses once installed; writes the correct /api config (no DOCUMENT_ROOT path bugs).
    function installation() {
        require_once dirname(__DIR__, 2) . '/vendor/autoload.php';

        $installer = new \App\Services\Installer();
        if ($installer->isInstalled()) {
            http_response_code(403);
            die('Already installed. Remove api/system/.installed to reinstall.');
        }
        if (!$_POST) {
            header('Location: /api/install/');
            die();
        }

        try {
            $installer->run([
                'url'  => 'https://example.com',
                'name' => 'SeedProject',
                'db'   => [
                    'host' => $_POST['dbloca'],
                    'name' => $_POST['dbname'],
                    'user' => $_POST['dbuser'],
                    'pass' => $_POST['dbpass'],
                ],
            ]);
        } catch (\Throwable $e) {
            http_response_code(500);
            die('Install failed: ' . htmlspecialchars($e->getMessage()));
        }

        header('Location: /api/install/i/complete');
    }

}
