#!/usr/bin/env php
<?php

foreach ([
    __DIR__."/..",
    __DIR__."/../..",
    __DIR__."/../../..",
] as $dir) {
    if (file_exists($dir."/autoload.php")) {
        define("COMPOSER_VENDOR_PATH", $dir);
        require_once $dir."/autoload.php";
    } elseif (file_exists($dir."/vendor/autoload.php")) {
        define("COMPOSER_VENDOR_PATH", $dir."/vendor");
        require_once $dir."/vendor/autoload.php";
    }
}

$datasetManager = new NoccyLabs\Dataset\DatasetManager();

$datasets = $datasetManager->getAvailableDatasets();

function _printf(string $fmt, ...$args): void {
    $out = sprintf($fmt, ...$args);
    if (!posix_isatty(STDOUT)) {
        $out = preg_replace("<\e\\[[0-9;]+m>", "", $out);
    }
    echo $out;
}

foreach ($datasets as $dataset) {
    _printf("Identifier:  \e[36m%s\e[35m#\e[36;1m%s\e[0m\n", $dataset->getPackageName(), $dataset->getDatasetName());
    //  $dataset->getIdentifier()."\n";
    _printf("    Package: \e[33m%s\e[0m\n", $dataset->getPackageName());
    _printf("    Dataset: \e[33m%s\e[0m\n", $dataset->getDatasetName());
    _printf("    Version: \e[33m%s\e[0m\n", $dataset->getVersion());
    $reader = $dataset->open();
    $rows = 0;
    $headers = null;
    foreach ($reader as $row) {
        if (!$headers) $headers = array_keys($row);
        $rows++;
    }
    _printf("    Rows:    \e[33m%d\e[0m\n", $rows);
    _printf("    Fields:  \e[32;1m%s\e[0m\n", join("\e[0m\n             \e[32;1m",$headers));
}