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

declare(strict_types=1);

require_once '/usr/lib/abraflexi-cli/autoload.php';


use Symfony\Component\Console\Application;
use Symfony\Component\Dotenv\Dotenv;
use VitexSoftware\AbraflexiCli\Command\ListCompaniesCommand;
use VitexSoftware\AbraflexiCli\Command\ListEvidencesCommand;
use VitexSoftware\AbraflexiCli\Command\RecordCommand;

$envFile = null;
foreach ($_SERVER['argv'] as $key => $val) {
    if (str_starts_with($val, '--envfile=')) {
        $envFile = substr($val, 10);
        unset($_SERVER['argv'][$key]);
    } elseif ($val === '--envfile') {
        $envFile = $_SERVER['argv'][$key + 1] ?? null;
        unset($_SERVER['argv'][$key], $_SERVER['argv'][$key + 1]);
    }
}

if ($envFile && file_exists($envFile)) {
    (new Dotenv())->usePutenv()->load($envFile);
}

if (file_exists(__DIR__ . '/../.env')) {
    (new Dotenv())->usePutenv()->load(__DIR__ . '/../.env');
}

if (file_exists('.env')) {
    (new Dotenv())->usePutenv()->load('.env');
}

$_SERVER['argv'] = array_values($_SERVER['argv']);

$application = new Application('AbraFlexi CLI', '0.2.0');

$application->add(new ListCompaniesCommand());
$application->add(new ListEvidencesCommand());
$application->add(new RecordCommand());

$application->add(new \VitexSoftware\AbraflexiCli\Command\StatusCommand());

$application->run();
