<?php

use App\Controller\BindAPI;

error_reporting(error_level: E_ALL & ~E_DEPRECATED);

if (!is_file(filename: dirname(path: __DIR__, levels: 2) . '/vendor/autoload.php')) {
    exit('Required runtime components are missing. Try running "composer install".' . PHP_EOL);
}

require dirname(path: __DIR__, levels: 2) . '/vendor/autoload.php';


$shortOpts = 'v::'; // version
$shortOpts .= 'q::'; // quiet
$shortOpts .= "h::"; // help

$longOpts = [
    'version::',
    'quiet::',
    'help::'
];

$options = getopt(short_options: $shortOpts, long_options: $longOpts, rest_index: $restIndex);

$arguments = array_slice(array: $argv, offset: $restIndex);

if (array_key_exists(key: 'v', array: $options) || array_key_exists(key: 'version', array: $options)) {
    $arguments = 'showVersion';

    $composerJson = json_decode(json: file_get_contents(filename: dirname(path: __DIR__, levels: 2) . '/composer.json'));
    $name = $composerJson->name;
    $description = $composerJson->description;
    $version = $composerJson->version;
    $buildNumber = $composerJson->build_number;
    $authors = $composerJson->authors;
    // currently only one author, so just handle only first entry
    $authorName = $authors[0]->name;
    $authorEmail = $authors[0]->email;

    echo 'Name: $name' . PHP_EOL;
    echo 'Description: $description' . PHP_EOL;
    echo 'Version: $version' . PHP_EOL;
    echo 'Build Number: $buildNumber' . PHP_EOL;
    echo 'Author: $authorName ($authorEmail)' . PHP_EOL;
    exit(0);
}

if (array_key_exists(key: 'h', array: $options) || array_key_exists(key: 'help', array: $options)) {
    $arguments = "showUsage";
}

if (array_key_exists(key: 'q', array: $options) || array_key_exists(key: 'quiet', array: $options)) {
    $quiet = true;
} else {
    $quiet = false;
}


try {
    $app = new BindAPI(quiet: $quiet);
} catch (Exception $e) {
    echo 'Could not initialize the application: ' . $e->getMessage() . PHP_EOL;
    exit(1);
}


try {
    $app->runCommand(arguments: $arguments);
} catch (Exception $e) {
    $exceptionMessage = $e->getMessage();
    preg_match(pattern: '/\[1045]/', subject: $exceptionMessage, matches: $matches);
    if (!empty($matches)) {
        echo 'Access was denied for a user when trying to access the database.' . PHP_EOL;
    } else {
        echo 'The error message could not be parsed.';
        echo $exceptionMessage . PHP_EOL;
        exit(1);
    }
}


function confirm(string $message = 'Are you sure? ', array $options = ['y', 'n'], string $default = 'n'): bool
{
    // first $options means true, any other false
    echo $message, ' (';
    $first = true;
    foreach ($options as $option) {
        // mark default
        if ($option === $default) {
            $option = strtoupper(string: $option);
        }
        if ($first) {
            echo $option;
            $first = false;
        } else {
            echo '/', $option;
        }
    }
    echo '): ';

    $handle = fopen(filename: "php://stdin", mode: 'r');
    $line = trim(string: fgetc(stream: $handle));
    fclose(stream: $handle);

    if ($line == '') {
        // enter
        $line = $default;
    }

    if ($line == $options[0]) {
        $result = true;
    } else {
        $result = false;
    }

    return $result;
}

